~ruther/NosSmooth

ref: ecb4ceb20b0dbc5fc4a342415c109ccbb00996ca NosSmooth/Core/NosSmooth.Game/Apis/NostaleChatPacketApi.cs -rw-r--r-- 3.9 KiB
ecb4ceb2 — Rutherther chore: make updates to get rid of warnings 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
//
//  NostaleChatPacketApi.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.Core.Client;
using NosSmooth.Packets.Enums.Chat;
using NosSmooth.Packets.Enums.Entities;
using NosSmooth.Packets.Server.Chat;
using Remora.Results;

namespace NosSmooth.Game.Apis;

/// <summary>
/// Packet api for sending and receiving messages.
/// </summary>
public class NostaleChatPacketApi
{
    // TODO: check length of the messages
    private readonly INostaleClient _client;

    /// <summary>
    /// Initializes a new instance of the <see cref="NostaleChatPacketApi"/> class.
    /// </summary>
    /// <param name="client">The nostale client.</param>
    public NostaleChatPacketApi(INostaleClient client)
    {
        _client = client;
    }

    /// <summary>
    /// Receive the given system message on the client.
    /// </summary>
    /// <remarks>
    /// Won't send anything to the server, it's just the client who will see the message.
    /// </remarks>
    /// <param name="content">The content of the message.</param>
    /// <param name="color">The color of the message.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Task<Result> ReceiveSystemMessageAsync(string content, SayColor color = SayColor.Yellow, CancellationToken ct = default)
        => _client.ReceivePacketAsync(new SayPacket(EntityType.Map, 0, color, content), ct);

    /// <summary>
    /// Sends the given message to the public chat.
    /// </summary>
    /// <param name="content">The content of the message.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Task<Result> SendMessageAsync(string content, CancellationToken ct = default)
        => _client.SendPacketAsync(new Packets.Client.Chat.SayPacket(content), ct);

    /// <summary>
    /// Sends the given message to the family chat.
    /// </summary>
    /// <remarks>
    /// Should be used only if the user is in a family.
    /// </remarks>
    /// <param name="content">The content of the message.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Task<Result> SendFamilyMessage(string content, CancellationToken ct = default)
        => _client.SendPacketAsync(":" + content, ct);

    /// <summary>
    /// Sends the given message to the group chat.
    /// </summary>
    /// <remarks>
    /// Should be used only if the user is in a group. (with people, not only pets).
    /// </remarks>
    /// <param name="content">The content of the message.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Task<Result> SendGroupMessage(string content, CancellationToken ct = default)
        => _client.SendPacketAsync(";" + content, ct);

    /// <summary>
    /// Sends the given message to the target only.
    /// </summary>
    /// <remarks>
    /// Won't return if the whisper has actually came through, event has to be hooked
    /// up to know if the whisper has went through (and you can know only for messages that are sufficiently long).
    /// </remarks>
    /// <param name="targetName">The name of the user you want to whisper to.</param>
    /// <param name="content">The content of the message.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Task<Result> SendWhisper(string targetName, string content, CancellationToken ct = default)
        => _client.SendPacketAsync($"/{targetName} {content}", ct);
}
Do not follow this link