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