-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cs
86 lines (74 loc) · 2.45 KB
/
Util.cs
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
using Discord;
using ProcessMunch.Exception;
using TetrLoader.JsonClass.Raw;
namespace Minomuncher;
public static class Util
{
/// <summary>
/// get player replays in args
/// </summary>
/// <param name="players"></param>
public static async Task GetPlayerGames(string[] players, List<string> errors,
IUserMessage message, Dictionary<string, List<string>> playerGames, int? gameCountToDownload)
{
int munchGames = 0;
Dictionary<string, List<Record>> playerGameIds = new();
foreach (var player in players)
{
List<Record> gamesList;
try
{
gamesList = await FetchGameListAsync(player);
if (gameCountToDownload == null)
gameCountToDownload = gamesList.Count;
if (gamesList.Count > gameCountToDownload)
gamesList.RemoveRange((int)gameCountToDownload, gamesList.Count - (int)gameCountToDownload);
}
catch
{
throw new MunchException($"failed to fetch user from {string.Join(",", players)}");
}
munchGames += gamesList.Count;
playerGameIds.Add(player, gamesList);
}
await message.ModifyAsync(properties =>
properties.Content = $"fetched {munchGames} TL games from {JoinWithQuotes(",", players)}\n" +
$"downloading...");
foreach (var playerGameList in playerGameIds)
{
var games = new List<string>();
//ダウンロードと処理
for (var i = 0; i < playerGameList.Value.Count; i++)
{
var record = playerGameList.Value[i];
await message.ModifyAsync(properties =>
properties.Content = $"fetched {munchGames} TL games from {JoinWithQuotes(",", players)}\n" +
$"downloading...({i + 1}/{playerGameList.Value.Count}) {record.replayid}");
try
{
games.Add(await TetrioAPI.GetReplayFromIdAsync(record.replayid));
}
catch (Exception e)
{
errors.Add($"{record.replayid} is not exists");
}
}
playerGames.Add(playerGameList.Key, games);
}
}
/// <summary>
/// 引数のユーザーのそれぞれ過去10試合のゲームをダウンロード
/// </summary>
/// <param name="players"></param>
/// <param name="message"></param>
/// <returns></returns>
private static async Task<List<Record>> FetchGameListAsync(string player)
{
var user = await TetrioAPI.DownloadUserAsync(player);
return (await TetrioAPI.DownloadListOfGameIds(user._id)).ToList();
}
public static string JoinWithQuotes(string separator, IEnumerable<string> values)
{
return string.Join(separator, values.Select(v => $"`{v}`"));
}
}