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