//
// 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 =>
{
var subPacket = packet.FriendSubPackets.FirstOrDefault(y => x.PlayerId == y.PlayerId);
if (subPacket is not null)
{
x.IsConnected = subPacket.IsConnected;
x.CharacterName = subPacket.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)
{
PlayerId = x.PlayerId,
CharacterName = x.Name,
IsConnected = x.IsConnected
}
)
.ToList();
await _game.CreateOrUpdateFriendsAsync
(
() => friends,
_ => friends,
ct: ct
);
return Result.FromSuccess();
}
}