+ // add global info!
+ if(p.alivetime)
+ {
+ PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
+ p.alivetime = 0;
+ }
+
+ db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
+
+ if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
+ db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
+
+ if(teamplay)
+ db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
+
+ if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
+ PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
+
+ PlayerStats_Accuracy(p);
+
+ if(IS_REAL_CLIENT(p))
+ {
+ if(p.latency_cnt)
+ {
+ float latency = (p.latency_sum / p.latency_cnt);
+ if(latency) { PlayerStats_Event(p, PLAYERSTATS_AVGLATENCY, latency); }
+ }
+ }
+
+ strunzone(p.playerstats_id);
+ p.playerstats_id = string_null;
+}
+
+.float scoreboard_pos;
+void PlayerStats_GameReport_EndMatch(float finished)
+{
+ entity p;
+ PlayerScore_Sort(score_dummyfield, 0, 0, 0);
+ PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
+ if(teamplay) { PlayerScore_TeamStats(); }
+ FOR_EACH_CLIENT(p)
+ {
+ // add personal score rank
+ PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
+
+ if(!p.scoreboard_pos) { continue; }
+
+ // scoreboard is valid!
+ PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
+
+ // add scoreboard position
+ PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
+
+ // add scoreboard data
+ PlayerScore_PlayerStats(p);
+
+ // if the match ended normally, add winning info
+ if(finished)
+ {
+ PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
+ PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
+ }
+ }
+}
+
+void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
+{
+ string uri;
+ playerstats_db = -1;
+ playerstats_waitforme = TRUE;
+ uri = autocvar_g_playerstats_uri;
+ if(uri == "")
+ return;
+ playerstats_db = db_create();
+ if(playerstats_db >= 0)
+ playerstats_waitforme = FALSE; // must wait for it at match end
+
+ serverflags |= SERVERFLAG_PLAYERSTATS;
+
+ PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
+ PlayerStats_AddEvent(PLAYERSTATS_AVGLATENCY);
+ PlayerStats_AddEvent(PLAYERSTATS_WINS);
+ PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
+ PlayerStats_AddEvent(PLAYERSTATS_JOINS);
+ PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
+ PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
+ PlayerStats_AddEvent(PLAYERSTATS_RANK);
+
+ // accuracy stats
+ entity w;
+ float i;
+ for(i = WEP_FIRST; i <= WEP_LAST; ++i)
+ {
+ w = get_weaponinfo(i);
+ PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
+ PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
+ PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
+ PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
+ PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
+ }
+
+ PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
+ PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
+ PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
+ PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
+ PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
+ PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
+ PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
+ PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
+ PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
+ PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
+}
+
+void PlayerStats_GameReport_Shutdown()
+{
+ string uri;
+
+ if(playerstats_db < 0) { return; }
+
+ uri = autocvar_g_playerstats_uri;
+ if(uri != "")
+ {
+ playerstats_waitforme = FALSE;
+ url_multi_fopen(uri, FILE_APPEND, PlayerStats_GameReport_Handler, world);
+ }
+ else
+ {
+ playerstats_waitforme = TRUE;
+ db_close(playerstats_db);
+ playerstats_db = -1;
+ }
+}
+
+void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)