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