void() spawnfunc_info_player_deathmatch; // needed for the other spawnpoints
void() spawnpoint_use;
-string race_GetName(float pos);
-string race_PlaceName(float pos);
+string race_readName(float pos);
+string race_placeName(float pos);
string GetMapname();
string ColoredTeamName(float t);
return s;
}
-float race_GetTime(string map, float pos)
+float race_readTime(string map, float pos)
{
- float rr;
+ string rr;
if(g_cts)
rr = CTS_RECORD;
else
return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
}
-string race_GetUID(string map, float pos)
+string race_readUID(string map, float pos)
{
- float rr;
+ string rr;
if(g_cts)
rr = CTS_RECORD;
else
rr = RACE_RECORD;
- return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
+ return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
+}
+
+float race_readPos(string map, float t) {
+ float i;
+ for (i = 1; i <= RANKINGS_CNT; ++i)
+ if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
+ return i;
+
+ return 0; // pos is zero if unranked
+}
+
+float race_writeTime(string map, float t, string myuid)
+{
+ string rr;
+ if(g_cts)
+ rr = CTS_RECORD;
+ else
+ rr = RACE_RECORD;
+
+ float pos;
+ pos = race_readPos(map, t);
+
+ if(pos) { // don't even ALLOW writing unranked times into the db, less stuff to worry about in other code then :-)
+ db_put(ServerProgsDB, strcat(map, rr, "time", ftos(pos)), ftos(t));
+ db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)), myuid);
+ }
}
-string race_GetName(string map, float pos)
+string race_readName(string map, float pos)
{
- float rr;
+ string rr;
if(g_cts)
rr = CTS_RECORD;
else
return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
}
-string race_PlaceName(float pos) {
+string race_placeName(float pos) {
if(floor((pos % 100)/10) * 10 != 10) // examples: 12th, 111th, 213th will not execute this block
{
if(pos % 10 == 1)
{
if (MapInfo_Get_ByID(i))
{
- r = race_GetTime(MapInfo_Map_bspname, 1);
+ r = race_readTime(MapInfo_Map_bspname, 1);
if (r == 0)
continue;
- h = race_GetName(MapInfo_Map_bspname, 1)
+ h = race_readName(MapInfo_Map_bspname, 1)
s = strcat(s, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
++rec;
}
{
if (MapInfo_Get_ByID(i))
{
- r = race_GetTime(MapInfo_Map_bspname, 1);
+ r = race_readTime(MapInfo_Map_bspname, 1);
if (r == 0)
continue;
- h = race_GetName(MapInfo_Map_bspname, 1)
+ h = race_readName(MapInfo_Map_bspname, 1)
s = strcat(s, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
++rec;
}
for (i = 1; i <= RANKINGS_CNT; ++i)
{
- t = race_GetTime(map, i);
+ t = race_readTime(map, i);
if (t == 0)
continue;
- n = race_GetName(map, i);
- p = race_PlaceName(i);
+ n = race_readName(map, i);
+ p = race_placeName(i);
s = strcat(s, strpad(8, p), " ", strpad(-8, TIME_ENCODED_TOSTRING(t)), " ", n, "\n");
}
if (MapInfo_Get_ByID(k))
{
for (i = 0; i <= LADDER_CNT; ++i) { // i = 0 because it is the speed award
- if(race_GetTime(GetMapname(), i) == 0)
+ if(race_readTime(GetMapname(), i) == 0)
break;
- myuid = race_GetUID(GetMapname(), i);
+ myuid = race_readUID(GetMapname(), i);
// string s contains:
// arg 0 = # of speed recs
s = strcat(s, " ^7Total ^3|");
for (i = 1; i <= LADDER_CNT; ++i)
{
- s = strcat(s, " ^7", race_PlaceName(i), " ^3|");
+ s = strcat(s, " ^7", race_placeName(i), " ^3|");
}
s = strcat(s, " ^7Speed awards ^3| ^7Name");
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(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)
{
race_SendNextCheckpoint(msg_entity.enemy, 1);
}
-string rr;
-float grecordtime[RANKINGS_CNT];
-string grecordholder[RANKINGS_CNT];
-string grecorduid[RANKINGS_CNT];
-float worst_time; // last ranked time
-float have_recs; // have we already read the records from the database before?
-float race_GetTime(float pos) {
- if(g_cts)
- rr = CTS_RECORD;
- else
- rr = RACE_RECORD;
-
- if (!have_recs) { // I guess this is better than checking if the first array item is empty, rumor has it that arrays are slow
- float i;
- for(i=0;i<RANKINGS_CNT;++i) {
- grecordtime[i] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i))));
- grecordholder[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i))));
- grecorduid[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i))));
- }
- grecordtime[0] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
- grecordholder[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname")));
- grecorduid[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp")));
- worst_time = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, strcat("time", ftos(RANKINGS_CNT-1)))));
- have_recs = 1;
- }
-
- return grecordtime[pos-1];
-}
-
-string race_GetName(float pos) { // these other functions assume that race_GetTime has been called >= once before
- return grecordholder[pos-1];
-}
-
-float race_CheckUID(string myuid, string net_name) { // return existing UID or player name ranking pos, else 0
- float i;
- if(myuid)
- {
- for (i=RANKINGS_CNT-1;i>=0;--i)
- if(grecorduid[i] == myuid)
- return i+1;
- }
- for (i=RANKINGS_CNT-1;i>=0;--i)
- if(!grecorduid[i])
- if(grecordholder[i] == net_name)
- return i+1;
- return 0;
-}
-
-float race_GetPos(float t) {
- float i;
-
- if(worst_time == 0)
- for (i=0;i<RANKINGS_CNT;++i)
- if (grecordtime[i] == 0 || grecordtime[i] > t)
- return i+1;
-
- for (i=0;i<RANKINGS_CNT;++i)
- if (grecordtime[i] > t)
- return i+1;
- return 0;
-}
-
void race_send_recordtime(float msg)
{
// send the server best time
WriteByte(msg, SVC_TEMPENTITY);
WriteByte(msg, TE_CSQC_RACE);
WriteByte(msg, RACE_NET_SERVER_RECORD);
- WriteInt24_t(msg, race_GetTime(1));
+ WriteInt24_t(msg, race_readTime(1));
}
void race_SendRankings(float pos, float prevpos, float del, float msg)
WriteShort(msg, pos);
WriteShort(msg, prevpos);
WriteShort(msg, del);
- WriteString(msg, race_GetName(pos));
- WriteInt24_t(msg, race_GetTime(pos));
+ WriteString(msg, race_readName(pos));
+ WriteInt24_t(msg, race_readTime(pos));
}
void race_SendStatus(float id, entity e)
});
}
-void race_SetTime(entity e, float t, float match_rec) {
- float pos, prevpos;
- pos = race_GetPos(t);
- prevpos = race_CheckUID(e.crypto_idfp, e.netname);
+void race_setTime(string map, float t, string myuid, string netname) { // netname only used TEMPORARILY for printing
+ float newpos, prevpos;
+ newpos = race_readPos(map, t);
+
+ float i;
+ for(i = 1; i <= RANKINGS_CNT; ++i)
+ {
+ if(race_readUID(map, i) == myuid)
+ prevpos = i;
+ }
float oldrec;
string recorddifference;
if (prevpos && (prevpos < pos || !pos))
{
- oldrec = race_GetTime(prevpos);
+ oldrec = race_readTime(prevpos);
recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - oldrec), "]");
- bprint(e.netname, "^7 couldn't break their ", race_PlaceName(prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
+ bprint(e.netname, "^7 couldn't break their ", race_placeName(prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
race_SendStatus(0, e); // "fail"
return;
} else if (!pos) { // no ranking, time worse than the worst ranked
recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - grecordtime[RANKINGS_CNT-1]), "]");
- bprint(e.netname, "^7 couldn't break the ", race_PlaceName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(grecordtime[RANKINGS_CNT-1]), recorddifference, "\n");
+ bprint(e.netname, "^7 couldn't break the ", race_placeName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(grecordtime[RANKINGS_CNT-1]), recorddifference, "\n");
race_SendStatus(0, e); // "fail"
return;
}
// move other rankings out of the way
float i;
- if (prevpos) { // player improved his existing record
- for (i=prevpos-1;i>pos-1;--i) {
+ if (prevpos) { // player improved his existing record, only have to iterate on ranks between new and old recs
+ for (i=prevpos;i>pos;--i) {
db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
- db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), grecorduid[i-1]);
- grecordtime[i] = grecordtime[i-1];
-
- if (grecordholder[i])
- strunzone(grecordholder[i]);
- grecordholder[i] = strzone(grecordholder[i-1]);
- if (grecorduid[i])
- strunzone(grecorduid[i]);
- grecorduid[i] = strzone(grecorduid[i-1]);
}
} else { // player has no ranked record yet
- for (i=RANKINGS_CNT-1;i>pos-1;--i) {
- db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
- db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
- db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), grecorduid[i-1]);
- grecordtime[i] = grecordtime[i-1];
-
- if (grecordholder[i])
- strunzone(grecordholder[i]);
- grecordholder[i] = strzone(grecordholder[i-1]);
- if (grecorduid[i])
- strunzone(grecorduid[i]);
- grecorduid[i] = strzone(grecorduid[i-1]);
+ for (i=RANKINGS_CNT;i>pos;--i) {
+ db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i]));
+ db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), grecorduid[i]);
}
}
// store new ranking
if (pos == 1) {
db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
- db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp"), e.crypto_idfp);
grecordtime[0] = t;
race_send_recordtime(MSG_ALL);
} else {
db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(pos-1)), ftos(t));
- db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(pos-1)), e.netname);
db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(pos-1)), e.crypto_idfp);
grecordtime[pos-1] = t;
}
if (pos == RANKINGS_CNT)
- worst_time = t;
+ lastpos_time = t;
race_SendRankings(pos, prevpos, 0, MSG_ALL);
if(rankings_reply)
} else {
if(pos == prevpos) {
recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
- bprint(e.netname, "^5 improved their ", race_PlaceName(pos), " ^5place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
+ bprint(e.netname, "^5 improved their ", race_placeName(pos), " ^5place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
race_SendStatus(1, e); // "new time"
} else if (oldrec == 0) {
- bprint(e.netname, "^2 set the ", race_PlaceName(pos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
+ bprint(e.netname, "^2 set the ", race_placeName(pos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
race_SendStatus(2, e); // "new rank"
} else {
recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
- bprint(e.netname, "^2 broke ", grecordholder[pos], "^2's ", race_PlaceName(pos), " ^2place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
+ bprint(e.netname, "^2 broke ", grecordholder[pos], "^2's ", race_placeName(pos), " ^2place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
race_SendStatus(2, e); // "new rank"
}
}
for (i = pos-1; i <= RANKINGS_CNT-1; ++i) {
if (i == 0) {
db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(grecordtime[1]));
- db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), grecordholder[1]);
db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp"), grecorduid[1]);
grecordtime[0] = grecordtime[1];
if (grecordholder[i])
}
else if (i == RANKINGS_CNT-1) {
db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), string_null);
- db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), string_null);
db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), string_null);
grecordtime[i] = 0;
if (grecordholder[i])
}
else {
db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i+1]));
- db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i+1]);
db_put(ServerProgsDB, strcat(GetMapname(), rr, "crypto_idfp", ftos(i)), grecorduid[i+1]);
grecordtime[i] = grecordtime[i+1];
if (grecordholder[i])
strunzone(rankings_reply);
rankings_reply = strzone(getrankings());
- worst_time = 0;
+ lastpos_time = 0;
}
void race_SendTime(entity e, float cp, float t, float tvalid)