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