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