~ruther/NosSmooth

ref: 6334c0f5e711b3d9567687ae3ebf72fa41691b43 NosSmooth/Core/NosSmooth.Game/PacketHandlers/Raids/RaidResponder.cs -rw-r--r-- 4.0 KiB
6334c0f5 — František Boháček tests: add ptctl deserialization test 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
//  RaidResponder.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 System.Net.Http.Headers;
using NosSmooth.Core.Packets;
using NosSmooth.Game.Data.Info;
using NosSmooth.Game.Data.Raids;
using NosSmooth.Game.Data.Social;
using NosSmooth.Game.Events.Core;
using NosSmooth.Game.Events.Raids;
using NosSmooth.Packets.Enums.Raids;
using NosSmooth.Packets.Server.Raids;
using Remora.Results;

namespace NosSmooth.Game.PacketHandlers.Raids;

/// <summary>
/// A responder to <see cref="RaidPacket"/>.
/// </summary>
public class RaidResponder : IPacketResponder<RaidPacket>
{
    private readonly Game _game;
    private readonly EventDispatcher _eventDispatcher;

    /// <summary>
    /// Initializes a new instance of the <see cref="RaidResponder"/> class.
    /// </summary>
    /// <param name="game">The game.</param>
    /// <param name="eventDispatcher">The event dispatcher.</param>
    public RaidResponder(Game game, EventDispatcher eventDispatcher)
    {
        _game = game;
        _eventDispatcher = eventDispatcher;

    }

    /// <inheritdoc />
    public async Task<Result> Respond(PacketEventArgs<RaidPacket> packetArgs, CancellationToken ct = default)
    {
        var packet = packetArgs.Packet;
        if (packet.Type is not(RaidPacketType.Leader or RaidPacketType.ListMembers or RaidPacketType.PlayerHealths or RaidPacketType.JoinLeave))
        {
            return Result.FromSuccess();
        }

        Raid? prevRaid = null;
        var currentRaid = await _game.UpdateRaidAsync
        (
            raid =>
            {
                prevRaid = raid;
                switch (packet.Type)
                {
                    case RaidPacketType.JoinLeave:
                        if (packet.JoinLeaveType is not null && packet.JoinLeaveType == RaidJoinLeaveType.PlayerLeft)
                        { // the player has left.
                            prevRaid = raid with
                            {
                                State = RaidState.Left
                            };

                            return null;
                        }

                        return raid;
                    case RaidPacketType.Leader:
                        if (packet.LeaderId is null)
                        { // set the raid to null.
                            return null;
                        }

                        return raid with
                        {
                            Leader = raid.Members?.FirstOrDefault(x => x.PlayerId == packet.LeaderId.Value)
                        };
                    case RaidPacketType.ListMembers:
                        return raid with
                        {
                            Members = raid.Members?.Where(x => packet.ListMembersPlayerIds?.Contains(x.PlayerId) ?? true).ToList()
                        };
                    case RaidPacketType.PlayerHealths:
                        // update healths
                        foreach (var member in raid.Members ?? (IReadOnlyList<GroupMember>)Array.Empty<GroupMember>())
                        {
                            var data = packet.PlayerHealths?.FirstOrDefault(x => x.PlayerId == member.PlayerId);

                            if (data is not null)
                            {
                                member.Hp ??= new Health();
                                member.Mp ??= new Health();

                                member.Hp.Percentage = data.HpPercentage;
                                member.Mp.Percentage = data.MpPercentage;
                            }
                        }
                        return raid;
                }

                return raid;
            },
            ct: ct
        );

        if (currentRaid is null && prevRaid is not null)
        {
            return await _eventDispatcher.DispatchEvent(new RaidFinishedEvent(prevRaid), ct);
        }

        return Result.FromSuccess();
    }
}
Do not follow this link