~ruther/NosSmooth.Local

ref: d5b3c3ffb40aa86f582a0f26fc0376caa245f220 NosSmooth.Local/src/Samples/HighLevel/SimplePiiBot/Commands/EntityCommands.cs -rw-r--r-- 5.0 KiB
d5b3c3ff — Rutherther Merge pull request #18 from Rutherther/feat/optional-binding 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
//
//  EntityCommands.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.ChatCommands;
using NosSmooth.Extensions.Combat.Errors;
using NosSmooth.Game;
using NosSmooth.Game.Apis;
using NosSmooth.LocalBinding;
using NosSmooth.LocalBinding.Hooks;
using NosSmooth.LocalBinding.Objects;
using NosSmooth.LocalBinding.Structs;
using NosSmooth.Packets.Enums.Chat;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Results;

namespace SimplePiiBot.Commands;

/// <summary>
/// Represents command group for combat commands.
/// </summary>
public class EntityCommands : CommandGroup
{
    private readonly Game _game;
    private readonly IHookManager _hookManager;
    private readonly NosThreadSynchronizer _synchronizer;
    private readonly SceneManager _sceneManager;
    private readonly FeedbackService _feedbackService;

    /// <summary>
    /// Initializes a new instance of the <see cref="EntityCommands"/> class.
    /// </summary>
    /// <param name="game">The game.</param>
    /// <param name="hookManager">The hook manager.</param>
    /// <param name="synchronizer">The thread synchronizer.</param>
    /// <param name="sceneManager">The scene manager.</param>
    /// <param name="feedbackService">The feedback service.</param>
    public EntityCommands
    (
        Game game,
        IHookManager hookManager,
        NosThreadSynchronizer synchronizer,
        SceneManager sceneManager,
        FeedbackService feedbackService
    )
    {
        _game = game;
        _hookManager = hookManager;
        _synchronizer = synchronizer;
        _sceneManager = sceneManager;
        _feedbackService = feedbackService;
    }

    /// <summary>
    /// Show close entities.
    /// </summary>
    /// <param name="range">The range to look.</param>
    /// <returns>A task that may or may not have succeeded.</returns>
    [Command("close")]
    public async Task<Result> HandleCloseEntitiesAsync(int range = 10)
    {
        var map = _game.CurrentMap;
        var character = _game.Character;
        if (map is null)
        {
            return new MapNotInitializedError();
        }

        if (character is null)
        {
            return new CharacterNotInitializedError();
        }

        var position = character.Position;
        if (position is null)
        {
            return new CharacterNotInitializedError("Position");
        }

        var entities = map.Entities
            .GetEntities()
            .Where(x => x.Position?.DistanceSquared(position.Value) <= range * range)
            .ToList();

        if (entities.Count == 0)
        {
            await _feedbackService.SendInfoMessageAsync("No entity found.", CancellationToken);
        }

        foreach (var entity in entities)
        {
            await _feedbackService.SendMessageAsync($"Found entity: {entity.Id}", SayColor.Yellow, CancellationToken);
        }

        return Result.FromSuccess();
    }

    /// <summary>
    /// Focus the given entity.
    /// </summary>
    /// <param name="entityId">The entity id to focus.</param>
    /// <returns>A task that may or may not have succeeded.</returns>
    [Command("focus")]
    public async Task<Result> HandleFocusAsync(int entityId)
    {
        var entityResult = _sceneManager.FindEntity(entityId);
        if (!entityResult.IsDefined(out var entity))
        {
            return Result.FromError(entityResult);
        }

        return await _synchronizer.SynchronizeAsync
        (
            () => _hookManager.EntityFocus.MapResult
            (
                focus => focus.WrapperFunction.MapResult(wrapper => wrapper(entity))
            ),
            CancellationToken
        );
    }

    /// <summary>
    /// Follow the given entity.
    /// </summary>
    /// <param name="entityId">The entity id to follow.</param>
    /// <returns>A task that may or may not have succeeded.</returns>
    [Command("follow")]
    public async Task<Result> HandleFollowAsync(int entityId)
    {
        var entityResult = _sceneManager.FindEntity(entityId);
        if (!entityResult.IsDefined(out var entity))
        {
            return Result.FromError(entityResult);
        }

        return await _synchronizer.SynchronizeAsync
        (
            () => _hookManager.EntityFollow.MapResult
            (
                follow => follow.WrapperFunction.MapResult(wrapper => wrapper(entity))
            ),
            CancellationToken
        );
    }

    /// <summary>
    /// Stop following an entity.
    /// </summary>
    /// <returns>A task that may or may not have succeeded.</returns>
    [Command("unfollow")]
    public async Task<Result> HandleUnfollowAsync()
    {
        return await _synchronizer.SynchronizeAsync
        (
            () => _hookManager.EntityUnfollow.MapResult
            (
                unfollow => unfollow.WrapperFunction.MapResult(wrapper => wrapper())
            ),
            CancellationToken
        );
    }
}
Do not follow this link