~ruther/NosSmooth

ref: bbb771c93b1b4b8b76f8c70e5ae298a045250324 NosSmooth/Core/NosSmooth.Game/Game.cs -rw-r--r-- 14.4 KiB
bbb771c9 — Rutherther Merge pull request #80 from plsfixrito/ilivingenitityvnum 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//
//  Game.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 Microsoft.Extensions.Options;
using NosSmooth.Core.Stateful;
using NosSmooth.Game.Data;
using NosSmooth.Game.Data.Characters;
using NosSmooth.Game.Data.Chat;
using NosSmooth.Game.Data.Inventory;
using NosSmooth.Game.Data.Maps;
using NosSmooth.Game.Data.Mates;
using NosSmooth.Game.Data.Raids;
using NosSmooth.Game.Data.Social;

namespace NosSmooth.Game;

/// <summary>
/// Represents base nostale game class with the character, current map, friends and current raid.
/// </summary>
public class Game : IStatefulEntity
{
    private readonly GameOptions _options;
    private Map? _currentMap;

    /// <summary>
    /// Initializes a new instance of the <see cref="Game"/> class.
    /// </summary>
    /// <param name="options">The options for the game.</param>
    public Game(IOptions<GameOptions> options)
    {
        Semaphores = new GameSemaphores();
        _options = options.Value;
    }

    /// <summary>
    /// Gets the game semaphores.
    /// </summary>
    internal GameSemaphores Semaphores { get; }

    /// <summary>
    /// Gets the playing character of the client.
    /// </summary>
    public Character? Character { get; internal set; }

    /// <summary>
    /// Gets the mates of the current character.
    /// </summary>
    public Mates? Mates { get; internal set; }

    /// <summary>
    /// Gets or sets the inventory of the character.
    /// </summary>
    public Inventory? Inventory { get; internal set; }

    /// <summary>
    /// Get or sets the friends of the character.
    /// </summary>
    public IReadOnlyList<Friend>? Friends { get; internal set; }

    /// <summary>
    /// Gets or sets the skills of the player.
    /// </summary>
    public Skills? Skills { get; internal set; }

    /// <summary>
    /// Gets or sets the family.
    /// </summary>
    public Family? Family { get; internal set; }

    /// <summary>
    /// Gets or sets the group the player is in.
    /// </summary>
    public Group? Group { get; internal set; }

    /// <summary>
    /// Gets the current map of the client.
    /// </summary>
    /// <remarks>
    /// Will be null until current map packet is received.
    /// </remarks>
    public Map? CurrentMap
    {
        get => _currentMap;
        internal set { _currentMap = value; }
    }

    /// <summary>
    /// Gets the active raid the client is currently on.
    /// </summary>
    /// <remarks>
    /// May be null if there is no raid in progress.
    /// </remarks>
    public Raid? CurrentRaid { get; internal set; }

    /// <summary>
    /// Creates the mates if they are null, or updates the current mates.
    /// </summary>
    /// <param name="create">The function for creating the mates.</param>
    /// <param name="update">The function for updating the mates.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the mates.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated mates.</returns>
    internal Task<Mates?> CreateOrUpdateMatesAsync
    (
        Func<Mates?> create,
        Func<Mates, Mates?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        return CreateOrUpdateAsync
        (
            GameSemaphoreType.Mates,
            () => Mates,
            s => Mates = s,
            create,
            update,
            releaseSemaphore,
            ct
        );
    }

    /// <summary>
    /// Creates the skills if they are null, or updates the current skills.
    /// </summary>
    /// <param name="create">The function for creating the skills.</param>
    /// <param name="update">The function for updating the skills.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the skills.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated skills.</returns>
    internal Task<Skills?> CreateOrUpdateSkillsAsync
    (
        Func<Skills?> create,
        Func<Skills, Skills?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        return CreateOrUpdateAsync
        (
            GameSemaphoreType.Skills,
            () => Skills,
            s => Skills = s,
            create,
            update,
            releaseSemaphore,
            ct
        );
    }

    /// <summary>
    /// Creates the inventory if it is null, or updates the current inventory.
    /// </summary>
    /// <param name="create">The function for creating the inventory.</param>
    /// <param name="update">The function for updating the inventory.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the inventory.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated inventory.</returns>
    internal Task<Inventory?> CreateOrUpdateInventoryAsync
    (
        Func<Inventory?> create,
        Func<Inventory, Inventory?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        return CreateOrUpdateAsync
        (
            GameSemaphoreType.Inventory,
            () => Inventory,
            i => Inventory = i,
            create,
            update,
            releaseSemaphore,
            ct
        );
    }

    /// <summary>
    /// Creates the family if it is null, or updates the current family.
    /// </summary>
    /// <param name="create">The function for creating the family.</param>
    /// <param name="update">The function for updating the family.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the family.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated family.</returns>
    internal async Task<Family?> CreateOrUpdateFamilyAsync
    (
        Func<Family?> create,
        Func<Family, Family?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        var family = await CreateOrUpdateAsync
        (
            GameSemaphoreType.Family,
            () => Family,
            c => Family = c,
            create,
            update,
            releaseSemaphore,
            ct
        );

        await CreateOrUpdateCharacterAsync
        (
            () => new Character
            {
                Family = family
            },
            c =>
            {
                c.Family = family;
                return c;
            },
            ct: ct
        );

        return family;
    }

