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