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