Esempio n. 1
0
        public async Task <FindPlayerResult> FindPlayer(string searchTerm)
        {
            using Stream? stream =
                      await this._api.Client.GetStreamAsync(
                          $"/data/player?include=names&filter=login==*{searchTerm}*");

            using JsonDocument json = await JsonDocument.ParseAsync(stream);

            JsonElement      dataElement = json.RootElement.GetProperty("data");
            FindPlayerResult result      = new FindPlayerResult();

            foreach (JsonElement element in dataElement.EnumerateArray())
            {
                var typeElement = element.GetProperty("type");
                if (typeElement.GetString() != "player")
                {
                    continue;
                }

                var attributes = element.GetProperty("attributes");

                var username = attributes.GetProperty("login").GetString();
                if (username is not null)
                {
                    result.Usernames.Add(username);
                }
            }

            return(result);
        }
Esempio n. 2
0
        public async Task FindPlayerAsync(string searchTerm)
        {
            FindPlayerResult findPlayerResult = await _playerService.FindPlayer(searchTerm);

            if (findPlayerResult.Usernames.Count == 0)
            {
                await Context.ReplyAsync($"Found no players when searching for '{searchTerm}'");
            }
            else
            {
                string players = string.Join(", ", findPlayerResult.Usernames);
                await Context.ReplyAsync($"Found the following players: {players}");
            }
        }