-
Notifications
You must be signed in to change notification settings - Fork 0
/
Provider.TMioCampaign.as
151 lines (128 loc) · 5.27 KB
/
Provider.TMioCampaign.as
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) 2023 MisfitMaid <misfit@misfitmaid.com>
* Use of this source code is governed by the MIT license, which
* can be found in the LICENSE file.
*/
namespace Ticker {
class TMioCampaignLeaderboardProvider : TickerItemProvider {
string[] leaderboards;
bool fetchingCampaign = false;
void getCampaignList() {
if (fetchingCampaign) return; // only one at a time pls
fetchingCampaign = true;
auto req = Net::HttpGet("https://trackmania.io/api/campaigns/0");
while (!req.Finished()) yield();
Json::Value data = Json::Parse(req.String());
Json::Value camps = data["campaigns"];
for (uint i = 0; i < camps.Length; i++) {
if (bool(camps[i]['tracked'])) {
Json::Value c = camps[i];
string leaderboard = getCampaignLeaderboard(c["id"], c["clubid"]);
leaderboards.InsertLast("https://trackmania.io/api/leaderboard/activity/" + leaderboard + "/0");
}
}
fetchingCampaign = false;
}
string getCampaignLeaderboard(uint id, uint clubid = 0) {
if (IO::FileExists(IO::FromStorageFolder("campaigns/"+id))) {
IO::File store(IO::FromStorageFolder("campaigns/"+id), IO::FileMode::Read);
string lid = store.ReadToEnd();
store.Close();
return lid;
}
string url;
if (clubid == 0) {
url = "https://trackmania.io/api/officialcampaign/" + id;
} else {
url = "https://trackmania.io/api/campaign/" + clubid + "/" + id;
}
auto req2 = Net::HttpGet(url);
while (!req2.Finished()) yield();
Json::Value cData = Json::Parse(req2.String());
string lid = string(cData["leaderboarduid"]);
if (!IO::FolderExists(IO::FromStorageFolder("campaigns"))) IO::CreateFolder(IO::FromStorageFolder("campaigns"));
IO::File store(IO::FromStorageFolder("campaigns/"+id), IO::FileMode::Write);
store.Write(lid);
store.Close();
return lid;
}
string getID() { return "Ticker/TMioCampaignLeaderboard"; }
TickerItem@[]@ items;
TickerItem@[] getItems() {
return items;
}
void OnUpdate() {
if (leaderboards.Length == 0) getCampaignList();
@items = fetchNewRecords();
}
TickerItem@[]@ fetchNewRecords() {
TickerItem@[] allRecords;
Net::HttpRequest@[] reqs;
for (uint i = 0; i < leaderboards.Length; i++) {
reqs.InsertLast(Net::HttpGet(leaderboards[i]));
}
bool allDone = true;
for (uint i = 0; i < reqs.Length; i++) {
if (!reqs[i].Finished()) allDone = false;
}
while (!allReqsDone(reqs)) yield();
for (uint i = 0; i < reqs.Length; i++) {
Json::Value lData = Json::Parse(reqs[i].String());
for (uint k = 0; k < lData.Length; k++) {
if (lData[k]["map"].GetType() == Json::Type::Null) continue;
if (lData[k]["player"].GetType() == Json::Type::Null) continue;
allRecords.InsertLast(TMIOImprovedTime(lData[k]));
}
}
return allRecords;
}
bool allReqsDone(Net::HttpRequest@[]@ reqs) {
for (uint i = 0; i < reqs.Length; i++) {
if (!reqs[i].Finished()) return false;
}
return true;
}
}
class TMIOImprovedTime : BaseTickerItem {
Json::Value@ data;
uint64 parsedTime;
TMIOImprovedTime() {}
TMIOImprovedTime(Json::Value@ inDat) {
@data = inDat;
parsedTime = ParseTime(inDat["drivenat"]);
}
string getItemText() override {
string map = Text::StripFormatCodes(data["map"]["name"]);
string player = Text::StripFormatCodes(data["player"]["name"]);
string time = Time::Format(data["time"]);
string diff = Time::Format(Math::Abs(int(data["timediff"])), true, false);
string at = relTimeStr();
return "\\$666" + at + " ago:\\$z " + map + " \\$666in\\$z " + time + " \\$66c(-" + diff + ")\\$666 by \\$z" + player;
}
uint64 getSortTime() const override {
return parsedTime;
}
string relTimeStr() {
int64 diff = Time::Stamp - parsedTime;
int num = 0;
string unit;
if (diff > 86400) {
num = int(Math::Floor(diff/86400));
unit = "d";
} else if (diff > 3600) {
num = int(Math::Floor(diff/3600));
unit = "h";
} else if (diff > 60) {
num = int(Math::Floor(diff/60));
unit = "m";
} else {
num = diff;
unit = "s";
}
return Text::Format("%d", num) + unit;
}
void OnItemClick() override {
OpenBrowserURL("https://trackmania.io/#/leaderboard/" + string(data["map"]["mapUid"]));
}
}
}