]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/statslist.qc
Unnecessary newlines are unnecessary
[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         return sprintf(
45                 "%s %s, %d",
46                 monthname,
47                 count_ordinal(stof(substring(input, 8, 2))),
48                 stof(substring(input, 0, 4))
49         );
50 }
51
52 void XonoticStatsList_getStats(entity me)
53 {
54         LOG_TRACE("XonoticStatsList_getStats() at time: ", ftos(time));
55         // delete the old buffer if it exists
56         if(me.listStats >= 0)
57                 buf_del(me.listStats);
58
59         // create the new buffer if we have a stats buffer
60         if(PS_D_IN_DB >= 0)
61                 me.listStats = buf_create();
62
63         // now confirmation, if we didn't create a buffer then just return now
64         if(me.listStats < 0)
65         {
66                 me.nItems = 0;
67                 return;
68         }
69
70         float order = 0;
71         string e = "", en = "", data = "";
72
73         string outstr = ""; // NOTE: out string MUST use underscores for spaces here, we'll replace them later
74         string orderstr = "";
75
76         float out_total_matches = -1;
77         float out_total_wins = -1;
78         float out_total_losses = -1;
79
80         float out_total_kills = -1;
81         float out_total_deaths = -1;
82
83         for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
84         {
85                 order = 0;
86                 outstr = "";
87                 orderstr = "";
88                 data = db_get(PS_D_IN_DB, sprintf("#%s", e));
89
90                 // non-gamemode specific stuff
91                 switch(e)
92                 {
93                         case "overall/joined_dt":
94                         {
95                                 order = 1;
96                                 outstr = _("Joined:");
97                                 data = XonoticStatsList_convertDate(car(data));
98                                 break;
99                         }
100                         case "overall/last_seen_dt":
101                         {
102                                 order = 1;
103                                 outstr = _("Last_Seen:");
104                                 data = XonoticStatsList_convertDate(car(data));
105                                 break;
106                         }
107                         case "overall/alivetime":
108                         {
109                                 order = 1;
110                                 outstr = _("Time_Played:");
111                                 data = process_time(3, stof(data));
112                                 break;
113                         }
114                         case "overall/favorite-map":
115                         {
116                                 order = 2;
117                                 outstr = _("Favorite_Map:");
118                                 data = car(data);
119                                 break;
120                         }
121                         case "overall/matches":
122                         {
123                                 order = -1;
124                                 out_total_matches = stof(data);
125                                 break;
126                         }
127                         case "overall/wins":
128                         {
129                                 order = -1;
130                                 out_total_wins = stof(data);
131                                 break;
132                         }
133                         case "overall/total-kills":
134                         {
135                                 order = -1;
136                                 out_total_kills = stof(data);
137                                 break;
138                         }
139                         case "overall/total-deaths":
140                         {
141                                 order = -1;
142                                 out_total_deaths = stof(data);
143                                 break;
144                         }
145                 }
146
147                 if((order == -1) && (out_total_matches >= 0) && (out_total_wins >= 0))
148                 {
149                         bufstr_add(me.listStats, sprintf("003Matches: %d", out_total_matches), true);
150
151                         if(out_total_matches > 0) // don't show extra info if there are no matches played
152                         {
153                                 out_total_losses = max(0, (out_total_matches - out_total_wins));
154                                 bufstr_add(me.listStats, sprintf("003Wins/Losses: %d/%d", out_total_wins, out_total_losses), true);
155                                 bufstr_add(me.listStats, sprintf("004Win_Percentage: %d%%", ((out_total_wins / out_total_matches) * 100)), true);
156                         }
157
158                         out_total_matches = -1;
159                         out_total_wins = -1;
160                         out_total_losses = -1;
161                         continue;
162                 }
163
164                 if((order == -1) && (out_total_kills >= 0) && (out_total_deaths >= 0))
165                 {
166                         bufstr_add(me.listStats, sprintf("005Kills/Deaths: %d/%d", out_total_kills, out_total_deaths), true);
167
168                         // if there are no deaths, just show kill count
169                         if(out_total_deaths > 0)
170                                 bufstr_add(me.listStats, sprintf("006Kill_Ratio: %.2f", (out_total_kills / out_total_deaths)), true);
171                         else
172                                 bufstr_add(me.listStats, sprintf("006Kill_Ratio: %.2f", out_total_kills), true);
173
174                         out_total_kills = -1;
175                         out_total_deaths = -1;
176                         continue;
177                 }
178
179                 // game mode specific stuff
180                 if(order > 0)
181                 {
182                         orderstr = sprintf("%03d", order);
183                 }
184                 else
185                 {
186                         float dividerpos = strstrofs(e, "/", 0);
187
188                         string gametype = substring(e, 0, dividerpos);
189                         if(gametype == "overall") { continue; }
190
191                         string event = substring(e, (dividerpos + 1), strlen(e) - (dividerpos + 1));
192
193                         // if we are ranked, read these sets of possible options
194                         if(stof(db_get(PS_D_IN_DB, sprintf("#%s/rank", gametype))))
195                         {
196                                 switch(event)
197                                 {
198                                         case "matches":
199                                         {
200                                                 order = 1;
201                                                 outstr = sprintf(_("%s_Matches:"), strtoupper(gametype));
202                                                 //data = sprintf(_("%d (unranked)"), data);
203                                                 break;
204                                         }
205                                         case "elo":
206                                         {
207                                                 order = 2;
208                                                 outstr = sprintf(_("%s_ELO:"), strtoupper(gametype));
209                                                 data = sprintf("%d", stof(data));
210                                                 break;
211                                         }
212                                         case "rank":
213                                         {
214                                                 order = 3;
215                                                 outstr = sprintf(_("%s_Rank:"), strtoupper(gametype));
216                                                 data = sprintf("%d", stof(data));
217                                                 break;
218                                         }
219                                         case "percentile":
220                                         {
221                                                 order = 4;
222                                                 outstr = sprintf(_("%s_Percentile:"), strtoupper(gametype));
223                                                 data = sprintf("%d%%", stof(data));
224                                                 break;
225                                         }
226
227                                         #if 0
228                                         case "favorite-map":
229                                         {
230                                                 order = 5;
231                                                 outstr = sprintf(_("%s_Favorite_Map:"), strtoupper(gametype));
232                                                 //data = sprintf(_("%d (unranked)"), data);
233                                                 break;
234                                         }
235                                         #endif
236
237                                         default: continue; // nothing to see here
238                                 }
239
240                                 // now set up order for sorting later
241                                 orderstr = sprintf("%2.2s%d", gametype, order);
242                         }
243                         else if(event == "matches")
244                         {
245                                 outstr = sprintf(_("%s_Matches:"), strtoupper(gametype));
246                                 data = sprintf(_("%d (unranked)"), stof(data));
247
248                                 // unranked game modes ALWAYS get put last
249                                 orderstr = "zzz";
250                         }
251                         else { continue; }
252                 }
253
254                 bufstr_add(me.listStats, sprintf("%s%s %s", orderstr, outstr, data), true);
255         }
256
257         me.nItems = buf_getsize(me.listStats);
258         if(me.nItems > 0)
259                 buf_sort(me.listStats, 128, false);
260 }
261
262 void XonoticStatsList_destroy(entity me)
263 {
264         if(me.nItems > 0)
265                 buf_del(me.listStats);
266 }
267
268 void XonoticStatsList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
269 {
270         me.itemAbsSize = '0 0 0';
271         SUPER(XonoticStatsList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
272
273         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
274         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
275         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
276
277 #if 0
278         me.columnNameOrigin = me.realFontSize.x;
279         me.columnNameSize = 0.5 - me.realFontSize.x; // end halfway at maximum length
280         me.columnDataOrigin = me.columnNameOrigin + me.columnNameSize;
281         me.columnDataSize = 1 - me.columnNameSize - me.realFontSize.x; // fill the rest of the control
282 #else
283         me.columnNameOrigin = me.realFontSize.x;
284         me.columnNameSize = 1 - 2 * me.realFontSize.x;
285 #endif
286 }
287
288 void XonoticStatsList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
289 {
290         if(isFocused)
291         {
292                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
293                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
294         }
295
296         string data = bufstr_get(me.listStats, i);
297         string s = car(data);
298         string d = cdr(data);
299
300         s = substring(s, 3, strlen(s) - 3);
301         s = strreplace("_", " ", s);
302         s = draw_TextShortenToWidth(s, 0.5 * me.columnNameSize, 0, me.realFontSize);
303         draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, '1 1 1', SKINALPHA_TEXT, 1);
304
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 }