~ruther/NosSmooth

ref: 493935a2f77a55defb0c4416a92a57acdfd97842 NosSmooth/Core/NosSmooth.Game/PacketHandlers/Relations/FriendInitResponder.cs -rw-r--r-- 2.3 KiB
493935a2 — František Boháček fix(packets): correctly deserialize list with nullable elements inline 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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;

/// <summary>
/// A friends initialization responder.
/// </summary>
public class FriendInitResponder : IPacketResponder<FInfoPacket>, IPacketResponder<FInitPacket>
{
    private readonly Game _game;

    /// <summary>
    /// Initializes a new instance of the <see cref="FriendInitResponder"/> class.
    /// </summary>
    /// <param name="game">The game.</param>
    public FriendInitResponder(Game game)
    {
        _game = game;
    }

    /// <inheritdoc />
    public async Task<Result> Respond(PacketEventArgs<FInfoPacket> 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();
    }

    /// <inheritdoc />
    public async Task<Result> Respond(PacketEventArgs<FInitPacket> 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();
    }
}
Do not follow this link