-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
92 lines (79 loc) · 3.22 KB
/
Program.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
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ClashOfClans.Models;
using Newtonsoft.Json;
namespace ClanManager
{
internal static class Program
{
private static async Task Main(string[] args)
{
if (args is null)
{
throw new ArgumentNullException(nameof(args));
}
var requestFile = "request.json";
try
{
requestFile = args[0];
}
catch (IndexOutOfRangeException)
{
Console.WriteLine($"Missing request file. Using default request file: {requestFile}");
}
var spreadsheetConnector = new Spreadsheet();
Request request;
using (var stream = new StreamReader(requestFile))
{
string json = stream.ReadToEnd();
request = JsonConvert.DeserializeObject<Request>(json);
}
string clanTag = request.ClanTag;
string page = request.Page;
var coc = new ClashOfClansService();
var clan = await coc.GetClanAsync(clanTag).ConfigureAwait(false);
var playerData = new Dictionary<string, IDictionary<string, object>>();
foreach (var clanMember in clan.MemberList)
{
var player = await coc.GetPlayerAsync(clanMember.Tag).ConfigureAwait(false);
var items = player.Heroes
.Concat(player.Troops)
.Concat(player.Spells)
.Where(item => item.Village == Village.Home && !item.Name.Contains("Super"));
playerData[player.Tag] = new Dictionary<string, object>
{
["Tag"] = player.Tag,
["Name"] = player.Name,
["Town Hall"] = player.TownHallLevel,
};
foreach (var item in items)
{
playerData[player.Tag][item.Name] = item.Level;
}
}
var columns = request.Columns;
var sheet = new List<IList<object>> { columns.Select(column => column.Label).ToList<object>() };
foreach (var clanMember in clan.MemberList)
{
var data = playerData[clanMember.Tag];
var row = columns.Select(column =>
{
var name = column.Name;
return data.ContainsKey(name) ? data[name] : "N/T";
});
sheet.Add(row.ToList<object>());
}
var emptyLine = new List<object>();
var lastUpdateMetadata = new List<object> { "", "Última att:", };
var lastUpdateValue = new List<object> { "", DateTime.Now.ToString("HH:mm, dd/MM") };
sheet.Add(emptyLine);
sheet.Add(lastUpdateMetadata);
sheet.Add(lastUpdateValue);
var response = await spreadsheetConnector.UpdateDataAsync(request.SpreadsheetId, sheet, page).ConfigureAwait(false);
Console.WriteLine(response);
}
}
}