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