Rename menu files to be consistent
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / statslist.qc
1 #ifdef INTERFACE
2 CLASS(XonoticStatsList) EXTENDS(XonoticListBox)
3         METHOD(XonoticStatsList, configureXonoticStatsList, void(entity))
4         ATTRIB(XonoticStatsList, rowsPerItem, float, 1.4)
5         METHOD(XonoticStatsList, resizeNotify, void(entity, vector, vector, vector, vector))
6         METHOD(XonoticStatsList, drawListBoxItem, void(entity, float, vector, float))
7         METHOD(XonoticStatsList, getStats, void(entity))
8         METHOD(XonoticStatsList, doubleClickListBoxItem, void(entity, float, vector))
9         METHOD(XonoticStatsList, keyDown, float(entity, float, float, float))
10         METHOD(XonoticStatsList, destroy, void(entity))
11         METHOD(XonoticStatsList, showNotify, void(entity))
12
13         ATTRIB(XonoticStatsList, listStats, float, -1)
14         ATTRIB(XonoticStatsList, realFontSize, vector, '0 0 0')
15         ATTRIB(XonoticStatsList, realUpperMargin, float, 0)
16         ATTRIB(XonoticStatsList, columnNameOrigin, float, 0)
17         ATTRIB(XonoticStatsList, columnNameSize, float, 0)
18 ENDCLASS(XonoticStatsList)
19
20 entity statslist; // for reference elsewhere
21 entity makeXonoticStatsList();
22 #endif
23
24 #ifdef IMPLEMENTATION
25
26 entity makeXonoticStatsList()
27 {
28         entity me;
29         me = spawnXonoticStatsList();
30         me.configureXonoticStatsList(me);
31         return me;
32 }
33
34 void XonoticStatsList_configureXonoticStatsList(entity me)
35 {
36         me.configureXonoticListBox(me);
37         me.getStats(me);
38 }
39
40 string XonoticStatsList_convertDate(string input)
41 {
42         // 2013-12-21
43         // 0123456789
44         if(strlen(input) != 10)
45                 return input;
46
47         string monthname = "";
48
49         switch(stof(substring(input, 5, 2)))
50         {
51                 case 1:  monthname = _("January");    break;
52                 case 2:  monthname = _("February");   break;
53                 case 3:  monthname = _("March");      break;
54                 case 4:  monthname = _("April");      break;
55                 case 5:  monthname = _("May");        break;
56                 case 6:  monthname = _("June");       break;
57                 case 7:  monthname = _("July");       break;
58                 case 8:  monthname = _("August");     break;
59                 case 9:  monthname = _("September");  break;
60                 case 10: monthname = _("October");    break;
61                 case 11: monthname = _("November");   break;
62                 case 12: monthname = _("December");   break;
63                 default: return input; // failed, why?
64         }
65
66         return sprintf(
67                 "%s %s, %d",
68                 monthname,
69                 count_ordinal(stof(substring(input, 8, 2))),
70                 stof(substring(input, 0, 4))
71         );
72 }
73
74 void XonoticStatsList_getStats(entity me)
75 {
76         dprint("XonoticStatsList_getStats() at time: ", ftos(time), "\n");
77         // delete the old buffer if it exists
78         if(me.listStats >= 0)
79                 buf_del(me.listStats);
80
81         // create the new buffer if we have a stats buffer
82         if(PS_D_IN_DB >= 0)
83                 me.listStats = buf_create();
84
85         // now confirmation, if we didn't create a buffer then just return now
86         if(me.listStats < 0)
87         {
88                 me.nItems = 0;
89                 return;
90         }
91
92         float order = 0;
93         string e = "", en = "", data = "";
94
95         string outstr = ""; // NOTE: out string MUST use underscores for spaces here, we'll replace them later
96         string orderstr = "";
97
98         float out_total_matches = -1;
99         float out_total_wins = -1;
100         float out_total_losses = -1;
101
102         float out_total_kills = -1;
103         float out_total_deaths = -1;
104
105         for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
106         {
107                 order = 0;
108                 outstr = "";
109                 orderstr = "";
110                 data = db_get(PS_D_IN_DB, sprintf("#%s", e));
111
112                 // non-gamemode specific stuff
113                 switch(e)
114                 {
115                         case "overall/joined_dt":
116                         {
117                                 order = 1;
118                                 outstr = _("Joined:");
119                                 data = XonoticStatsList_convertDate(car(data));
120                                 break;
121                         }
122                         case "overall/last_seen_dt":
123                         {
124                                 order = 1;
125                                 outstr = _("Last_Seen:");
126                                 data = XonoticStatsList_convertDate(car(data));
127                                 break;
128                         }
129                         case "overall/alivetime":
130                         {
131                                 order = 1;
132                                 outstr = _("Time_Played:");
133                                 data = process_time(3, stof(data));
134                                 break;
135                         }
136                         case "overall/favorite-map":
137                         {
138                                 order = 2;
139                                 outstr = _("Favorite_Map:");
140                                 data = car(data);
141                                 break;
142                         }
143                         case "overall/matches":
144                         {
145                                 order = -1;
146                                 out_total_matches = stof(data);
147                                 break;
148                         }
149                         case "overall/wins":
150                         {
151                                 order = -1;
152                                 out_total_wins = stof(data);
153                                 break;
154                         }
155                         case "overall/total-kills":
156                         {
157                                 order = -1;
158                                 out_total_kills = stof(data);
159                                 break;
160                         }
161                         case "overall/total-deaths":
162                         {
163                                 order = -1;
164                                 out_total_deaths = stof(data);
165                                 break;
166                         }
167                 }
168
169                 if((order == -1) && (out_total_matches >= 0) && (out_total_wins >= 0))
170                 {
171                         bufstr_add(me.listStats, sprintf("003Matches: %d", out_total_matches), TRUE);
172                         
173                         if(out_total_matches > 0) // don't show extra info if there are no matches played
174                         {
175                                 out_total_losses = max(0, (out_total_matches - out_total_wins));
176                                 bufstr_add(me.listStats, sprintf("003Wins/Losses: %d/%d", out_total_wins, out_total_losses), TRUE);
177                                 bufstr_add(me.listStats, sprintf("004Win_Percentage: %d%%", ((out_total_wins / out_total_matches) * 100)), TRUE);
178                         }
179
180                         out_total_matches = -1;
181                         out_total_wins = -1;
182                         out_total_losses = -1;
183                         continue;
184                 }
185
186                 if((order == -1) && (out_total_kills >= 0) && (out_total_deaths >= 0))
187                 {
188                         bufstr_add(me.listStats, sprintf("005Kills/Deaths: %d/%d", out_total_kills, out_total_deaths), TRUE);
189
190                         // if there are no deaths, just show kill count 
191                         if(out_total_deaths > 0)
192                                 bufstr_add(me.listStats, sprintf("006Kill_Ratio: %.2f", (out_total_kills / out_total_deaths)), TRUE);
193                         else
194                                 bufstr_add(me.listStats, sprintf("006Kill_Ratio: %.2f", out_total_kills), TRUE);
195
196                         out_total_kills = -1;
197                         out_total_deaths = -1;
198                         continue;
199                 }
200
201                 // game mode specific stuff
202                 if(order > 0)
203                 {
204                         orderstr = sprintf("%03d", order);
205                 }
206                 else
207                 {
208                         float dividerpos = strstrofs(e, "/", 0);
209                         
210                         string gametype = substring(e, 0, dividerpos);
211                         if(gametype == "overall") { continue; }
212                         
213                         string event = substring(e, (dividerpos + 1), strlen(e) - (dividerpos + 1));
214
215                         // if we are ranked, read these sets of possible options
216                         if(stof(db_get(PS_D_IN_DB, sprintf("#%s/rank", gametype))))
217                         {
218                                 switch(event)
219                                 {
220                                         case "matches":
221                                         {
222                                                 order = 1;
223                                                 outstr = sprintf(_("%s_Matches:"), strtoupper(gametype));
224                                                 //data = sprintf(_("%d (unranked)"), data);
225                                                 break;
226                                         }
227                                         case "elo":
228                                         {
229                                                 order = 2;
230                                                 outstr = sprintf(_("%s_ELO:"), strtoupper(gametype));
231                                                 data = sprintf("%d", stof(data));
232                                                 break;
233                                         }
234                                         case "rank":
235                                         {
236                                                 order = 3;
237                                                 outstr = sprintf(_("%s_Rank:"), strtoupper(gametype));
238                                                 data = sprintf("%d", stof(data));
239                                                 break;
240                                         }
241                                         case "percentile":
242                                         {
243                                                 order = 4;
244                                                 outstr = sprintf(_("%s_Percentile:"), strtoupper(gametype));
245                                                 data = sprintf("%d%%", stof(data));
246                                                 break;
247                                         }
248                                         
249                                         #if 0
250                                         case "favorite-map":
251                                         {
252                                                 order = 5;
253                                                 outstr = sprintf(_("%s_Favorite_Map:"), strtoupper(gametype));
254                                                 //data = sprintf(_("%d (unranked)"), data);
255                                                 break;
256                                         }
257                                         #endif
258                                         
259                                         default: continue; // nothing to see here
260                                 }
261
262                                 // now set up order for sorting later
263                                 orderstr = sprintf("%2.2s%d", gametype, order);
264                         }
265                         else if(event == "matches")
266                         {
267                                 outstr = sprintf(_("%s_Matches:"), strtoupper(gametype));
268                                 data = sprintf(_("%d (unranked)"), stof(data));
269
270                                 // unranked game modes ALWAYS get put last
271                                 orderstr = "zzz";
272                         }
273                         else { continue; }
274                 }
275
276                 bufstr_add(me.listStats, sprintf("%s%s %s", orderstr, outstr, data), TRUE);
277         }
278
279         me.nItems = buf_getsize(me.listStats);
280         if(me.nItems > 0)
281                 buf_sort(me.listStats, 128, FALSE);
282 }
283
284 void XonoticStatsList_destroy(entity me)
285 {
286         if(me.nItems > 0)
287                 buf_del(me.listStats);
288 }
289
290 void XonoticStatsList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
291 {
292         me.itemAbsSize = '0 0 0';
293         SUPER(XonoticStatsList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
294
295         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize_y * me.itemHeight));
296         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize_x * (1 - me.controlWidth)));
297         me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
298
299 #if 0
300         me.columnNameOrigin = me.realFontSize_x;
301         me.columnNameSize = 0.5 - me.realFontSize_x; // end halfway at maximum length
302         me.columnDataOrigin = me.columnNameOrigin + me.columnNameSize;
303         me.columnDataSize = 1 - me.columnNameSize - me.realFontSize_x; // fill the rest of the control
304 #else
305         me.columnNameOrigin = me.realFontSize_x;
306         me.columnNameSize = 1 - 2 * me.realFontSize_x;
307 #endif
308 }
309
310 void XonoticStatsList_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
311 {
312         if(isSelected)
313                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
314
315         string data = bufstr_get(me.listStats, i);
316         string s = car(data);
317         string d = cdr(data);
318         
319         s = substring(s, 3, strlen(s) - 3);
320         s = strreplace("_", " ", s);
321         s = draw_TextShortenToWidth(s, 0.5 * me.columnNameSize, 0, me.realFontSize);
322         draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, '1 1 1', SKINALPHA_TEXT, 1);
323
324         d = draw_TextShortenToWidth(d, me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize), 0, me.realFontSize);
325         draw_Text(me.realUpperMargin * eY + (me.columnNameOrigin + 1 * (me.columnNameSize - draw_TextWidth(d, 0, me.realFontSize))) * eX, d, me.realFontSize, '1 1 1', SKINALPHA_TEXT, 1);
326 }
327
328 void XonoticStatsList_showNotify(entity me)
329 {
330         PlayerStats_PlayerDetail_CheckUpdate();
331 }
332
333 void XonoticStatsList_doubleClickListBoxItem(entity me, float i, vector where)
334 {
335         //DemoConfirm_ListClick_Check_Gamestatus(me);
336 }
337
338 float XonoticStatsList_keyDown(entity me, float scan, float ascii, float shift)
339 {
340         if(scan == K_ENTER || scan == K_KP_ENTER)
341         {
342                 //DemoConfirm_ListClick_Check_Gamestatus(me);
343                 return 1;
344         }
345         else
346         {
347                 return SUPER(XonoticStatsList).keyDown(me, scan, ascii, shift);
348         }
349 }
350 #endif
351