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