Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player left notification #278

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using MultiplayerMod.Core.Extensions;
using NUnit.Framework;

namespace MultiplayerMod.Test.Core.Extensions;

[TestFixture]
public class LinkedListExtensionTests {

[Test]
public void MustRemoveFirstNode() {
var list = CreateList();
list.ForEach(
(value, node) => {
if (value == 1)
list.Remove(node);
}
);
Assert.AreEqual(expected: new[] { 2, 3 }, actual: list);
}

[Test]
public void MustRemoveNode() {
var list = CreateList();
list.ForEach(
(value, node) => {
if (value == 2)
list.Remove(node);
}
);
Assert.AreEqual(expected: new[] { 1, 3 }, actual: list);
}

[Test]
public void MustRemoveLastNode() {
var list = CreateList();
list.ForEach(
(value, node) => {
if (value == 3)
list.Remove(node);
}
);
Assert.AreEqual(expected: new[] { 1, 2 }, actual: list);
}

private LinkedList<int> CreateList() {
var list = new LinkedList<int>();
list.AddLast(1);
list.AddLast(2);
list.AddLast(3);
return list;
}

}
18 changes: 18 additions & 0 deletions src/MultiplayerMod/Core/Extensions/LinkedListExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;

namespace MultiplayerMod.Core.Extensions;

public static class LinkedListExtensions {

// ReSharper disable Unity.PerformanceAnalysis
public static void ForEach<T>(this LinkedList<T> list, Action<T, LinkedListNode<T>> action) {
var node = list.First;
while (node != null) {
var next = node.Next;
action(node.Value, node);
node = next;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Collections.Generic;
using MultiplayerMod.Core.Dependency;
using MultiplayerMod.Core.Events;
using MultiplayerMod.Core.Extensions;
using MultiplayerMod.Core.Unity;
using MultiplayerMod.Multiplayer.Players.Events;
using UnityEngine;

namespace MultiplayerMod.Multiplayer.Components;

public class MultiplayerPlayerNotifier : MultiplayerMonoBehaviour {

[InjectDependency]
private readonly EventDispatcher dispatcher = null!;

private readonly float notificationTimeout = 10f;
polycone marked this conversation as resolved.
Show resolved Hide resolved
private readonly LinkedList<Notification> notifications = new();
private EventSubscription subscription = null!;
private bool removalPending;

protected override void Awake() {
base.Awake();
subscription = dispatcher.Subscribe<PlayerLeftEvent>(OnPlayerLeft);
NotificationManager.Instance.notificationRemoved += OnNotificationRemoved;
}

private void OnDestroy() {
if (NotificationManager.Instance != null)
NotificationManager.Instance.notificationRemoved -= OnNotificationRemoved;
subscription.Cancel();
}

private void OnPlayerLeft(PlayerLeftEvent @event) {
var playerName = @event.Player.Profile.PlayerName;
var message = $"{playerName} left";
var description = $"{playerName} {(@event.Gracefully ? "left" : "disconnected")}";
AddNotification(message, description, NotificationType.BadMinor);
}

private void AddNotification(string message, string tooltip, NotificationType type) {
var notification = new Notification(
message,
type,
tooltip: (_, _) => tooltip,
expires: false,
clear_on_click: true,
show_dismiss_button: true
) {
GameTime = Time.unscaledTime,
Time = KTime.Instance.UnscaledGameTime,
Delay = -Time.unscaledTime
};
NotificationManager.Instance.AddNotification(notification);
notifications.AddLast(notification);
}

private void OnNotificationRemoved(Notification notification) {
if (!removalPending)
notifications.Remove(notification);
}

private void Update() {
notifications.ForEach(
(notification, node) => {
if (Time.unscaledTime - notification.GameTime < notificationTimeout)
return;

removalPending = true;
NotificationManager.Instance.RemoveNotification(notification);
notifications.Remove(node);
removalPending = false;
}
);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using JetBrains.Annotations;
using MultiplayerMod.Core.Dependency;
using MultiplayerMod.Core.Events;
using MultiplayerMod.Core.Unity;
using MultiplayerMod.Multiplayer.Components;
using MultiplayerMod.Multiplayer.CoreOperations.Events;
using MultiplayerMod.Multiplayer.World.Debug;
using UnityEngine;

namespace MultiplayerMod.Multiplayer.CoreOperations;

Expand All @@ -15,8 +15,14 @@ public MultiplayerGameObjectsSpawner(EventDispatcher events) {
events.Subscribe<GameStartedEvent>(OnGameStarted);
}

// ReSharper disable once ObjectCreationAsStatement
private void OnGameStarted(GameStartedEvent _) {
UnityObject.CreateWithComponent<CursorManager, WorldDebugSnapshotRunner>();
new GameObject(
"Multiplayer",
typeof(CursorManager),
typeof(WorldDebugSnapshotRunner),
typeof(MultiplayerPlayerNotifier)
);
}

}