-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.php
689 lines (627 loc) · 27.3 KB
/
Main.php
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
<?php
// Manialink main script - Generate the ManiaLink Script for ManiaPlanet
require(__DIR__."/includes/functions.php");
require(__DIR__."/class/class.cacheManager.php");
$cacheManager = new cacheManager();
if(isClientName('maniaplanet'))
{
// Get vars
$QuestId = intval(filter_input(INPUT_GET, 'QuestId')); // Quest Id needed for every request
$State = strtolower(filter_input(INPUT_GET, 'State')); // Action to do, required for every request
$Token = filter_input(INPUT_GET, 'Token'); // Used for the RemoveToken state
$SimpleBoard = boolval(filter_input(INPUT_GET, 'SimpleBoard')); // Used to display a board without start quest button (e.g. quest started elsewhere w/o the board)
$SimpleBoard = ($SimpleBoard == true) ? "True" : "False";
$ShootManiaCall = boolval(filter_input(INPUT_GET, 'SM')); // Used to know if it's a ShootMania call, else it's considered a TM² one
$HideGuiGet = strtolower(filter_input(INPUT_GET, 'HideGui'));
$DisplayPosition = boolval(isset($_GET["DisplayPosition"])); // Used during map edition to know your exact position (call it w/ MT clip)
$DisplayMakingOf = boolval(isset($_GET["MakingOf"])); // Used during the creation of a making of to show the map name & display cost top left (call it w/ MT clip)
// Set constants used in the script
$BgColor = "000B"; // Background color of the board window
$HideGui = "no";
if ($HideGuiGet == "yes") {
// Hide the background and some UI elements when you click on the "Start Quest" button of the board
// Useful if you don't want to display another window to the player when (s)he starts the quest
$HideGui = "yes";
$BgColor = "000F";
}
// Name of the RaceTime constant of GUIPlayer in TM²/SM used for a check
$startTimeVar = "RaceStartTime";
// Name of the RequireContext condition for the script to run in-game
$RequiredContext = "CTmMlScriptIngame";
// Additional script included for ShootMania, concerning Obstacle to prevent jumps from cp to cp
$scriptSMObstaclePreventJump = "";
if ($ShootManiaCall == true) {
$startTimeVar = "StartTime";
$RequiredContext = "CSmMlScriptIngame";
$scriptSMObstaclePreventJump = '
// Little check SM Obstacle specific to avoid any CP jump during a quest
if (LoadedTitle.TitleId == "obstacle@smokegun") {
declare LocalPlayer <=> InputPlayer;
declare netread Net_UsedJump for LocalPlayer = False;
if (Net_UsedJump) {
if (DebugMode) log("Jump used. Quest cancelled.");
clearPersistentData();
return;
}
}
';
}
// URL used in the few requests done in the script (to get the board, the tokens and register a completed quest)
$urlToRequest = ($_SERVER['HTTPS'] == "on") ? "https://" : "http://";
$urlToRequest .= $_SERVER['HTTP_HOST'].str_replace("Main.php", "", $_SERVER['SCRIPT_NAME']);
// Get the quest info
$QuestInfoEmpty = array("title" => "", "map_uid" => "", "author_login" => "");
if ($State == "board" or $State == "start") {
// Quest info are cached for 1h
$cacheId = "tm2ml_board_q".$QuestId."_info";
$QuestInfo = $cacheManager->get($cacheId);
if ($cacheManager->getCacheHit() == false) {
// Include database info
require(__DIR__."/includes/dbConfig.php");
$dbh = databaseConnect($dbHost, $dbUser, $dbPwd, $dbName); // Database Handler
$sql = "SELECT * FROM quests WHERE id = :questid"; // Single row res
$params = array(":questid" => $QuestId);
$qh = $dbh->prepare($sql);
$qexec = $qh->execute($params);
$QuestInfo = $qh->fetch(PDO::FETCH_ASSOC);
$qh = null;
$cacheManager->set($cacheId, $QuestInfo, 3600);
}
if (empty($QuestInfo)) $QuestInfo = $QuestInfoEmpty;
} else {
$QuestInfo = $QuestInfoEmpty;
}
// If we only want to log the Player positions for mapping purpose, we just return this script
// Display the player position constantly
// Press Enter of Backspace to stop the loop
if ($DisplayPosition) {
echo '
<manialink version="2">
<frame>
<label id="Position" pos="-27.0 53.2" size="53.8 8.8" textprefix="$o$s" textcolor="FFF" valign="top" halign="center"/>
</frame>
<script><!--
#RequireContext '.$RequiredContext.'
#Include "TextLib" as TextLib
main() {
declare CMlLabel LblPosition = (Page.GetFirstChild("Position") as CMlLabel);
declare Boolean Continue = True;
declare Integer DisplayFrame = 10;
declare Integer CurrentFrame = 0;
while(Continue) {
if (DisplayFrame < CurrentFrame) {
LblPosition.SetText(TextLib::ToText(GUIPlayer.Position));
CurrentFrame = 0;
}
CurrentFrame += 1;
foreach (Event in PendingEvents)
{
if (Event.KeyCode == 20 || Event.KeyCode == 109)
{
Continue = False;
LblPosition.SetText("");
log("DisplayPositionScript stopped.");
}
}
yield;
}
}
--></script>
</manialink>';
return;
// If we only want to display the map name for making of purpose, we just return this script
// Display the map name & display cost top left of the screen
} else if ($DisplayMakingOf) {
echo '
<manialink version="2">
<frame hidden="0">
<quad id="MsgBackground" sizen="250 11" posn="-145 86 3" valign="top" bgcolor="0005" halign="center"/>
<label id="Message" hidden="0" text="" sizen="180 90" posn="-155 85 3" textcolor="FFFF" maxline="25" autonewline="1" valign="top" halign="left"/>
</frame>
<script><!--
#Include "TextLib" as TL
main() {
declare CMlLabel Message = (Page.GetFirstChild("Message") as CMlLabel);
Message.SetText(Map.MapName ^ "\nC: " ^ TL::ToText(Map.CopperPrice));
}
--></script>
</manialink>';
return;
}
if ($QuestId == 0) return; // Exit if QuestId isn't properly provided
// Echo whole manialink
echo '
<manialink version="2">
<timeout>15</timeout>
<frame id="Window" hidden="0">
<quad id="Background" sizen="160 120" posn="0 80 1" valign="top" bgcolor="'.$BgColor.'" halign="center"/>
<quad id="TitleBackground" sizen="160 10" posn="0 80 2" valign="top" bgcolor="000F" halign="center"/>
<label id="Close" style="TextValueBig" sizen="30 10" posn="73 78.5 3" text="x" textcolor="F30F" scriptevents="1" valign="top" halign="center"/>
<label id="Title" sizen="160 90" posn="0 77.5 3" textprefix="$o$s" textcolor="FFFF" valign="top" halign="center"/>
<label id="Message" sizen="130 90" posn="-60 60 3" textcolor="FF0F" maxline="25" autonewline="1" valign="top" halign="left"/>
<label id="StartQuest" style="CardButtonMedium" hidden="1" sizen="100 10" posn="0 -30 3" text="Start Quest" textcolor="FF0F" scriptevents="1" valign="top" halign="center"/>
<label id="ChangeSorting" style="CardButtonMedium" hidden="1" sizen="100 10" posn="76 -30 3" text="Sort by PB ⏱" textcolor="FF0F" scriptevents="1" valign="top" halign="right"/>
<label id="Left" style="TextValueBig" sizen="30 10" posn="-30 -20 3" text="<" scriptevents="1" valign="top" halign="center"/>
<label id="Right" style="TextValueBig" sizen="30 10" posn="30 -20 3" text=">" scriptevents="1" valign="top" halign="center"/>
</frame>
<script><!--
#RequireContext '.$RequiredContext.'
#Include "TextLib" as TextLib
#Include "MathLib" as MathLib
// Defining struct for the requests responses
#Struct SPlayer {
Text login;
Text nickname;
Integer I_firstTime;
Integer I_bestTime;
Text firstTime;
Text bestTime;
}
#Struct SBoardJsonResponse {
SPlayer[] Players;
Text QuestShortDesc;
Text QuestFullDesc;
Text QuestTitleList;
Text QuestTitleEmptyList;
}
#Struct SToken {
Integer id;
}
#Struct STokensJsonResponse {
SToken[] Tokens;
}
// Enum sorting options
#Const SortOpt_FirstCompletionDate 0
#Const SortOpt_PersonalBest 1
#Const SortOpt_FirstCompletionTime 2
// Global declarations //
// QuestId concerned
declare Integer G_QuestId;
// Quest window UI vars
declare CMlFrame Window;
declare CMlLabel Title;
declare CMlLabel Message;
declare CMlLabel ChangeSorting;
declare CMlLabel Left;
declare CMlLabel Right;
declare Text HideGui;
// Variables used to display the players list properly in the board
declare Integer MaxNumPerPage;
declare Integer NumOfPages;
declare Integer CurPage;
declare Integer CurSorting;
declare SPlayer[] PlayersSortedByFirstDate;
declare SPlayer[] PlayersSortedByBestTime;
declare SPlayer[] PlayersSortedByFirstTime;
// Check if all the tokens are collected to trigger server processing if yes
Boolean TokensAreCollected()
{
declare persistent Boolean[][Integer] Per_LocalTokensCollected for Map;
if (Per_LocalTokensCollected[G_QuestId].count == 0) return False;
for(i,0,Per_LocalTokensCollected[G_QuestId].count-1)
{
if (!Per_LocalTokensCollected[G_QuestId][i]) return False;
}
return True;
}
// Clear persistent data
// Called when the quest is started, supposedly completed or RaceStartTime mismatch
Void clearPersistentData() {
declare persistent Boolean[][Integer] Per_LocalTokensCollected for Map;
declare persistent Text[][Integer] Pe_LocalTokensIds for Map;
declare persistent Text[][Integer] Pe_PlayerPos for Map;
declare persistent Integer[Integer] P_PlayerRaceStartTime for Map;
declare persistent Text[Integer] P_QuestMapUid for Map;
Per_LocalTokensCollected[G_QuestId] = [];
Pe_LocalTokensIds[G_QuestId] = [];
Pe_PlayerPos[G_QuestId] = [];
P_PlayerRaceStartTime[G_QuestId] = 0;
P_QuestMapUid[G_QuestId] = "";
}
// Sort the players list according to the current sorting preference (default to first completion date)
SPlayer[] sortedPlayersList(SPlayer[] playersList, Integer nbPlayers) {
declare SPlayer[] sortedPlayersList = playersList;
declare SPlayer tempPlayer;
declare Integer j;
switch (CurSorting) {
case SortOpt_FirstCompletionDate:
{
if (PlayersSortedByFirstDate.count == 0) PlayersSortedByFirstDate = playersList;
return playersList;
}
case SortOpt_PersonalBest:
{
if (PlayersSortedByBestTime.count != 0) return PlayersSortedByBestTime;
}
case SortOpt_FirstCompletionTime:
{
if (PlayersSortedByFirstTime.count != 0) return PlayersSortedByFirstTime;
}
}
for(i, 0, nbPlayers - 1) {
tempPlayer = sortedPlayersList[i];
j = i;
switch (CurSorting) {
case SortOpt_PersonalBest:
{
while (j > 0 && sortedPlayersList[j - 1].I_bestTime > tempPlayer.I_bestTime) {
sortedPlayersList[j] = sortedPlayersList[j - 1];
j -= 1;
}
}
case SortOpt_FirstCompletionTime:
{
while (j > 0 && sortedPlayersList[j - 1].I_firstTime > tempPlayer.I_firstTime) {
sortedPlayersList[j] = sortedPlayersList[j - 1];
j -= 1;
}
}
}
sortedPlayersList[j] = tempPlayer;
}
return sortedPlayersList;
}
// Display players list in the board (players who completed the quest)
/* Param SPlayer[] playersList = List of players logins and nicknames
Param Text QuestShortDesc = Short description of the quest displayed alongside players list
Param Text QuestTitleList = Sentence displayed above non empty players list
Param Text QuestTitleEmptyList = Sentence displayed above an empty players list
*/
Void displayPlayers(SPlayer[] playersList, Text QuestShortDesc, Text QuestTitleList, Text QuestTitleEmptyList) {
declare Integer StartPos = (CurPage * MaxNumPerPage) - MaxNumPerPage;
declare Integer MaxLimit = (CurPage * MaxNumPerPage) - 1;
declare Integer nbPlayers = playersList.count;
declare Text PluralPlayers = "player";
declare Text OrderedBy;
declare SPlayer[] sortedPlayersList;
sortedPlayersList = sortedPlayersList(playersList, nbPlayers);
Message.SetText(QuestShortDesc ^ "$z\n\n");
if (nbPlayers > 1) PluralPlayers = "players";
if (nbPlayers >= 1) {
ChangeSorting.Visible = True;
declare Text QuestTitleToDisplay = TextLib::Replace(QuestTitleList, "[count]", nbPlayers ^ " " ^ PluralPlayers);
Message.SetText(Message.Value ^ QuestTitleToDisplay ^ "$z\n");
switch (CurSorting) {
case SortOpt_FirstCompletionDate:
{
OrderedBy = "first completion date";
}
case SortOpt_PersonalBest:
{
OrderedBy = "best completion time";
}
case SortOpt_FirstCompletionTime:
{
OrderedBy = "first completion time";
}
}
if (CurPage == 1) Message.SetText(Message.Value ^ "$n$fffNote: This list is cached 10 minutes, ordered by " ^ OrderedBy ^ ".$z\n");
} else {
Message.SetText(Message.Value ^ QuestTitleEmptyList ^ "$z");
}
for(i, StartPos, MaxLimit)
{
if (i > (nbPlayers - 1))
{
Right.Visible = False;
break;
}
if (sortedPlayersList.existskey(i))
{
declare Text RankDisplay = TextLib::ToText(i+1);
if (i < 9) RankDisplay = "0" ^ TextLib::ToText(i+1);
Message.SetText(Message.Value ^ "$fff#" ^ RankDisplay ^ ":$g\t" ^ sortedPlayersList[i].nickname ^ "$z $fff($n" ^ sortedPlayersList[i].login ^ "$m) in $n" ^ sortedPlayersList[i].firstTime ^ "$m (PB: $n" ^ sortedPlayersList[i].bestTime ^ "$m)\n");
}
}
}
// Log tokens when the quest has started and at each step/token collected
// Also returns the player positions list at each token as csv
Text LogMissingTokens()
{
declare persistent Boolean[][Integer] Per_LocalTokensCollected for Map;
declare persistent Text[][Integer] Pe_LocalTokensIds for Map;
declare persistent Text[][Integer] Pe_PlayerPos for Map;
declare persistent Boolean DebugMode for Map;
declare Text LogMsg = "Quest " ^ TextLib::ToText(G_QuestId) ^ " - Tokens missing: ";
declare Text Pe_PlayerPosString = "";
declare Integer NbMissingTokens = 0;
declare Integer NbTotalTokens = Per_LocalTokensCollected[G_QuestId].count;
for(i,0,NbTotalTokens-1)
{
Pe_PlayerPosString = Pe_PlayerPosString ^ Pe_LocalTokensIds[G_QuestId][i] ^ "::" ^ Pe_PlayerPos[G_QuestId][i] ^ "||";
if (!Per_LocalTokensCollected[G_QuestId][i]) {
if (DebugMode) LogMsg = LogMsg ^ Pe_LocalTokensIds[G_QuestId][i] ^ ", ";
NbMissingTokens += 1;
}
}
if (DebugMode)
{
LogMsg = LogMsg ^ " | " ^ NbMissingTokens ^ "/" ^ NbTotalTokens;
log(LogMsg);
}
return Pe_PlayerPosString;
}
// Main script
main()
{
// Local storage
declare persistent Boolean[][Integer] Per_LocalTokensCollected for Map;
declare persistent Text[][Integer] Pe_LocalTokensIds for Map;
declare persistent Text[][Integer] Pe_PlayerPos for Map;
declare persistent Integer[Integer] P_PlayerRaceStartTime for Map;
declare persistent Text[Integer] P_QuestMapUid for Map;
declare persistent Boolean DebugMode for Map;
G_QuestId = '.htmlentities($QuestId, ENT_XML1).';
// Debug mode for the author of the map only
if (!DebugMode && LocalUser.Login == "'.htmlentities($QuestInfo["author_login"], ENT_XML1).'") {
DebugMode = True;
}
HideGui = "'.htmlentities($HideGui, ENT_XML1).'";
if (DebugMode) log("---- Hide gui = [" ^ HideGui ^ "] ---- Debug Mode = [" ^ TextLib::ToText(DebugMode) ^ "]");
// Entering the main script if the user exists and is not spectating
if (GUIPlayer != Null && !IsSpectatorClient)
{
Window = (Page.GetFirstChild("Window") as CMlFrame);
Title = (Page.GetFirstChild("Title") as CMlLabel);
Message = (Page.GetFirstChild("Message") as CMlLabel);
Window.Visible = False;
declare Text State = "'.htmlentities($State, ENT_XML1).'";
switch (State)
{
case "board":
{
// Board Request Processing - Display the quest window
// Get window labels and start to show & config it
Window.Visible = True;
Left = (Page.GetFirstChild("Left") as CMlLabel);
Right = (Page.GetFirstChild("Right") as CMlLabel);
declare CMlLabel StartQuest = (Page.GetFirstChild("StartQuest") as CMlLabel);
ChangeSorting = (Page.GetFirstChild("ChangeSorting") as CMlLabel);
CurSorting = SortOpt_FirstCompletionDate;
declare Boolean HideStartButton = '.htmlentities($SimpleBoard, ENT_XML1).';
Title.SetText("'.htmlentities($QuestInfo["title"], ENT_XML1).'");
// Requesting quest info and players who completed it
if (DebugMode) log("Retrieving quest " ^ TextLib::ToText(G_QuestId) ^ " info...");
declare CHttpRequest request;
request = Http.CreateGet("'.$urlToRequest.'GetBoard.php?QuestId=" ^ TextLib::ToText(G_QuestId) ^ "&" ^ Now);
wait(request.IsCompleted);
// Assigning results
declare SBoardJsonResponse Response;
Response.fromjson(request.Result);
declare SPlayer[] Parts = Response.Players;
declare Text QuestShortDesc = Response.QuestShortDesc;
declare Text QuestFullDesc = Response.QuestFullDesc;
declare Text QuestTitleList = Response.QuestTitleList;
declare Text QuestTitleEmptyList = Response.QuestTitleEmptyList;
// Display parameters
MaxNumPerPage = 10;
NumOfPages = 0;
CurPage = 1;
NumOfPages = MathLib::CeilingInteger(Parts.count / (MaxNumPerPage + 0.0));
if (!HideStartButton) {
StartQuest.Visible = True;
StartQuest.SetText("Start Quest");
}
Left.Visible = False;
displayPlayers(Parts, QuestShortDesc, QuestTitleList, QuestTitleEmptyList); // Display players list who completed the quest
// Start a loop waiting for the quest to either be started of the quest window to be closed
declare Boolean Loopme = True;
while(Loopme)
{
foreach (Event in PendingEvents)
{
if (Event.Type == CMlScriptEvent::Type::MouseClick)
{
if(Event.ControlId=="StartQuest")
{
if(StartQuest.Value == "Start Quest")
{
// Starting the quest
StartQuest.SetText("OK");
Left.Hide();
Right.Hide();
clearPersistentData(); // Reset any persistent data that would remain
if (P_QuestMapUid[G_QuestId] == "") P_QuestMapUid[G_QuestId] = "'.htmlentities($QuestInfo["map_uid"], ENT_XML1).'";
if (DebugMode) log("MapUid for quest " ^ TextLib::ToText(G_QuestId) ^ " set to: " ^ P_QuestMapUid[G_QuestId]);
P_PlayerRaceStartTime[G_QuestId] = GUIPlayer.'.$startTimeVar.';
// Check if the MapUid matches
if (P_QuestMapUid[G_QuestId] != Map.MapInfo.MapUid) {
Message.SetText("$b00Sorry, an error occured preventing you from starting the quest.\nThis is due to a map version mismatch.\nYou may want to update the map to do this!");
if (DebugMode) log("MapUid mismatch. Current MapUid = " ^ Map.MapInfo.MapUid);
} else {
// MapUid matches, requesting quest tokens list
if (DebugMode) log("Retrieving quest " ^ TextLib::ToText(G_QuestId) ^ " tokens...");
request = Http.CreateGet("'.$urlToRequest.'GetTokens.php?QuestId=" ^ TextLib::ToText(G_QuestId) ^ "&" ^ Now);
wait(request.IsCompleted);
declare STokensJsonResponse Response;
Response.fromjson(request.Result);
declare SToken[] Parts = Response.Tokens;
for(n,0,Parts.count-1)
{
Per_LocalTokensCollected[G_QuestId].add(False);
Pe_LocalTokensIds[G_QuestId].add(TextLib::ToText(Parts[n].id));
Pe_PlayerPos[G_QuestId].add("");
}
// Add quest info only if DisplayGUI = yes
if(HideGui == "yes")
{
declare CMlQuad QuadGui = (Page.GetFirstChild("Background") as CMlQuad);
QuadGui.Hide();
QuadGui = (Page.GetFirstChild("TitleBackground") as CMlQuad);
QuadGui.Hide();
declare CMlLabel LabelGui = (Page.GetFirstChild("Close") as CMlLabel);
LabelGui.Hide();
LabelGui = (Page.GetFirstChild("Title") as CMlLabel);
LabelGui.Hide();
Message.SetText("");
}
else
{
Message.SetText(QuestFullDesc ^ "$z\n\n$fffHave fun!\n\n$nNote: You need to complete the quest in the same race from where you started it. If you restart (e.g. by pressing del), you\'ll need to start the quest again. Several quests can be done in the same race.$z");
}
// Log all tokens for quest if debug activated
if (DebugMode)
{
log("Quest " ^ TextLib::ToText(G_QuestId) ^ " started.");
LogMissingTokens();
}
}
break;
}
else
{
declare CMlFrame Window = (Page.GetFirstChild("Window") as CMlFrame);
Window.Visible = False;
Loopme = False;
break;
}
}
else if(Event.ControlId=="Left")
{
Left.Visible = True;
Right.Visible = True;
CurPage -= 1;
if (CurPage == 1) Left.Visible = False;
displayPlayers(Parts, QuestShortDesc, QuestTitleList, QuestTitleEmptyList);
}
else if(Event.ControlId=="Right")
{
Left.Visible = True;
Right.Visible = True;
CurPage += 1;
if (CurPage == NumOfPages) Right.Visible = False;
displayPlayers(Parts, QuestShortDesc, QuestTitleList, QuestTitleEmptyList);
}
else if(Event.ControlId=="ChangeSorting")
{
if (CurSorting == SortOpt_FirstCompletionDate) {
CurSorting = SortOpt_PersonalBest;
ChangeSorting.SetText("$nSort by 1st ⏱$z");
} else if (CurSorting == SortOpt_PersonalBest) {
CurSorting = SortOpt_FirstCompletionTime;
ChangeSorting.SetText("$nSort by 1st $z");
} else {
CurSorting = SortOpt_FirstCompletionDate;
ChangeSorting.SetText("$nSort by PB ⏱$z");
}
displayPlayers(Parts, QuestShortDesc, QuestTitleList, QuestTitleEmptyList);
}
else if(Event.ControlId=="Close")
{
declare CMlFrame Window = (Page.GetFirstChild("Window") as CMlFrame);
Window.Visible = False;
Loopme = False;
}
}
}
yield;
}
}
case "start":
{
// Start Request Processing - Start a quest without any board display in the first place
clearPersistentData(); // Reset any persistent data that would remain for the quest
if (P_QuestMapUid[G_QuestId] == "") P_QuestMapUid[G_QuestId] = "'.htmlentities($QuestInfo["map_uid"], ENT_XML1).'";
if (DebugMode) log("MapUid for quest " ^ TextLib::ToText(G_QuestId) ^ " set to: " ^ P_QuestMapUid[G_QuestId]);
P_PlayerRaceStartTime[G_QuestId] = GUIPlayer.'.$startTimeVar.';
// Check if the MapUid matches
if (P_QuestMapUid[G_QuestId] != Map.MapInfo.MapUid) {
Window.Visible = True;
Message.SetText("$b00Sorry, an error occured preventing you from starting the quest.\nThis is due to a map version mismatch.\nYou may want to update the map to do this!\nPress restart to continue.");
if (DebugMode) log("MapUid mismatch. Current MapUid = " ^ Map.MapInfo.MapUid);
} else {
// MapUid matches, requesting quest tokens list
if (DebugMode) log("Retrieving quest " ^ TextLib::ToText(G_QuestId) ^ " tokens...");
declare CHttpRequest request;
request = Http.CreateGet("'.$urlToRequest.'GetTokens.php?QuestId=" ^ TextLib::ToText(G_QuestId) ^ "&" ^ Now);
wait(request.IsCompleted);
declare STokensJsonResponse Response;
Response.fromjson(request.Result);
declare SToken[] Parts = Response.Tokens;
for(n,0,Parts.count-1)
{
Per_LocalTokensCollected[G_QuestId].add(False);
Pe_LocalTokensIds[G_QuestId].add(TextLib::ToText(Parts[n].id));
Pe_PlayerPos[G_QuestId].add("");
}
// Log all tokens for quest if debug activated
if (DebugMode)
{
log("Quest " ^ TextLib::ToText(G_QuestId) ^ " started.");
LogMissingTokens();
}
}
}
case "removetoken":
{
// Remove token Request Processing - Token triggered, we set it as collected
// If a call is made without the quest started, we stop there
if (!Per_LocalTokensCollected.existskey(G_QuestId) || Pe_LocalTokensIds[G_QuestId].count == 0) return;
// Check we\'re in the same race than when the quest started
if (P_PlayerRaceStartTime[G_QuestId] != GUIPlayer.'.$startTimeVar.') {
if (DebugMode) log("RaceStartTime mismatch: " ^ TextLib::ToText(P_PlayerRaceStartTime[G_QuestId]) ^ " / " ^ GUIPlayer.'.$startTimeVar.');
clearPersistentData();
return;
}
'.$scriptSMObstaclePreventJump.'
declare Text RemoveToken = "'.htmlentities($Token, ENT_XML1).'";
// We set the right token state as collected (True)
for(i,0,Pe_LocalTokensIds[G_QuestId].count-1)
{
if (TextLib::ToLowerCase(RemoveToken) == TextLib::ToLowerCase(Pe_LocalTokensIds[G_QuestId][i]))
{
if (Per_LocalTokensCollected[G_QuestId][i]) {
if (DebugMode) log("Quest " ^ TextLib::ToText(G_QuestId) ^ " - Token " ^ Pe_LocalTokensIds[G_QuestId][i] ^ " already collected, skipping - Position: " ^ TextLib::ToText(GUIPlayer.Position));
break;
}
if (DebugMode) log("Quest " ^ TextLib::ToText(G_QuestId) ^ " - Token " ^ Pe_LocalTokensIds[G_QuestId][i] ^ " collected - Position: " ^ TextLib::ToText(GUIPlayer.Position));
Per_LocalTokensCollected[G_QuestId][i] = True;
Pe_PlayerPos[G_QuestId][i] = TextLib::ToText(GUIPlayer.Position);
break;
}
}
// If all the tokens are collected we submit the completion to the server for processing
if (TokensAreCollected())
{
// Get the player positions list at each token as csv
// Log all tokens for quest if debug activated
declare Text Pe_PlayerPosString = LogMissingTokens();
// Get quest completion time
declare Integer QuestCompletionTime = GameTime - P_PlayerRaceStartTime[G_QuestId];
declare CHttpRequest request;
if (DebugMode) log("Quest " ^ TextLib::ToText(G_QuestId) ^ " completed. Processing...");
request = Http.CreateGet("'.$urlToRequest.'UpdateQuest.php?QuestId=" ^ TextLib::ToText(G_QuestId) ^ "&Login=" ^ LocalUser.Login ^ "&UserName=" ^ TextLib::URLEncode(LocalUser.Name) ^ "&Positions=" ^ TextLib::URLEncode(Pe_PlayerPosString) ^ "&MapUid=" ^ P_QuestMapUid[G_QuestId] ^ "&Time=" ^ TextLib::ToText(QuestCompletionTime) ^ "&" ^ Now);
clearPersistentData();
wait(request.IsCompleted);
if (DebugMode) {
log("All tokens collected.");
}
// Only log visible to anyone that returns what happened, can be useful
log(request.Result);
} else if (DebugMode) {
LogMissingTokens();
}
}
}
}
else
{
declare CMlFrame Window = (Page.GetFirstChild("Window") as CMlFrame);
Window.Visible = False;
if (DebugMode) log("IsSpectatorClient: " ^ TextLib::ToText(IsSpectatorClient));
}
}
--></script>
</manialink>';
} else {
$client = strtolower($_SERVER['HTTP_USER_AGENT']);
echo '
<manialink version="3">
<script><!--
main()
{
log("Client id: " ^ "'.htmlentities($client, ENT_XML1).'" ^ " does not match");
}
--></script>
</manialink>';
}