+const float LADDER_FIRSTPOINT = 100;
+const float LADDER_CNT = 10; // position X still gives LADDER_FIRSTPOINT/X points
+const float LADDER_SIZE = 30; // ladder shows the top X players
+string top_uids[LADDER_SIZE];
+float top_scores[LADDER_SIZE];
+float UIDCNT;
+string getladder()
+{
+ float i, j, k;
+ string s, len, temp_s;
+
+ s = "";
+ temp_s = "";
+
+ string myuid;
+
+ for (k = 0; k < MapInfo_count; ++k)
+ {
+ if (MapInfo_Get_ByID(k))
+ {
+ for (i = 0; i < LADDER_CNT; ++i) {
+ if(stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(Ii)))) == 0)
+ break;
+
+ myuid = db_get(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)));
+
+ // string s contains:
+ // arg 0 = # of speed recs
+ // arg 1 = # of 1st place recs
+ // arg 2 = # of 2nd place recs
+ // ... etc
+ // LADDER_CNT+1 = total points
+
+ temp_s = db_get(TemporaryDB, strcat("ladder", myuid));
+ if (temp_s == "")
+ {
+ db_put(TemporaryDB, strcat("uid", ftos(UIDCNT)), myuid);
+ ++UIDCNT;
+ for (j = 0; j <= LADDER_CNT + 1; ++j)
+ {
+ if(j != LADDER_CNT + 1)
+ temp_s = strcat(temp_s, "0 ");
+ else
+ temp_s = strcat(temp_s, "0");
+ }
+ }
+
+ len = tokenize_console(temp_s);
+ s = "";
+
+ if(i == 0) // speedrec
+ for (j = 0; j <= LADDER_CNT; ++j) // loop over each arg in the string
+ {
+ if(j == 0) // speed rec!
+ s = strcat(s, ftos(stof(argv(j)) +1)); // add 1 to speed rec count and write
+ else
+ s = strcat(s, " ", argv(j)); // just copy over everything else
+ }
+ else // record
+ for (j = 0; j <= LADDER_CNT; ++j) // loop over each arg in the string
+ {
+ if(j == 0)
+ s = strcat(s, argv(j)); // speed rec, dont prefix with " "
+ else if(j == i) // wanted rec!
+ s = strcat(s, " ", ftos(stof(argv(j)) +1)); // update argv(j)
+ else
+ s = strcat(s, " ", argv(j)); // just copy over everything else
+ }
+
+ // total points are (by default) calculated like this:
+ // speedrec = floor(100 / 10) = 10 points
+ // 1st place = floor(100 / 1) = 100 points
+ // 2nd place = floor(100 / 2) = 50 points
+ // 3rd place = floor(100 / 3) = 33 points
+ // 4th place = floor(100 / 4) = 25 points
+ // 5th place = floor(100 / 5) = 20 points
+ // ... etc
+
+ if(i == 0)
+ s = strcat(s, " ", ftos(stof(argv(LADDER_CNT+1)) + LADDER_FIRSTPOINT / 10)); // speedrec, add LADDER_FIRSTPOINT / 10 points
+ else
+ s = strcat(s, " ", ftos(stof(argv(LADDER_CNT+1)) + floor(LADDER_FIRSTPOINT / i))); // record, add LADDER_FIRSTPOINT / i points
+
+ db_put(TemporaryDB, strcat("ladder", myuid), s);
+ }
+ }
+ }
+ }
+
+ float thiscnt;
+ string thisuid;
+ for (i = 0; i <= UIDCNT; ++i) // for each known uid
+ {
+ thisuid = db_get(TemporaryDB, strcat("uid", ftos(i)));
+ temp_s = db_get(TemporaryDB, strcat("ladder", thisuid));
+ len = tokenize_console(temp_s);
+ thiscnt = stof(argv(LADDER_CNT+1));
+
+ if(thiscnt > top_scores[LADDER_SIZE-1])
+ for (j = 0; j < LADDER_SIZE; ++j) // for each place in ladder
+ {
+ if(thiscnt > top_scores[j])
+ {
+ for (k = LADDER_SIZE-1; k >= j; --k)
+ {
+ top_uids[k] = top_uids[k-1];
+ top_scores[k] = top_scores[k-1];
+ }
+ top_uids[j] = thisuid;
+ top_scores[j] = thiscnt;
+ break;
+ }
+ }
+ }
+
+ s = "^3-----------------------\n\n";
+
+ s = strcat(s, "Pos ^3|");
+ s = strcat(s, " ^7Total ^3|");
+ for (i = 1; i <= LADDER_CNT; ++i)
+ {
+ s = strcat(s, " ^7", race_PlaceName(i), " ^3|");
+ }
+ s = strcat(s, " ^7Speed awards ^3| ^7Name");
+
+ s = strcat(s, "\n^3----+--------");
+ for (i = 1; i <= LADDER_CNT; ++i)
+ {
+ s = strcat(s, "+-----");
+ }
+ s = strcat(s, "+--------------+--------------------\n");
+
+ for (i = 0; i < LADDER_SIZE; ++i)
+ {
+ temp_s = db_get(TemporaryDB, strcat("ladder", top_uids[i]));
+ len = tokenize_console(temp_s);
+ if (argv(LADDER_CNT+1) == "") // total is 0, skip
+ continue;
+ s = strcat(s, strpad(4, race_PlaceName(i+1)), "^3| ^7"); // pos
+ s = strcat(s, strpad(7, argv(LADDER_CNT+1)), "^3| ^7"); // total
+ for (j = 1; j <= LADDER_CNT; ++j)
+ {
+ s = strcat(s, strpad(4, argv(j)), "^3| ^7"); // 1st, 2nd, 3rd etc cnt
+ }
+ s = strcat(s, strpad(13, argv(0)), "^3| ^7"); // speed rec cnt
+ s = strcat(s, race_GetName_OfUID(top_uids[i]), "\n"); // name
+ }
+
+ MapInfo_ClearTemps();
+
+ if (s == "")
+ return "No ladder on this server!\n";
+ else
+ return strcat("Top ", ftos(LADDER_SIZE), " ladder rankings:\n", s);
+}
+
+