This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CampaignAPI.Script.txt
274 lines (240 loc) · 9.89 KB
/
CampaignAPI.Script.txt
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Component : CampaignAPI
*/
#Const Version "2021-01-14"
#Const ScriptName "Libs/Nadeo/TMNext/TrackMania/API/CampaignAPI.Script.txt"
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Libraries
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
#Include "TextLib" as TL
#Include "Libs/Nadeo/MenuLibs/Common/Manialink/ManiaView2.Script.txt" as MV
#Include "Libs/Nadeo/CommonLibs/Common/Http.Script.txt" as Http
#Include "Libs/Nadeo/TMNext/TrackMania/Structures/CampaignStruct.Script.txt" as CampaignStruct
#Include "Libs/Nadeo/TMNext/TrackMania/Menu/Constants.Script.txt" as Consts
#Include "Libs/Nadeo/TMNext/TrackMania/Config.Script.txt" as Config
#Include "Libs/Nadeo/TMNext/TrackMania/Structures/HttpStruct.Script.txt" as HttpStruct
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Constants
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
#Const C_Name "lib-campaign-api" //< Component name
#Const P "LibCampaignAPI_" //< Prefix use to differentiate functions/variables in the script
#Const C_API_Campaign "/api/token/campaign"
#Const C_RouteParameter_CampaignId "CampaignId"
#Const C_QueryParameter_Offset "offset"
#Const C_QueryParameter_Length "length"
#Const C_Route_GetList "/official"
#Const C_Route_GetMonth "/month"
#Const C_Headers [
"Accept" => "application/json",
"Content-Type" => "application/json"
]
#Struct LibCampaignAPI_K_HttpCampaignList {
HttpStruct::LibHttpStruct_K_HttpCampaign[] campaignList;
Integer itemCount;
}
#Struct LibCampaignAPI_K_ResponseGetCampaignList {
CampaignStruct::LibCampaignStruct_K_Campaign[] CampaignList;
Integer CampaignsTotal;
}
#Struct LibCampaignAPI_K_ResponseGetMonthlyCampaignList {
CampaignStruct::LibCampaignStruct_K_MonthlyCampaign[] CampaignList;
Integer CampaignsTotal;
}
#Struct LibCampaignAPI_K_HttpResponseGetMonthlyCampaignList {
HttpStruct::LibHttpStruct_K_HttpMonthlyCampaign[] monthList;
Integer itemCount;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
/** Start a request to get a list of campaigns
*
* @param _Offset The starting index
* @param _Length The length of the list
*
* @return The request
*/
Http::LibCommonHttp_K_Request GetCampaignList(Integer _Offset, Integer _Length) {
declare Text[Text] QueryArray = [
C_QueryParameter_Offset => ""^_Offset,
C_QueryParameter_Length => ""^_Length
];
declare Text Query = Http::CreateQueryString(QueryArray);
return Http::CreateGet(Config::Get().APIBaseUrl^C_API_Campaign^C_Route_GetList^Query, C_Headers);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Parse the response to the GetCampaignList request
LibCampaignAPI_K_ResponseGetCampaignList GetResponseFromGetCampaignList(Http::LibCommonHttp_K_Request _Request) {
declare LibCampaignAPI_K_ResponseGetCampaignList ResponseGetCampaignList;
declare LibCampaignAPI_K_HttpCampaignList HttpCampaignList;
HttpCampaignList.fromjson(Http::GetResult(_Request));
foreach (HttpCampaign in HttpCampaignList.campaignList) {
ResponseGetCampaignList.CampaignList.add(CampaignStruct::GetCampaignFromHttpCampaign(HttpCampaign));
}
ResponseGetCampaignList.CampaignsTotal = HttpCampaignList.itemCount;
return ResponseGetCampaignList;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
/** Start a request to get a list of monthly campaigns
*
* @param _Offset The starting index
* @param _Length The length of the list
*
* @return The request
*/
Http::LibCommonHttp_K_Request GetMonthlyCampaignList(Integer _Offset, Integer _Length) {
declare Text[Text] QueryArray = [
C_QueryParameter_Offset => ""^_Offset,
C_QueryParameter_Length => ""^_Length
];
declare Text Query = Http::CreateQueryString(QueryArray);
return Http::CreateGet(Config::Get().APIBaseUrl^C_API_Campaign^C_Route_GetMonth^Query, C_Headers);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Parse the response to the GetMonthlyCampaignList request
LibCampaignAPI_K_ResponseGetMonthlyCampaignList GetResponseFromGetMonthlyCampaignList(Http::LibCommonHttp_K_Request _Request) {
declare LibCampaignAPI_K_HttpResponseGetMonthlyCampaignList HttpResponseGetMonthlyCampaignList;
HttpResponseGetMonthlyCampaignList.fromjson(Http::GetResult(_Request));
declare LibCampaignAPI_K_ResponseGetMonthlyCampaignList ResponseMonthlyCampaignList;
ResponseMonthlyCampaignList.CampaignsTotal = HttpResponseGetMonthlyCampaignList.itemCount;
foreach (HttpMonthlyCampaign in HttpResponseGetMonthlyCampaignList.monthList) {
declare MonthlyCampaign = CampaignStruct::LibCampaignStruct_K_MonthlyCampaign {
Id = TL::ToInteger(HttpMonthlyCampaign.month^""^HttpMonthlyCampaign.year),
Year = HttpMonthlyCampaign.year,
Month = HttpMonthlyCampaign.month,
LastDay = HttpMonthlyCampaign.lastDay,
Media = CampaignStruct::LibCampaignStruct_K_Media {
ButtonBackgroundUrl = HttpMonthlyCampaign.media.buttonBackgroundUrl,
ButtonForegroundUrl = HttpMonthlyCampaign.media.buttonForegroundUrl,
DecalUrl = HttpMonthlyCampaign.media.decalUrl,
PopUpBackgroundUrl = HttpMonthlyCampaign.media.popUpBackgroundUrl,
PopUpImageUrl = HttpMonthlyCampaign.media.popUpImageUrl,
LiveButtonBackgroundUrl = HttpMonthlyCampaign.media.liveButtonBackgroundUrl,
LiveButtonForegroundUrl = HttpMonthlyCampaign.media.liveButtonForegroundUrl
},
IsLive = False
};
for (MonthDay, 0, MonthlyCampaign.LastDay-1) {
MonthlyCampaign.DailyMaps[MonthDay] = CampaignStruct::LibCampaignStruct_K_DailyMap {
MonthDay = MonthDay
};
}
foreach (HttpDailyMap in HttpMonthlyCampaign.days) {
MonthlyCampaign.DailyMaps[HttpDailyMap.monthDay-1] = CampaignStruct::LibCampaignStruct_K_DailyMap {
CampaignId = HttpDailyMap.campaignId,
SeasonId = HttpDailyMap.seasonUid,
Day = HttpDailyMap.day,
MonthDay = HttpDailyMap.monthDay,
Map = CampaignStruct::LibCampaignStruct_K_Map {
Uid = HttpDailyMap.mapUid
},
RelativeStart = HttpDailyMap.relativeStart,
RelativeEnd = HttpDailyMap.relativeEnd,
RelativeNow = Now,
LoadStatus = CampaignStruct::C_LoadStatus_NotLoaded
};
if (
!MonthlyCampaign.IsLive && (
MonthlyCampaign.DailyMaps[HttpDailyMap.monthDay-1].RelativeStart > 0 ||
MonthlyCampaign.DailyMaps[HttpDailyMap.monthDay-1].RelativeEnd > 0
)
) {
MonthlyCampaign.IsLive = True;
}
}
ResponseMonthlyCampaignList.CampaignList.add(MonthlyCampaign);
}
return ResponseMonthlyCampaignList;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
/** Get the component
*
* @return The component id
*/
Text Component() {
return MV::Component(
C_Name,
"""
""",
"""
#Include "TextLib" as {{{P}}}TL
{{{dumptype(LibCampaignAPI_K_ResponseGetMonthlyCampaignList)}}}
{{{dumptype(LibCampaignAPI_K_HttpResponseGetMonthlyCampaignList)}}}
#Const {{{P}}}C_API_Campaign {{{dump(C_API_Campaign)}}}
#Const {{{P}}}C_RouteParameter_CampaignId {{{dump(C_RouteParameter_CampaignId)}}}
#Const {{{P}}}C_QueryParameter_Offset {{{dump(C_QueryParameter_Offset)}}}
#Const {{{P}}}C_QueryParameter_Length {{{dump(C_QueryParameter_Length)}}}
#Const {{{P}}}C_Route_GetMonth {{{dump(C_Route_GetMonth)}}}
#Const {{{P}}}C_Headers {{{dump(C_Headers)}}}
""",
"""
/** Start a request to get a list of monthly campaigns
*
* @param _ClubId The Id of the club to fetch
* @param _Offset The starting index
* @param _Length The length of the list
*
* @return The request
*/
{{{Http::P}}}K_Request {{{P}}}GetMonthlyCampaignList(Integer _Offset, Integer _Length) {
declare Text[Text] QueryArray = [
{{{P}}}C_QueryParameter_Offset => ""^_Offset,
{{{P}}}C_QueryParameter_Length => ""^_Length
];
declare Text Query = {{{Http::P}}}CreateQueryString(QueryArray);
return {{{Http::P}}}CreateGet({{{Config::P}}}Get().APIBaseUrl^{{{P}}}C_API_Campaign^{{{P}}}C_Route_GetMonth^Query, {{{P}}}C_Headers);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Parse the response to the GetMonthlyCampaignList request
{{{P}}}K_ResponseGetMonthlyCampaignList {{{P}}}GetResponseFromGetMonthlyCampaignList({{{Http::P}}}K_Request _Request) {
declare {{{P}}}K_HttpResponseGetMonthlyCampaignList HttpResponseGetMonthlyCampaignList;
HttpResponseGetMonthlyCampaignList.fromjson({{{Http::P}}}GetResult(_Request));
declare {{{P}}}K_ResponseGetMonthlyCampaignList ResponseMonthlyCampaignList;
ResponseMonthlyCampaignList.CampaignsTotal = HttpResponseGetMonthlyCampaignList.itemCount;
foreach (HttpMonthlyCampaign in HttpResponseGetMonthlyCampaignList.monthList) {
declare MonthlyCampaign = {{{CampaignStruct::P}}}K_MonthlyCampaign {
Id = {{{P}}}TL::ToInteger(HttpMonthlyCampaign.month^""^HttpMonthlyCampaign.year),
Year = HttpMonthlyCampaign.year,
Month = HttpMonthlyCampaign.month,
LastDay = HttpMonthlyCampaign.lastDay,
Media = {{{CampaignStruct::P}}}K_Media {
ButtonBackgroundUrl = HttpMonthlyCampaign.media.buttonBackgroundUrl,
ButtonForegroundUrl = HttpMonthlyCampaign.media.buttonForegroundUrl,
DecalUrl = HttpMonthlyCampaign.media.decalUrl,
PopUpBackgroundUrl = HttpMonthlyCampaign.media.popUpBackgroundUrl,
PopUpImageUrl = HttpMonthlyCampaign.media.popUpImageUrl,
LiveButtonBackgroundUrl = HttpMonthlyCampaign.media.liveButtonBackgroundUrl,
LiveButtonForegroundUrl = HttpMonthlyCampaign.media.liveButtonForegroundUrl
}
};
for (MonthDay, 0, {{{Consts::C_MonthDay_Total}}}-1) {
MonthlyCampaign.DailyMaps[MonthDay] = {{{CampaignStruct::P}}}K_DailyMap {
MonthDay = MonthDay
};
}
foreach (HttpDailyMap in HttpMonthlyCampaign.days) {
MonthlyCampaign.DailyMaps[HttpDailyMap.monthDay-1] = {{{CampaignStruct::P}}}K_DailyMap {
CampaignId = HttpDailyMap.campaignId,
SeasonId = HttpDailyMap.seasonUid,
Day = HttpDailyMap.day,
MonthDay = HttpDailyMap.monthDay,
Map = {{{CampaignStruct::P}}}K_Map {
Uid = HttpDailyMap.mapUid
},
RelativeStart = HttpDailyMap.relativeStart,
RelativeEnd = HttpDailyMap.relativeEnd,
RelativeNow = Now
};
}
ResponseMonthlyCampaignList.CampaignList.add(MonthlyCampaign);
}
return ResponseMonthlyCampaignList;
}
""",
[
Http::Component(),
CampaignStruct::Component(),
HttpStruct::Component(),
Config::Component()
],
[]
);
}