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