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
|
/*
Menu with the top global times for a map course and mode.
*/
static bool cameFromLocalRanks[MAXPLAYERS + 1];
static char mapTopMap[MAXPLAYERS + 1][64];
static int mapTopCourse[MAXPLAYERS + 1];
static int mapTopMode[MAXPLAYERS + 1];
// =====[ PUBLIC ]=====
void DisplayMapTopModeMenu(int client, const char[] map, int course)
{
FormatEx(mapTopMap[client], sizeof(mapTopMap[]), map);
mapTopCourse[client] = course;
Menu menu = new Menu(MenuHandler_MapTopModeMenu);
MapTopModeMenuSetTitle(client, menu);
GOKZ_MenuAddModeItems(client, menu, false);
menu.Display(client, MENU_TIME_FOREVER);
}
void DisplayMapTopMenu(int client, const char[] map, int course, int mode)
{
FormatEx(mapTopMap[client], sizeof(mapTopMap[]), map);
mapTopCourse[client] = course;
mapTopMode[client] = mode;
Menu menu = new Menu(MenuHandler_MapTopMenu);
MapTopMenuSetTitle(client, menu);
MapTopMenuAddItems(client, menu);
menu.Display(client, MENU_TIME_FOREVER);
}
void DisplayMapTopSubmenu(int client, const char[] map, int course, int mode, int timeType, bool fromLocalRanks = false)
{
char modeStr[32];
cameFromLocalRanks[client] = fromLocalRanks;
DataPack dp = new DataPack();
dp.WriteCell(GetClientUserId(client));
dp.WriteCell(timeType);
FormatEx(mapTopMap[client], sizeof(mapTopMap[]), map);
mapTopCourse[client] = course;
mapTopMode[client] = mode;
GOKZ_GL_GetModeString(mode, modeStr, sizeof(modeStr));
// TODO Hard coded 128 tick
// TODO Hard coded cap at top 20
// TODO Not true NUB yet
GlobalAPI_GetRecordsTop(DisplayMapTopSubmenuCallback, dp, _, _, _, map, 128, course, modeStr,
timeType == TimeType_Nub ? DEFAULT_BOOL : false, _, 0, 20);
}
// =====[ EVENTS ]=====
public int MenuHandler_MapTopModeMenu(Menu menu, MenuAction action, int param1, int param2)
{
if (action == MenuAction_Select)
{
// param1 = client, param2 = mode
DisplayMapTopMenu(param1, mapTopMap[param1], mapTopCourse[param1], param2);
}
else if (action == MenuAction_End)
{
delete menu;
}
return 0;
}
public int MenuHandler_MapTopMenu(Menu menu, MenuAction action, int param1, int param2)
{
if (action == MenuAction_Select)
{
char info[8];
menu.GetItem(param2, info, sizeof(info));
int timeType = StringToInt(info);
DisplayMapTopSubmenu(param1, mapTopMap[param1], mapTopCourse[param1], mapTopMode[param1], timeType);
}
if (action == MenuAction_Cancel && param2 == MenuCancel_Exit)
{
DisplayMapTopModeMenu(param1, mapTopMap[param1], mapTopCourse[param1]);
}
else if (action == MenuAction_End)
{
delete menu;
}
return 0;
}
public int MenuHandler_MapTopSubmenu(Menu menu, MenuAction action, int param1, int param2)
{
if (action == MenuAction_Cancel && param2 == MenuCancel_Exit)
{
if (cameFromLocalRanks[param1])
{
GOKZ_LR_ReopenMapTopMenu(param1);
}
else
{
DisplayMapTopMenu(param1, mapTopMap[param1], mapTopCourse[param1], mapTopMode[param1]);
}
}
if (action == MenuAction_End)
{
delete menu;
}
return 0;
}
// =====[ PRIVATE ]=====
static void MapTopModeMenuSetTitle(int client, Menu menu)
{
if (mapTopCourse[client] == 0)
{
menu.SetTitle("%T", "Global Map Top Mode Menu - Title", client, mapTopMap[client]);
}
else
{
menu.SetTitle("%T", "Global Map Top Mode Menu - Title (Bonus)", client, mapTopMap[client], mapTopCourse[client]);
}
}
static void MapTopMenuSetTitle(int client, Menu menu)
{
if (mapTopCourse[client] == 0)
{
menu.SetTitle("%T", "Global Map Top Menu - Title", client, mapTopMap[client], gC_ModeNames[mapTopMode[client]]);
}
else
{
menu.SetTitle("%T", "Global Map Top Menu - Title (Bonus)", client, mapTopMap[client], mapTopCourse[client], gC_ModeNames[mapTopMode[client]]);
}
}
static void MapTopMenuAddItems(int client, Menu menu)
{
char display[32];
for (int i = 0; i < TIMETYPE_COUNT; i++)
{
FormatEx(display, sizeof(display), "%T", "Global Map Top Menu - Top", client, gC_TimeTypeNames[i]);
menu.AddItem(IntToStringEx(i), display);
}
}
public int DisplayMapTopSubmenuCallback(JSON_Object top, GlobalAPIRequestData request, DataPack dp)
{
dp.Reset();
int client = GetClientOfUserId(dp.ReadCell());
int timeType = dp.ReadCell();
delete dp;
if (request.Failure)
{
LogError("Failed to get top records with Global API.");
return 0;
}
if (!top.IsArray)
{
LogError("GlobalAPI returned a malformed response while looking up the top records.");
return 0;
}
if (!IsValidClient(client))
{
return 0;
}
Menu menu = new Menu(MenuHandler_MapTopSubmenu);
if (mapTopCourse[client] == 0)
{
menu.SetTitle("%T", "Global Map Top Submenu - Title", client,
gC_TimeTypeNames[timeType], mapTopMap[client], gC_ModeNames[mapTopMode[client]]);
}
else
{
menu.SetTitle("%T", "Global Map Top Submenu - Title (Bonus)", client,
gC_TimeTypeNames[timeType], mapTopMap[client], mapTopCourse[client], gC_ModeNames[mapTopMode[client]]);
}
if (MapTopSubmenuAddItems(menu, top, timeType) == 0)
{ // If no records found
if (timeType == TimeType_Pro)
{
GOKZ_PrintToChat(client, true, "%t", "No Global Times Found (PRO)");
}
else
{
GOKZ_PrintToChat(client, true, "%t", "No Global Times Found");
}
if (cameFromLocalRanks[client])
{
GOKZ_LR_ReopenMapTopMenu(client);
}
else
{
DisplayMapTopMenu(client, mapTopMap[client], mapTopCourse[client], mapTopMode[client]);
}
}
else
{
menu.Pagination = 5;
menu.Display(client, MENU_TIME_FOREVER);
}
return 0;
}
// Returns number of record times added to the menu
static int MapTopSubmenuAddItems(Menu menu, JSON_Object records, int timeType)
{
char playerName[MAX_NAME_LENGTH];
char display[128];
for (int i = 0; i < records.Length; i++)
{
APIRecord record = view_as<APIRecord>(records.GetObjectIndexed(i));
record.GetPlayerName(playerName, sizeof(playerName));
switch (timeType)
{
case TimeType_Nub:
{
FormatEx(display, sizeof(display), "#%-2d %11s %3d TP %s",
i + 1, GOKZ_FormatTime(record.Time), record.Teleports, playerName);
}
case TimeType_Pro:
{
FormatEx(display, sizeof(display), "#%-2d %11s %s",
i + 1, GOKZ_FormatTime(record.Time), playerName);
}
}
menu.AddItem("", display, ITEMDRAW_DISABLED);
}
return records.Length;
}
|