Merge branch 'master' into terencehill/string_prefixes_cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / getreplies.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4         #include "../../dpdefs/progsdefs.qh"
5     #include "../../dpdefs/dpextensions.qh"
6     #include "../../common/constants.qh"
7     #include "../../common/util.qh"
8     #include "../../common/counting.qh"
9     #include "../../common/monsters/monsters.qh"
10     #include "../defs.qh"
11     #include "../../common/mapinfo.qh"
12     #include "getreplies.qh"
13     #include "../race.qh"
14 #endif
15
16 // =========================================================
17 //  Reply messages for common commands, re-worked by Samual
18 //  Last updated: December 30th, 2011
19 // =========================================================
20
21 // These strings are set usually during init in g_world.qc,
22 // or also by some game modes or other functions manually,
23 // and their purpose is to output information to clients
24 // without using any extra processing time.
25
26 // See common.qc for their proper commands
27
28 string getrecords(float page) // 50 records per page
29 {
30         float rec = 0, r, i;
31         string h, s;
32
33         s = "";
34
35         if (g_ctf)
36         {
37                 for (i = page * 200; i < MapInfo_count && i < page * 200 + 200; ++i)
38                 {
39                         if (MapInfo_Get_ByID(i))
40                         {
41                                 r = stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, "/captimerecord/time")));
42
43                                 if (!r)
44                                         continue;
45
46                                 // TODO: uid2name
47                                 h = db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, "/captimerecord/netname"));
48                                 s = strcat(s, strpad(32, MapInfo_Map_bspname), " ", strpad(-6, ftos_decimals(r, 2)), " ", h, "\n");
49                                 ++rec;
50                         }
51                 }
52         }
53
54         if (g_race)
55         {
56                 for (i = page * 200; i < MapInfo_count && i < page * 200 + 200; ++i)
57                 {
58                         if (MapInfo_Get_ByID(i))
59                         {
60                                 r = race_readTime(MapInfo_Map_bspname, 1);
61
62                                 if (!r)
63                                         continue;
64
65                                 h = race_readName(MapInfo_Map_bspname, 1);
66                                 s = strcat(s, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
67                                 ++rec;
68                         }
69                 }
70         }
71
72         if (g_cts)
73         {
74                 for (i = page * 200; i < MapInfo_count && i < page * 200 + 200; ++i)
75                 {
76                         if (MapInfo_Get_ByID(i))
77                         {
78                                 r = race_readTime(MapInfo_Map_bspname, 1);
79
80                                 if (!r)
81                                         continue;
82
83                                 h = race_readName(MapInfo_Map_bspname, 1);
84                                 s = strcat(s, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
85                                 ++rec;
86                         }
87                 }
88         }
89
90         MapInfo_ClearTemps();
91
92         if(s == "" && page == 0)
93                 return "No records are available on this server.\n";
94         else
95                 return s;
96 }
97
98 string getrankings()
99 {
100         float t, i;
101         string n, s, p, map;
102
103         map = GetMapname();
104
105         s = "";
106         for (i = 1; i <= RANKINGS_CNT; ++i)
107         {
108                 t = race_readTime(map, i);
109
110                 if (t == 0)
111                         continue;
112
113                 n = race_readName(map, i);
114                 p = count_ordinal(i);
115                 s = strcat(s, strpad(8, p), " ", strpad(-8, TIME_ENCODED_TOSTRING(t)), " ", n, "\n");
116         }
117
118         MapInfo_ClearTemps();
119
120         if (s == "")
121                 return strcat("No records are available for the map: ", map, "\n");
122         else
123                 return strcat("Records for ", map, ":\n", s);
124 }
125
126 string getladder()
127 {
128         int i, j, k, uidcnt = 0, thiscnt;
129         string s, temp_s, rr, myuid, thisuid;
130
131         rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
132
133         for(k = 0; k < MapInfo_count; ++k)
134         {
135                 if(MapInfo_Get_ByID(k))
136                 {
137                         for(i = 0; i <= LADDER_CNT; ++i) // i = 0 because it is the speed award
138                         {
139                                 if(i == 0) // speed award
140                                 {
141                                         if(stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, rr, "speed/speed"))) == 0)
142                                                 continue;
143
144                                         myuid = db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, rr, "speed/crypto_idfp"));
145                                 }
146                                 else // normal record, if it exists (else break)
147                                 {
148                                         if(race_readTime(MapInfo_Map_bspname, i) == 0)
149                                                 continue;
150
151                                         myuid = race_readUID(MapInfo_Map_bspname, i);
152                                 }
153
154                                 // string s contains:
155                                 // arg 0 = # of speed recs
156                                 // arg 1 = # of 1st place recs
157                                 // arg 2 = # of 2nd place recs
158                                 // ... etc
159                                 // LADDER_CNT+1 = total points
160
161                                 temp_s = db_get(TemporaryDB, strcat("ladder", myuid));
162
163                                 if(temp_s == "")
164                                 {
165                                         db_put(TemporaryDB, strcat("uid", ftos(uidcnt)), myuid);
166                                         ++uidcnt;
167
168                                         for(j = 0; j <= LADDER_CNT + 1; ++j)
169                                         {
170                                                 if(j != LADDER_CNT + 1)
171                                                         temp_s = strcat(temp_s, "0 ");
172                                                 else
173                                                         temp_s = strcat(temp_s, "0");
174                                         }
175                                 }
176
177                                 tokenize_console(temp_s);
178                                 s = "";
179
180                                 if(i == 0) // speed award
181                                 {
182                                         for(j = 0; j <= LADDER_CNT; ++j) // loop over each arg in the string
183                                         {
184                                                 if(j == 0) // speed award
185                                                         s = strcat(s, ftos(stof(argv(j)) +1)); // add 1 to speed rec count and write
186                                                 else
187                                                         s = strcat(s, " ", argv(j)); // just copy over everything else
188                                         }
189                                 }
190                                 else // record
191                                 {
192                                         for(j = 0; j <= LADDER_CNT; ++j) // loop over each arg in the string
193                                         {
194                                                 if(j == 0)
195                                                         s = strcat(s, argv(j)); // speed award, dont prefix with " "
196                                                 else if(j == i) // wanted rec!
197                                                         s = strcat(s, " ", ftos(stof(argv(j)) +1)); // update argv(j)
198                                                 else
199                                                         s = strcat(s, " ", argv(j)); // just copy over everything else
200                                         }
201                                 }
202
203                                 // total points are (by default) calculated like this:
204                                 // speedrec = floor(100 / 10) = 10 points
205                                 // 1st place = floor(100 / 1) = 100 points
206                                 // 2nd place = floor(100 / 2) = 50 points
207                                 // 3rd place = floor(100 / 3) = 33 points
208                                 // 4th place = floor(100 / 4) = 25 points
209                                 // 5th place = floor(100 / 5) = 20 points
210                                 // ... etc
211
212                                 if(i == 0)
213                                         s = strcat(s, " ", ftos(stof(argv(LADDER_CNT+1)) + LADDER_FIRSTPOINT / 10)); // speed award, add LADDER_FIRSTPOINT / 10 points
214                                 else
215                                         s = strcat(s, " ", ftos(stof(argv(LADDER_CNT+1)) + floor(LADDER_FIRSTPOINT / i))); // record, add LADDER_FIRSTPOINT / i points
216
217                                 db_put(TemporaryDB, strcat("ladder", myuid), s);
218                         }
219                 }
220         }
221
222         for(i = 0; i <= uidcnt; ++i) // for each known uid
223         {
224                 thisuid = db_get(TemporaryDB, strcat("uid", ftos(i)));
225                 temp_s = db_get(TemporaryDB, strcat("ladder", thisuid));
226                 tokenize_console(temp_s);
227                 thiscnt = stof(argv(LADDER_CNT+1));
228
229                 if(thiscnt > top_scores[LADDER_SIZE-1])
230                 {
231                         for(j = 0; j < LADDER_SIZE; ++j) // for each place in ladder
232                         {
233                                 if(thiscnt > top_scores[j])
234                                 {
235                                         for(k = LADDER_SIZE-1; k >= j; --k)
236                                         {
237                                                 top_uids[k] = top_uids[k-1];
238                                                 top_scores[k] = top_scores[k-1];
239                                         }
240
241                                         top_uids[j] = thisuid;
242                                         top_scores[j] = thiscnt;
243                                         break;
244                                 }
245                         }
246                 }
247         }
248
249         s = "^3-----------------------\n\n";
250
251         s = strcat(s, "Pos ^3|");
252         s = strcat(s, " ^7Total  ^3|");
253
254         for(i = 1; i <= LADDER_CNT; ++i)
255                 { s = strcat(s, " ^7", count_ordinal(i), " ^3|"); }
256
257         s = strcat(s, " ^7Speed awards ^3| ^7Name");
258         s = strcat(s, "\n^3----+--------");
259
260         for(i = 1; i <= min(9, LADDER_CNT); ++i)
261                 { s = strcat(s, "+-----"); }
262
263         #if LADDER_CNT > 9
264         for(i = 1; i <= LADDER_CNT - 9; ++i)
265                 { s = strcat(s, "+------"); }
266         #endif
267
268         s = strcat(s, "+--------------+--------------------\n");
269
270         for(i = 0; i < LADDER_SIZE; ++i)
271         {
272                 temp_s = db_get(TemporaryDB, strcat("ladder", top_uids[i]));
273                 tokenize_console(temp_s);
274
275                 if(argv(LADDER_CNT+1) == "") // total is 0, skip
276                         continue;
277
278                 s = strcat(s, strpad(4, count_ordinal(i+1)), "^3| ^7"); // pos
279                 s = strcat(s, strpad(7, argv(LADDER_CNT+1)), "^3| ^7"); // total
280
281                 for(j = 1; j <= min(9, LADDER_CNT); ++j)
282                         { s = strcat(s, strpad(4, argv(j)), "^3| ^7"); } // 1st, 2nd, 3rd etc cnt
283
284                 #if LADDER_CNT > 9
285                 for(j = 10; j <= LADDER_CNT; ++j)
286                         { s = strcat(s, strpad(4, argv(j)), " ^3| ^7"); } // 1st, 2nd, 3rd etc cnt
287                 #endif
288
289                 s = strcat(s, strpad(13, argv(0)), "^3| ^7"); // speed award cnt
290                 s = strcat(s, uid2name(top_uids[i]), "\n"); // name
291         }
292
293         MapInfo_ClearTemps();
294
295         if(s == "")
296                 return "No ladder on this server!\n";
297         else
298                 return strcat("Top ", ftos(LADDER_SIZE), " ladder rankings:\n", s);
299 }
300
301 string getmaplist()
302 {
303         string maplist = "", col;
304         int i, argc;
305
306         argc = tokenize_console(autocvar_g_maplist);
307         for(i = 0; i < argc; ++i)
308         {
309                 if(MapInfo_CheckMap(argv(i)))
310                 {
311                         if(i % 2) { col = "^2"; }
312                         else { col = "^3"; }
313                         maplist = sprintf("%s%s%s ", maplist, col, argv(i));
314                 }
315         }
316
317         MapInfo_ClearTemps();
318         return sprintf("^7Maps in list: %s\n", maplist);
319 }
320
321
322 string getlsmaps()
323 {
324         string lsmaps = "", col;
325         float i, newmaps = 0;
326
327         for(i = 0; i < MapInfo_count; ++i)
328         {
329                 if((MapInfo_Get_ByID(i)) && !(MapInfo_Map_flags & MapInfo_ForbiddenFlags()))
330                 {
331                         // todo: Check by play count of maps for other game types?
332                         if(
333                                 (g_race && !stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, RACE_RECORD, "time"))))
334                                 ||
335                                 (g_cts && !stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, CTS_RECORD, "time"))))
336                         )
337                         {
338                                 newmaps = true;
339                                 if(i % 2) { col = "^4*"; }
340                                 else { col = "^5*"; }
341                         }
342                         else
343                         {
344                                 if(i % 2) { col = "^2"; }
345                                 else { col = "^3"; }
346                         }
347
348                         lsmaps = sprintf("%s%s%s ", lsmaps, col, MapInfo_Map_bspname);
349                 }
350         }
351
352         MapInfo_ClearTemps();
353         return sprintf("^7Maps available (%d)%s: %s\n", tokenize_console(lsmaps), (newmaps ? " (New maps have asterisks marked in blue)" : ""), lsmaps);
354 }
355
356 string getmonsterlist()
357 {
358         string monsterlist = "", col;
359
360         for(int i = MON_FIRST; i <= MON_LAST; ++i)
361         {
362                 if(i % 2) { col = "^2"; }
363                 else { col = "^3"; }
364                 monsterlist = sprintf("%s%s%s ", monsterlist, col, (get_monsterinfo(i)).netname);
365         }
366
367         return sprintf("^7Monsters available: %s\n", monsterlist);
368 }