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