Limit the number of maps lsmaps can show to 250 (better than either increasing tempst...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / getreplies.qc
1 #include "getreplies.qh"
2 #include <common/command/_mod.qh>
3 #include "getreplies.qh"
4
5 #include "../race.qh"
6
7 #include <common/constants.qh>
8 #include <common/net_linked.qh>
9 #include <common/mapinfo.qh>
10 #include <common/util.qh>
11
12 #include <common/monsters/_mod.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(int page)  // 50 records per page
27 {
28         string s = "";
29
30         MUTATOR_CALLHOOK(GetRecords, page, s);
31         s = M_ARGV(1, string);
32
33         MapInfo_ClearTemps();
34
35         if (s == "" && page == 0) return "No records are available on this server.\n";
36         else return s;
37 }
38
39 string getrankings()
40 {
41         float t, i;
42         string n, s, p, map;
43
44         map = GetMapname();
45
46         s = "";
47         for (i = 1; i <= RANKINGS_CNT; ++i)
48         {
49                 t = race_readTime(map, i);
50
51                 if (t == 0) continue;
52
53                 n = race_readName(map, i);
54                 p = count_ordinal(i);
55                 s = strcat(s, strpad(8, p), " ", strpad(-8, TIME_ENCODED_TOSTRING(t)), " ", n, "\n");
56         }
57
58         MapInfo_ClearTemps();
59
60         if (s == "") return strcat("No records are available for the map: ", map, "\n");
61         else return strcat("Records for ", map, ":\n", s);
62 }
63
64 string getladder()
65 {
66         int i, j, k, uidcnt = 0, thiscnt;
67         string s, temp_s, rr, myuid, thisuid;
68
69         rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
70
71         for (k = 0; k < MapInfo_count; ++k)
72         {
73                 if (MapInfo_Get_ByID(k))
74                 {
75                         for (i = 0; i <= LADDER_CNT; ++i) // i = 0 because it is the speed award
76                         {
77                                 if (i == 0)                   // speed award
78                                 {
79                                         if (stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, rr, "speed/speed"))) == 0) continue;
80
81                                         myuid = db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, rr, "speed/crypto_idfp"));
82                                 }
83                                 else  // normal record, if it exists (else break)
84                                 {
85                                         if (race_readTime(MapInfo_Map_bspname, i) == 0) continue;
86
87                                         myuid = race_readUID(MapInfo_Map_bspname, i);
88                                 }
89
90                                 // string s contains:
91                                 // arg 0 = # of speed recs
92                                 // arg 1 = # of 1st place recs
93                                 // arg 2 = # of 2nd place recs
94                                 // ... etc
95                                 // LADDER_CNT+1 = total points
96
97                                 temp_s = db_get(TemporaryDB, strcat("ladder", myuid));
98
99                                 if (temp_s == "")
100                                 {
101                                         db_put(TemporaryDB, strcat("uid", ftos(uidcnt)), myuid);
102                                         ++uidcnt;
103
104                                         for (j = 0; j <= LADDER_CNT + 1; ++j)
105                                         {
106                                                 if (j != LADDER_CNT + 1) temp_s = strcat(temp_s, "0 ");
107                                                 else temp_s = strcat(temp_s, "0");
108                                         }
109                                 }
110
111                                 tokenize_console(temp_s);
112                                 s = "";
113
114                                 if (i == 0)                                         // speed award
115                                 {
116                                         for (j = 0; j <= LADDER_CNT; ++j)               // loop over each arg in the string
117                                         {
118                                                 if (j == 0)                                 // speed award
119                                                         s = strcat(s, ftos(stof(argv(j)) + 1)); // add 1 to speed rec count and write
120                                                 else s = strcat(s, " ", argv(j));           // just copy over everything else
121                                         }
122                                 }
123                                 else  // record
124                                 {
125                                         for (j = 0; j <= LADDER_CNT; ++j)                    // loop over each arg in the string
126                                         {
127                                                 if (j == 0) s = strcat(s, argv(j));              // speed award, dont prefix with " "
128                                                 else if (j == i)                                 // wanted rec!
129                                                         s = strcat(s, " ", ftos(stof(argv(j)) + 1)); // update argv(j)
130                                                 else s = strcat(s, " ", argv(j));                // just copy over everything else
131                                         }
132                                 }
133
134                                 // total points are (by default) calculated like this:
135                                 // speedrec = floor(100 / 10) = 10 points
136                                 // 1st place = floor(100 / 1) = 100 points
137                                 // 2nd place = floor(100 / 2) = 50 points
138                                 // 3rd place = floor(100 / 3) = 33 points
139                                 // 4th place = floor(100 / 4) = 25 points
140                                 // 5th place = floor(100 / 5) = 20 points
141                                 // ... etc
142
143                                 if (i == 0) s = strcat(s, " ", ftos(stof(argv(LADDER_CNT + 1)) + LADDER_FIRSTPOINT / 10)); // speed award, add LADDER_FIRSTPOINT / 10 points
144                                 else s = strcat(s, " ", ftos(stof(argv(LADDER_CNT + 1)) + floor(LADDER_FIRSTPOINT / i)));  // record, add LADDER_FIRSTPOINT / i points
145
146                                 db_put(TemporaryDB, strcat("ladder", myuid), s);
147                         }
148                 }
149         }
150
151         for (i = 0; i <= uidcnt; ++i)  // for each known uid
152         {
153                 thisuid = db_get(TemporaryDB, strcat("uid", ftos(i)));
154                 temp_s = db_get(TemporaryDB, strcat("ladder", thisuid));
155                 tokenize_console(temp_s);
156                 thiscnt = stof(argv(LADDER_CNT + 1));
157
158                 if (thiscnt > top_scores[LADDER_SIZE - 1])
159                 {
160                         for (j = 0; j < LADDER_SIZE; ++j)  // for each place in ladder
161                         {
162                                 if (thiscnt > top_scores[j])
163                                 {
164                                         for (k = LADDER_SIZE - 1; k >= j; --k)
165                                         {
166                                                 top_uids[k] = top_uids[k - 1];
167                                                 top_scores[k] = top_scores[k - 1];
168                                         }
169
170                                         top_uids[j] = thisuid;
171                                         top_scores[j] = thiscnt;
172                                         break;
173                                 }
174                         }
175                 }
176         }
177
178         s = "^3-----------------------\n\n";
179
180         s = strcat(s, "Pos ^3|");
181         s = strcat(s, " ^7Total  ^3|");
182
183         for (i = 1; i <= LADDER_CNT; ++i)
184                 s = strcat(s, " ^7", count_ordinal(i), " ^3|");
185         s = strcat(s, " ^7Speed awards ^3| ^7Name");
186         s = strcat(s, "\n^3----+--------");
187
188         for (i = 1; i <= min(9, LADDER_CNT); ++i)
189                 s = strcat(s, "+-----");
190 #if LADDER_CNT > 9
191                 for (i = 1; i <= LADDER_CNT - 9; ++i)
192                         s = strcat(s, "+------");
193 #endif
194
195         s = strcat(s, "+--------------+--------------------\n");
196
197         for (i = 0; i < LADDER_SIZE; ++i)
198         {
199                 temp_s = db_get(TemporaryDB, strcat("ladder", top_uids[i]));
200                 tokenize_console(temp_s);
201
202                 if (argv(LADDER_CNT + 1) == "")                           // total is 0, skip
203                         continue;
204
205                 s = strcat(s, strpad(4, count_ordinal(i + 1)), "^3| ^7"); // pos
206                 s = strcat(s, strpad(7, argv(LADDER_CNT + 1)), "^3| ^7"); // total
207
208                 for (j = 1; j <= min(9, LADDER_CNT); ++j)
209                         s = strcat(s, strpad(4, argv(j)), "^3| ^7");          // 1st, 2nd, 3rd etc cnt
210
211 #if LADDER_CNT > 9
212                         for (j = 10; j <= LADDER_CNT; ++j)
213                                 s = strcat(s, strpad(4, argv(j)), " ^3| ^7"); // 1st, 2nd, 3rd etc cnt
214 #endif
215
216                 s = strcat(s, strpad(13, argv(0)), "^3| ^7");         // speed award cnt
217                 s = strcat(s, uid2name(top_uids[i]), "\n");           // name
218         }
219
220         MapInfo_ClearTemps();
221
222         if (s == "") return "No ladder on this server!\n";
223         else return strcat("Top ", ftos(LADDER_SIZE), " ladder rankings:\n", s);
224 }
225
226 string getmaplist()
227 {
228         string maplist = "", col;
229         int i, argc;
230
231         argc = tokenize_console(autocvar_g_maplist);
232         for (i = 0; i < argc; ++i)
233         {
234                 if (MapInfo_CheckMap(argv(i)))
235                 {
236                         if (i % 2) col = "^2"; else col = "^3";
237                         maplist = sprintf("%s%s%s ", maplist, col, argv(i));
238                 }
239         }
240
241         MapInfo_ClearTemps();
242         return sprintf("^7Maps in list: %s\n", maplist);
243 }
244
245 const int LSMAPS_MAX = 250;
246 string getlsmaps()
247 {
248         string lsmaps = "", col;
249         bool newmaps = false;
250         int added = 0;
251
252         for (int i = 0; i < MapInfo_count; ++i)
253         {
254                 if ((MapInfo_Get_ByID(i)) && !(MapInfo_Map_flags & MapInfo_ForbiddenFlags()))
255                 {
256                         ++added;
257
258                         if(added > LSMAPS_MAX)
259                                 continue; // we still get the added count, but skip the actual processing
260
261                         // todo: Check by play count of maps for other game types?
262                         if (
263                             (g_race && !stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, RACE_RECORD, "time"))))
264                             ||
265                             (g_cts && !stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, CTS_RECORD, "time"))))
266                            )
267                         {
268                                 newmaps = true;
269                                 if (i % 2) col = "^4*"; else col = "^5*";
270                         }
271                         else
272                         {
273                                 if (i % 2) col = "^2"; else col = "^3";
274                         }
275
276                         lsmaps = sprintf("%s%s%s ", lsmaps, col, MapInfo_Map_bspname);
277                 }
278         }
279
280         if(added > LSMAPS_MAX)
281                 lsmaps = sprintf("%s^7(%d not listed)", lsmaps, added - LSMAPS_MAX);
282
283         MapInfo_ClearTemps();
284         return sprintf("^7Maps available (%d)%s: %s\n", added, (newmaps ? " (New maps have asterisks marked in blue)" : ""), lsmaps);
285 }
286
287 string getmonsterlist()
288 {
289         string monsterlist = "", col;
290
291         for (int i = MON_FIRST; i <= MON_LAST; ++i)
292         {
293                 if (i % 2) col = "^2"; else col = "^3";
294                 monsterlist = sprintf("%s%s%s ", monsterlist, col, (get_monsterinfo(i)).netname);
295         }
296
297         return sprintf("^7Monsters available: %s\n", monsterlist);
298 }