    /// <summary>
    /// Creates the friends if it is null, or updates the current friends.
    /// </summary>
    /// <param name="create">The function for creating the friends.</param>
    /// <param name="update">The function for updating the friends.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the friends.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated friends.</returns>
    internal Task<IReadOnlyList<Friend>?> CreateOrUpdateFriendsAsync
    (
        Func<IReadOnlyList<Friend>?> create,
        Func<IReadOnlyList<Friend>, IReadOnlyList<Friend>?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        return CreateOrUpdateAsync
        (
            GameSemaphoreType.Friends,
            () => Friends,
            c => Friends = c,
            create,
            update,
            releaseSemaphore,
            ct
        );
    }

    /// <summary>
    /// Creates the group if it is null, or updates the current group.
    /// </summary>
    /// <param name="create">The function for creating the group.</param>
    /// <param name="update">The function for updating the group.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the group.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated group.</returns>
    internal Task<Group?> CreateOrUpdateGroupAsync
    (
        Func<Group?> create,
        Func<Group, Group?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        return CreateOrUpdateAsync
        (
            GameSemaphoreType.Group,
            () => Group,
            c => Group = c,
            create,
            update,
            releaseSemaphore,
            ct
        );
    }

    /// <summary>
    /// Creates the character if it is null, or updates the current character.
    /// </summary>
    /// <param name="create">The function for creating the character.</param>
    /// <param name="update">The function for updating the character.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the character.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated character.</returns>
    internal Task<Character?> CreateOrUpdateCharacterAsync
    (
        Func<Character?> create,
        Func<Character, Character?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        return CreateOrUpdateAsync
        (
            GameSemaphoreType.Character,
            () => Character,
            c => Character = c,
            create,
            update,
            releaseSemaphore,
            ct
        );
    }

    /// <summary>
    /// Creates the map if it is null, or updates the current map.
    /// </summary>
    /// <param name="create">The function for creating the map.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the map.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated character.</returns>
    internal Task<Map?> CreateMapAsync
    (
        Func<Map?> create,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        return CreateAsync
        (
            GameSemaphoreType.Map,
            m => CurrentMap = m,
            create,
            releaseSemaphore,
            ct
        );
    }

    /// <summary>
    /// Creates the map if it is null, or updates the current map.
    /// </summary>
    /// <param name="create">The function for creating the map.</param>
    /// <param name="update">The function for updating the map.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the map.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated character.</returns>
    internal Task<Map?> CreateOrUpdateMapAsync
    (
        Func<Map?> create,
        Func<Map?, Map?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        return CreateOrUpdateAsync<Map?>
        (
            GameSemaphoreType.Map,
            () => CurrentMap,
            m => CurrentMap = m,
            create,
            update,
            releaseSemaphore,
            ct
        );
    }

    /// <summary>
    /// Updates the current raid, if it is not null.
    /// </summary>
    /// <param name="update">The function for updating the raid.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the raid.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated raid.</returns>
    internal Task<Raid?> UpdateRaidAsync
    (
        Func<Raid, Raid?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        return CreateOrUpdateAsync
        (
            GameSemaphoreType.Raid,
            () => CurrentRaid,
            m => CurrentRaid = m,
            () => null,
            update,
            releaseSemaphore,
            ct
        );
    }

    /// <summary>
    /// Creates the raid if it is null, or updates the current raid.
    /// </summary>
    /// <param name="create">The function for creating the raid.</param>
    /// <param name="update">The function for updating the raid.</param>
    /// <param name="releaseSemaphore">Whether to release the semaphore used for changing the raid.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>The updated raid.</returns>
    internal Task<Raid?> CreateOrUpdateRaidAsync
    (
        Func<Raid?> create,
        Func<Raid, Raid?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        return CreateOrUpdateAsync
        (
            GameSemaphoreType.Raid,
            () => CurrentRaid,
            m => CurrentRaid = m,
            create,
            update,
            releaseSemaphore,
            ct
        );
    }

    private async Task<T> CreateAsync<T>
    (
        GameSemaphoreType type,
        Action<T> set,
        Func<T> create,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        await Semaphores.AcquireSemaphore(type, ct);

        try
        {
            var current = create();
            set(current);
            return current;
        }
        finally
        {
            if (releaseSemaphore)
            {
                Semaphores.ReleaseSemaphore(type);
            }
        }
    }

    private async Task<T?> CreateOrUpdateAsync<T>
    (
        GameSemaphoreType type,
        Func<T?> get,
        Action<T?> set,
        Func<T?> create,
        Func<T, T?> update,
        bool releaseSemaphore = true,
        CancellationToken ct = default
    )
    {
        await Semaphores.AcquireSemaphore(type, ct);

        try
        {
            var current = get();
            if (current is null)
            {
                current = create();
            }
            else
            {
                current = update(current);
            }

            set(current);
            return current;
        }
        finally
        {
            if (releaseSemaphore)
            {
                Semaphores.ReleaseSemaphore(type);
            }
        }
    }
}
Do not follow this link