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