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