~ruther/NosSmooth

ref: ee5e9661e7e771ce6947630093a4eb4a4c16c876 NosSmooth/Core/NosSmooth.Game/Game.cs -rw-r--r-- 2.7 KiB
ee5e9661 — František Boháček feat: add effects to living entities 3 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
//
//  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.Game.Data.Characters;
using NosSmooth.Game.Data.Maps;
using NosSmooth.Game.Data.Raids;

namespace NosSmooth.Game;

/// <summary>
/// Represents base nostale game class with the character, current map, friends and current raid.
/// </summary>
public class Game
{
    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)
    {
        _options = options.Value;
    }

    /// <summary>
    /// Gets the playing character of the client.
    /// </summary>
    public Character? Character { 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;
            MapChanged?.CancelAfter(TimeSpan.FromSeconds(_options.EntityCacheDuration));
        }
    }

    /// <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>
    /// Cancellation token for changing the map to use in memory cache.
    /// </summary>
    internal CancellationTokenSource? MapChanged { get; private set; }

    /// <summary>
    /// Gets the set semaphore used for changing internal fields.
    /// </summary>
    internal SemaphoreSlim SetSemaphore { get; } = new SemaphoreSlim(1, 1);

    /// <summary>
    /// Ensures that Character is not null.
    /// </summary>
    /// <param name="releaseSemaphore">Whether to release the semaphore.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A Task that may or may not have succeeded.</returns>
    internal async Task<Character> EnsureCharacterCreatedAsync(bool releaseSemaphore, CancellationToken ct = default)
    {
        await SetSemaphore.WaitAsync(ct);
        Character? character = Character;
        if (Character is null)
        {
            Character = character = new Character();
        }

        if (releaseSemaphore)
        {
            SetSemaphore.Release();
        }

        return character!;
    }
}
Do not follow this link