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