From ad1078c592d416b0a88aa4b85da94274fbc53be5 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 6 Jan 2023 23:01:31 +0100 Subject: [PATCH] feat(game): add friends processing --- Core/NosSmooth.Game/Data/Chat/Friend.cs | 16 +--- .../Extensions/ServiceCollectionExtensions.cs | 1 + Core/NosSmooth.Game/Game.cs | 28 +++++++ .../Relations/FriendInitResponder.cs | 83 +++++++++++++++++++ 4 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 Core/NosSmooth.Game/PacketHandlers/Relations/FriendInitResponder.cs diff --git a/Core/NosSmooth.Game/Data/Chat/Friend.cs b/Core/NosSmooth.Game/Data/Chat/Friend.cs index aae785a..416a479 100644 --- a/Core/NosSmooth.Game/Data/Chat/Friend.cs +++ b/Core/NosSmooth.Game/Data/Chat/Friend.cs @@ -4,23 +4,15 @@ // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using NosSmooth.Packets.Enums.Relations; + namespace NosSmooth.Game.Data.Chat; /// /// Represents character's friend. /// -public class Friend +public record Friend(long PlayerId, CharacterRelationType RelationType) { - /// - /// The id of the character. - /// - public long CharacterId { get; internal set; } - - // - // The type of the relation. - // - // public CharacterRelationType RelationType { get; internal set; } - /// /// The name of the character. /// @@ -29,5 +21,5 @@ public class Friend /// /// Whether the friend is connected to the server. /// - public bool IsOnline { get; internal set; } + public bool IsConnected { get; internal set; } } \ No newline at end of file diff --git a/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs b/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs index def27fd..a71fc86 100644 --- a/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs +++ b/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs @@ -42,6 +42,7 @@ public static class ServiceCollectionExtensions .AddPacketResponder() .AddPacketResponder() .AddPacketResponder() + .AddPacketResponder() .AddPacketResponder() .AddPacketResponder() .AddPacketResponder() diff --git a/Core/NosSmooth.Game/Game.cs b/Core/NosSmooth.Game/Game.cs index 5cf72ef..53be409 100644 --- a/Core/NosSmooth.Game/Game.cs +++ b/Core/NosSmooth.Game/Game.cs @@ -188,6 +188,34 @@ public class Game : IStatefulEntity return family; } + /// + /// Creates the friends if it is null, or updates the current friends. + /// + /// The function for creating the friends. + /// The function for updating the friends. + /// Whether to release the semaphore used for changing the friends. + /// The cancellation token for cancelling the operation. + /// The updated friends. + internal async Task?> CreateOrUpdateFriendsAsync + ( + Func?> create, + Func, IReadOnlyList?> update, + bool releaseSemaphore = true, + CancellationToken ct = default + ) + { + return await CreateOrUpdateAsync + ( + GameSemaphoreType.Friends, + () => Friends, + c => Friends = c, + create, + update, + releaseSemaphore, + ct + ); + } + /// /// Creates the group if it is null, or updates the current group. /// diff --git a/Core/NosSmooth.Game/PacketHandlers/Relations/FriendInitResponder.cs b/Core/NosSmooth.Game/PacketHandlers/Relations/FriendInitResponder.cs new file mode 100644 index 0000000..816f6c8 --- /dev/null +++ b/Core/NosSmooth.Game/PacketHandlers/Relations/FriendInitResponder.cs @@ -0,0 +1,83 @@ +// +// FriendInitResponder.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosSmooth.Core.Packets; +using NosSmooth.Game.Data.Chat; +using NosSmooth.Packets.Enums.Relations; +using NosSmooth.Packets.Server.Relations; +using Remora.Results; + +namespace NosSmooth.Game.PacketHandlers.Relations; + +/// +/// A friends initialization responder. +/// +public class FriendInitResponder : IPacketResponder, IPacketResponder +{ + private readonly Game _game; + + /// + /// Initializes a new instance of the class. + /// + /// The game. + public FriendInitResponder(Game game) + { + _game = game; + } + + /// + public async Task Respond(PacketEventArgs packetArgs, CancellationToken ct = default) + { + var packet = packetArgs.Packet; + + await _game.CreateOrUpdateFriendsAsync + ( + () => null, + friends => friends + .Select( + x => + { + if (x.PlayerId == packet.PlayerId) + { + x.IsConnected = packet.IsConnected; + x.CharacterName = packet.Name; + } + + return x; + }) + .ToList(), + ct: ct + ); + + return Result.FromSuccess(); + } + + /// + public async Task Respond(PacketEventArgs packetArgs, CancellationToken ct = default) + { + var packet = packetArgs.Packet; + var friends = packet.FriendSubPackets + .Select + ( + x => new Friend(x.PlayerId, x.RelationType ?? CharacterRelationType.Blocked) + { + PlayerId = x.PlayerId, + CharacterName = x.Name, + IsConnected = x.IsConnected + } + ) + .ToList(); + + await _game.CreateOrUpdateFriendsAsync + ( + () => friends, + _ => friends, + ct: ct + ); + + return Result.FromSuccess(); + } +} \ No newline at end of file -- 2.49.0