~ruther/NosSmooth

ref: 18b22db021c0bbd6bfb5739721e01710a86c6a6d NosSmooth/Samples/DataBrowser/Commands/ItemInfoCommand.cs -rw-r--r-- 2.3 KiB
18b22db0 — František Boháček feat: add packet api for chat and skills 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
//
//  ItemInfoCommand.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.Logging;
using NosSmooth.Data.Abstractions;
using NosSmooth.Data.Abstractions.Language;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Results;

namespace DataBrowser.Commands;

/// <summary>
/// Command for getting information about an item.
/// </summary>
public class ItemInfoCommand : CommandGroup
{
    private readonly IInfoService _infoService;
    private readonly ILanguageService _languageService;

    /// <summary>
    /// Initializes a new instance of the <see cref="ItemInfoCommand"/> class.
    /// </summary>
    /// <param name="infoService">The info service.</param>
    /// <param name="languageService">The language service.</param>
    public ItemInfoCommand(IInfoService infoService, ILanguageService languageService)
    {
        _infoService = infoService;
        _languageService = languageService;
    }

    /// <summary>
    /// Gets the info about the specified item.
    /// </summary>
    /// <param name="vnum">The vnum of the item.</param>
    /// <param name="language">The language.</param>
    /// <returns>A result that may or may not succeeded.</returns>
    [Command("item")]
    public async Task<Result> HandleItemInfo(int vnum, Language language = Language.Uk)
    {
        var itemResult = await _infoService.GetItemInfoAsync(vnum, CancellationToken);
        if (!itemResult.IsSuccess)
        {
            return Result.FromError(itemResult);
        }

        var translationResult = await _languageService.GetTranslationAsync
            (itemResult.Entity.Name, language, CancellationToken);
        if (!translationResult.IsSuccess)
        {
            return Result.FromError(translationResult);
        }

        Console.WriteLine("Found item {0}", translationResult.Entity);
        Console.WriteLine("  Type: {0}", itemResult.Entity.Type);
        Console.WriteLine("  SubType: {0}", itemResult.Entity.SubType);
        Console.WriteLine("  BagType: {0}", itemResult.Entity.BagType);
        Console.WriteLine("  Data: {0}", string.Join('|', itemResult.Entity.Data));

        return Result.FromSuccess();
    }
}
Do not follow this link