~ruther/NosSmooth

ref: d85ab0c2e24d30643533ce155ea5e7a1d4c181fb NosSmooth/Extensions/NosSmooth.Extensions.Combat/CombatState.cs -rw-r--r-- 4.5 KiB
d85ab0c2 — Rutherther feat(packets): make su packet position nullable 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
//  CombatState.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.Diagnostics.CodeAnalysis;
using NosSmooth.Core.Client;
using NosSmooth.Extensions.Combat.Operations;

namespace NosSmooth.Extensions.Combat;

/// <inheritdoc />
internal class CombatState : ICombatState
{
    private readonly Dictionary<OperationQueueType, LinkedList<ICombatOperation>> _operations;
    private readonly Dictionary<OperationQueueType, ICombatOperation> _currentOperations;

    /// <summary>
    /// Initializes a new instance of the <see cref="CombatState"/> class.
    /// </summary>
    /// <param name="client">The NosTale client.</param>
    /// <param name="game">The game.</param>
    /// <param name="combatManager">The combat manager.</param>
    public CombatState(INostaleClient client, Game.Game game, CombatManager combatManager)
    {
        Client = client;
        Game = game;
        CombatManager = combatManager;
        _operations = new Dictionary<OperationQueueType, LinkedList<ICombatOperation>>();
        _currentOperations = new Dictionary<OperationQueueType, ICombatOperation>();
    }

    /// <summary>
    /// Gets whether the combat state should be quit.
    /// </summary>
    public bool ShouldQuit { get; private set; }

    /// <inheritdoc/>
    public CombatManager CombatManager { get; }

    /// <inheritdoc/>
    public Game.Game Game { get; }

    /// <inheritdoc/>
    public INostaleClient Client { get; }

    /// <summary>
    /// Gets whether the manager may currently quit.
    /// </summary>
    /// <remarks>
    /// Used for finishing the current operations.
    /// </remarks>
    public bool CanQuit => _currentOperations.Values.All(x => !x.IsExecuting() || x.IsFinished());

    /// <inheritdoc/>
    public bool IsWaitingOnOperation => _currentOperations.Any(x => !x.Value.IsExecuting());

    /// <summary>
    /// Move to next operation, if available.
    /// </summary>
    /// <param name="queueType">The queue type to move to next operation in.</param>
    /// <returns>Next operation, if any.</returns>
    public ICombatOperation? NextOperation(OperationQueueType queueType)
    {
        if (_operations.ContainsKey(queueType))
        {
            var nextOperation = _operations[queueType].FirstOrDefault();

            if (nextOperation is not null)
            {
                _operations[queueType].RemoveFirst();
                _currentOperations[queueType] = nextOperation;
                return nextOperation;
            }
        }

        return null;
    }

    /// <inheritdoc/>
    public IReadOnlyList<ICombatOperation> GetWaitingForOperations()
    {
        return _currentOperations.Values.Where(x => !x.IsExecuting()).ToList();
    }

    /// <inheritdoc/>
    public ICombatOperation? GetCurrentOperation(OperationQueueType queueType)
        => _currentOperations.GetValueOrDefault(queueType);

    /// <inheritdoc/>
    public bool IsExecutingOperation(OperationQueueType queueType, [NotNullWhen(true)] out ICombatOperation? operation)
    {
        operation = GetCurrentOperation(queueType);
        return operation is not null && operation.IsExecuting();
    }

    /// <inheritdoc/>
    public void QuitCombat()
    {
        ShouldQuit = true;
    }

    /// <inheritdoc/>
    public void SetCurrentOperation
        (ICombatOperation operation, bool emptyQueue = false, bool prependCurrentOperationToQueue = false)
    {
        var type = operation.QueueType;

        if (!_operations.ContainsKey(type))
        {
            _operations[type] = new LinkedList<ICombatOperation>();
        }

        if (emptyQueue)
        {
            _operations[type].Clear();
        }

        if (prependCurrentOperationToQueue)
        {
            _operations[type].AddFirst(operation);
            return;
        }

        if (_currentOperations.ContainsKey(type))
        {
            _currentOperations[type].Dispose();
        }

        _currentOperations[type] = operation;
    }

    /// <inheritdoc/>
    public void EnqueueOperation(ICombatOperation operation)
    {
        var type = operation.QueueType;

        if (!_operations.ContainsKey(type))
        {
            _operations[type] = new LinkedList<ICombatOperation>();
        }

        _operations[type].AddLast(operation);
    }

    /// <inheritdoc/>
    public void RemoveOperations(Func<ICombatOperation, bool> filter)
    {
        throw new NotImplementedException();
    }
}
Do not follow this link