-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStats.as
747 lines (582 loc) · 22.4 KB
/
Stats.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// using the filesystem as a database. Shouldn't be a problem until sven has millions of players (never)
// DB size for TWLZ is about 30 MB (50k unique players, mostly short visitors)
const string ROOT_DB_PATH = "scripts/plugins/store/rtv/";
// TODO
// - detect series maps so playing 50% of a series counts as a play (I always join HL late)
// - add map prefs
// - replace game_end with custom ent so script can run next map logic
// - categorize every map or add tags
// - add categories to prefs and nom menu
// Your map preferences:
// 1. Preferences enabled: [Yes, No]
// 2. Types: [action, puzzle, chill, skill, meme, Any]
// 3. Quality: [good, bad, Any]
// 4. Mode: [survival, casual, Any]
// Map preferences -> Select categories
// 1. ..
// 2. Action
// 3. Puzzle
// 5. Chill
// 6. Skill
// 7. Meme
// Total maps in your selection: 1234
// Your preferred map list has a chance to be used when deciding the next map.
// Because you have 0 maps selected, other players will decide the next map.
// Say '.cycle' for more info.
// Use bucket folders to keep filesystem lookups fast.
// For 50k unique players, 256 buckets would have 195 files each, on average
// Make sure to keep this in sync with the db_setup.py script
const uint MAX_DB_BUCKETS = 256;
const int FRESH_MAP_LIMIT = 20;
const float MIN_ACTIVE_TIME = 0.5f;
dictionary g_player_map_history; // maps steam id to PlayerMapHistory
dictionary g_player_activity; // maps steam id to time they joined the map. used to track play time for the previous map
dictionary g_prev_map_activity; // maps steam id to percentage active time in previous map
array<SteamName> g_nextmap_players; // active players from the previous map that will decide the next map for the current map
bool g_anyone_joined = false; // used to prevent double-loaded maps from clearing active player list
string g_previous_stats_map = ""; // last map which ran the map selection logic
float g_lastActiveCheck = 0;
enum MAP_RATINGS {
RATE_NONE, // no preference or never played
RATE_FAVORITE, // player likes the map and wants to play it all the time
RATE_TRASH // player hates the map and never wants to play it again
}
// stats for a map by a single player
// used in a dictionary which maps a map name -> player
class MapStat {
uint32 last_played = 0; // unix timestamp, max date of 2106
uint16 total_plays = 0;
uint8 rating = 0; // does the player like this map?
}
class PlayerMapHistory {
HashMapMapStat stats = HashMapMapStat(512); // maps map name to MapStat
bool loaded = false;
File@ fileHandle = null; // for loading across multiple server frames
int lineNum = 1;
PlayerMapHistory() {}
}
class PlayerActivity {
float totalActivity;
}
class SteamName {
string steamid;
string name;
SteamName() {}
SteamName(string steamid, string name) {
this.steamid = steamid;
this.name = name;
}
}
class LastPlay {
string steamid;
string name;
bool wasConsidered = false; // was considered for deciding the next map
uint64 last_played = 0;
int previousPlayPercent;
int total_plays = 0;
LastPlay() {}
LastPlay(string steamid, string name, bool wasConsidered) {
this.steamid = steamid;
this.name = name;
this.wasConsidered = wasConsidered;
if (g_prev_map_activity.exists(steamid)) {
g_prev_map_activity.get(steamid, previousPlayPercent);
}
}
}
enum TargetTypes {
TARGET_SELF,
TARGET_PLAYER,
TARGET_ALL
}
funcdef void void_callback();
funcdef void dict_callback(dictionary);
void initStats() {
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
string steamid = getPlayerUniqueId(plr);
loadPlayerMapStats(steamid, function(args){}, {});
g_player_activity[steamid] = PlayerActivity();
}
g_lastActiveCheck = g_Engine.time;
g_Scheduler.SetInterval("updatePlayerActivity", 1.0f, -1);
}
void updatePlayerActivity() {
float delta = g_Engine.time - g_lastActiveCheck;
g_lastActiveCheck = g_Engine.time;
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
if (g_playerStates[i].afkTime != 0) {
continue;
}
PlayerActivity@ activity = cast<PlayerActivity@>(g_player_activity[getPlayerUniqueId(plr)]);
if (activity !is null) {
activity.totalActivity += delta;
}
}
}
string getPlayerDbPath(string steamid) {
string safeid = steamid.Replace(":", "_");
uint64 hash = hash_FNV1a(safeid) % MAX_DB_BUCKETS;
return ROOT_DB_PATH + hash + "/" + safeid + ".txt";
}
void loadPlayerMapStats(string steamid, dict_callback@ callback, dictionary callbackArgs) {
PlayerMapHistory@ history = null;
if (g_player_map_history.exists(steamid)) {
@history = cast<PlayerMapHistory@>( g_player_map_history[steamid] );
if (history !is null && history.loaded) {
//println("Skipping map history for player - already loaded");
callback(callbackArgs);
return;
}
}
string path = getPlayerDbPath(steamid);
if (history is null) {
g_player_map_history.set(steamid, PlayerMapHistory());
@history = cast<PlayerMapHistory@>( g_player_map_history[steamid] );
@history.fileHandle = g_FileSystem.OpenFile(path, OpenFile::READ);
if (history.fileHandle is null or !history.fileHandle.IsOpen()) {
history.loaded = true;
g_Log.PrintF("[RTV] player stat file not found: " + path + "\n");
return;
}
}
// limit lines loaded per server frame to prevent server freezing
const int max_line_loads_per_frame = 32;
int linesLoaded = 0;
while (!history.fileHandle.EOFReached() and linesLoaded < max_line_loads_per_frame) {
string line;
history.fileHandle.ReadLine(line);
if (line.IsEmpty()) {
continue;
}
array<string> parts = line.Split(" ");
if (parts.size() != 4) {
g_Log.PrintF("[RTV] player stat file malformed (line " + history.lineNum + "): " + path + "\n");
continue;
}
MapStat stat;
string map_name = parts[0];
stat.last_played = atoi(parts[1]);
stat.total_plays = atoi(parts[2]);
stat.rating = atoi(parts[3]);
history.stats.put(map_name, stat);
history.lineNum += 1;
linesLoaded += 1;
}
if (history.fileHandle.EOFReached()) {
history.fileHandle.Close();
history.loaded = true;
callback(callbackArgs);
} else {
g_Scheduler.SetTimeout("loadPlayerMapStats", 0.0f, steamid, callback, callbackArgs);
}
}
// find the map that has the highest minumum age across all players (age = time since last play)
// example:
// I played stadium4 1 hour ago, but no one else has ever played it. It will not be selected
// because there are other maps which none of us have played in the last hour.
// This also means that if you never played stadium4, you might have to play
// a map that you did last week because someone else played stadium4 recently.
void setFreshMapAsNextMap(array<SortableMap>@ maps) {
if (g_nextmap_players.size() == 0) {
println("No players were active in the previous map. Selecting a random next map.");
string nextmap = maps[Math.RandomLong(0, maps.size()-1)].map;
g_EngineFuncs.ServerCommand("mp_nextmap_cycle " + nextmap + "\n");
return;
}
sortMapsByFreshness(maps, g_nextmap_players, 0, setNextMap, {});
}
void setNextMap(dictionary args) {
// prevent the same maps always being shown for players who have little history
uint maxUnplayed = 0;
for (uint i = 0; i < g_randomCycleMaps.size(); i++) {
if (g_randomCycleMaps[i].sort != 0) {
maxUnplayed = i;
break;
}
}
string nextmap = g_randomCycleMaps[Math.RandomLong(0, maxUnplayed)].map;
println("[RTV] Most fresh next map: " + nextmap);
g_EngineFuncs.ServerCommand("mp_nextmap_cycle " + nextmap + "\n");
}
uint insertSort(array<SortableMap>@ a, uint i) {
int ops = 0; // limit operations per frame so server doesn't freeze
for (i; i < a.size() && ops < 2000; i++) {
SortableMap value = SortableMap(a[i]);
int j;
for (j = i - 1; j >= 0 && a[j].sort > value.sort; j--, ops++)
a[j + 1] = a[j];
a[j + 1] = value;
}
return i;
}
void insertion_sort_step(array<SortableMap>@ a, uint i, dict_callback@ callback, dictionary callbackArgs) {
i = insertSort(a, i);
if (i < a.size()) {
g_Scheduler.SetTimeout("insertion_sort_step", 0.0f, @a, i, callback, callbackArgs);
} else {
callback(callbackArgs);
}
}
void shuffle(array<SortableMap>@ arr) {
for (int i = arr.size()-1; i >= 1; i--) {
int j = Math.RandomLong(0, i);
SortableMap temp = SortableMap(arr[j]);
arr[j] = arr[i];
arr[i] = temp;
}
}
// sorts according to minimum play time of all players in the server
void sortMapsByFreshness(array<SortableMap>@ maps, array<SteamName>@ activePlayers, uint pidx, dict_callback@ callback, dictionary callbackArgs) {
if (activePlayers.size() == 0) {
g_Log.PrintF("[RTV] No one was active in the previous map. Using current players to sort maps by freshness.\n");
activePlayers = getAllPlayers();
if (activePlayers.size() == 0) {
g_Log.PrintF("[RTV] No one is in the server. Skipping map sort.\n");
callback(callbackArgs);
return;
}
}
array<bool> isSeriesMap(maps.size());
for (uint k = 0; k < maps.size(); k++) {
maps[k].sort = 0;
isSeriesMap[k] = g_seriesMaps.exists(maps[k].map);
}
for ( pidx; pidx < activePlayers.size(); pidx++ ) {
string steamid = activePlayers[pidx].steamid;
PlayerMapHistory@ history = cast<PlayerMapHistory@>(g_player_map_history[steamid]);
if (history is null) {
println(steamid + " has no map history yet");
continue;
}
for (uint k = 0; k < maps.size(); k++) {
if (isSeriesMap[k]) {
uint64 lastplay = getLastSeriesPlayTime(history, maps[k].map);
if (lastplay > maps[k].sort) {
maps[k].sort = lastplay;
}
} else {
MapStat@ stat = history.stats.get(maps[k].map, maps[k].hashKey);
if (stat.last_played > maps[k].sort) {
maps[k].sort = stat.last_played;
}
}
}
}
if (pidx < activePlayers.size()) {
g_Scheduler.SetTimeout("sortMapsByFreshness", 0.0f, @maps, @activePlayers, pidx, callback, callbackArgs);
} else {
insertion_sort_step(maps, 1, callback, callbackArgs);
}
}
// used to prevent parallel sorts messing up the global map lists
array<SortableMap> copyArray(array<SortableMap>@ arr) {
array<SortableMap> newArr;
for (uint i = 0; i < arr.size(); i++) {
newArr.insertLast(SortableMap(arr[i]));
}
return newArr;
}
uint64 getLastPlayTime(string steamid, SortableMap map) {
PlayerMapHistory@ history = cast<PlayerMapHistory@>(g_player_map_history[steamid]);
if (history is null) {
return 0;
}
if (g_seriesMaps.exists(map.map)) {
return getLastSeriesPlayTime(history, map.map);
}
return history.stats.get(map.map, map.hashKey).last_played;
}
uint64 getLastSeriesPlayTime(PlayerMapHistory@ history, string mapname) {
array<SortableMap>@ series = cast<array<SortableMap>@>(g_seriesMaps[mapname]);
uint64 mostRecent = 0;
for (uint i = 0; i < series.size(); i++) {
uint64 lastPlay = history.stats.get(series[i].map, series[i].hashKey).last_played;
mostRecent = Math.max(lastPlay, mostRecent);
}
return mostRecent;
}
void showFreshMaps(EHandle h_plr, int targetType, string targetName, string targetId, array<SortableMap>@ maps, bool reverse) {
CBasePlayer@ plr = cast<CBasePlayer@>(h_plr.GetEntity());
if (plr is null) {
return;
}
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "Processing map list...\n");
string header = "";
if (!reverse) {
header = "\n" + FRESH_MAP_LIMIT + " maps " + targetName + " played most recently\n\n";
} else {
header = "\n" + FRESH_MAP_LIMIT + " maps " + targetName + (targetType == TARGET_SELF ? " haven't" : " hasn't") + " played in a long time\n\n";
}
dictionary callbackArgs = {
{'plr', h_plr},
{'header', header},
{'maps', maps},
{'reverse', reverse}
};
if (targetType == TARGET_ALL) {
sortMapsByFreshness(maps, getAllPlayers(), 0, showFreshMaps_afterSort, callbackArgs);
}
else {
PlayerMapHistory@ history = cast<PlayerMapHistory@>(g_player_map_history[targetId]);
if (history is null) {
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, (targetType == TARGET_SELF ? "You have" : targetName + " has") + " no map history yet\n");
return;
}
for (uint k = 0; k < maps.size(); k++) {
MapStat@ stat = history.stats.get(maps[k].map, maps[k].hashKey);
maps[k].sort = stat.last_played;
}
insertion_sort_step(maps, 1, showFreshMaps_afterSort, callbackArgs);
}
}
void showFreshMaps_afterSort(dictionary args) {
CBasePlayer@ plr = cast<CBasePlayer@>(EHandle(args['plr']).GetEntity());
if (plr is null) {
return;
}
array<SortableMap>@ maps = cast<array<SortableMap>@>(args['maps']);
bool reverse = bool(args['reverse']);
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, string(args['header']));
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "Map name Last played\n");
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "----------------------------------------------------------\n");
int listedMaps = 0;
int numExclude = 0;
for (uint m = 0; listedMaps < FRESH_MAP_LIMIT && m < maps.size(); m++) {
uint idx = reverse ? m : maps.size() - (m+1);
string map = maps[idx].map;
if (g_memeMapsHashed.exists(map)) {
numExclude += 1;
continue;
}
listedMaps++;
int padding = 32;
padding -= map.Length();
string spad = "";
for (int p = 0; p < padding; p++) spad += " ";
int diff = int(DateTime().ToUnixTimestamp() - maps[idx].sort);
string age = maps[idx].sort == 0 ? "never" : formatLastPlayedTime(diff) + " ago";
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, map + spad + age + "\n");
}
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "----------------------------------------------------------\n\n");
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "Hidden maps (maps with long cooldowns) are not included in this list.\n\n");
}
void showLastPlayedTimes(CBasePlayer@ plr, string mapname) {
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "\nPrevious play times for \"" + mapname + "\"\n\n");
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "Player Last played Total Plays Previous activity\n");
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "---------------------------------------------------------------------------------\n");
array<LastPlay> allPlayers;
for ( uint c = 0; c < g_nextmap_players.size(); c++ ) {
allPlayers.insertLast(LastPlay(g_nextmap_players[c].steamid, g_nextmap_players[c].name, true));
}
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ p = g_PlayerFuncs.FindPlayerByIndex(i);
if (p is null or !p.IsConnected()) {
continue;
}
string steamid = getPlayerUniqueId(p);
bool alreadyInList = false;
for (uint k = 0; k < allPlayers.size(); k++) {
if (allPlayers[k].steamid == steamid) {
alreadyInList = true;
break;
}
}
if (!alreadyInList) {
allPlayers.insertLast(LastPlay(getPlayerUniqueId(p), p.pev.netname, false));
}
}
uint64 hashKey = hash_FNV1a(mapname);
for ( uint c = 0; c < allPlayers.size(); c++ ) {
PlayerMapHistory@ history = cast<PlayerMapHistory@>(g_player_map_history[allPlayers[c].steamid]);
if (history is null) {
allPlayers[c].last_played = 0;
}
MapStat@ stat = history.stats.get(mapname, hashKey);
allPlayers[c].last_played = stat.last_played;
allPlayers[c].total_plays = stat.total_plays;
}
allPlayers.sort(function(a,b) { return a.last_played < b.last_played; });
for ( uint c = 0; c < allPlayers.size(); c++ ) {
string steamid = allPlayers[c].steamid;
string name = allPlayers[c].name;
string prefix = allPlayers[c].wasConsidered ? "[x] " : "[ ] ";
int prc = allPlayers[c].previousPlayPercent;
string prevPlay = "" + prc + "%%";
if (prc < 100) prevPlay = " " + prevPlay;
if (prc < 10) prevPlay = " " + prevPlay;
prevPlay = prefix + prevPlay;
{
int padding = 32;
padding -= name.Length();
string spad = "";
for (int p = 0; p < padding; p++) spad += " ";
name += spad;
}
int64 diff = int64(DateTime().ToUnixTimestamp() - allPlayers[c].last_played);
string age = allPlayers[c].last_played == 0 ? "never" : formatLastPlayedTime(diff) + " ago";
{
int padding = 17;
padding -= age.Length();
string spad = "";
for (int p = 0; p < padding; p++) spad += " ";
age += spad;
}
string totalPlays = "" + allPlayers[c].total_plays;
{
int padding = 15;
padding -= totalPlays.Length();
string spad = "";
for (int p = 0; p < padding; p++) spad += " ";
totalPlays += spad;
}
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, name + age + totalPlays + prevPlay + "\n");
}
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "---------------------------------------------------------------------------------\n\n");
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "Previous play times are used to pick \"Next maps\" that have the highest minimum age.\n\n");
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "\"Previous activity\" shows the percentage of activity in the previous map (" + g_previous_stats_map + ").\n");
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "Players with an [x] were considered when deciding the current \"Next Map\".\n");
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "" + int(MIN_ACTIVE_TIME*100) + "%% is the minimum to be considered for map selection.\n\n");
}
// players who have been at least 50% active up until the current time
array<SteamName> getActivePlayers(bool connectedOnly=false) {
array<SteamName> activePlayers;
array<string>@ idKeys = g_player_activity.getKeys();
for (uint i = 0; i < idKeys.length(); i++) {
string steamid = idKeys[i];
PlayerActivity@ activity = cast<PlayerActivity@>(g_player_activity[steamid]);
if (activity is null) {
continue;
}
CBasePlayer@ plr = getPlayerById(steamid);
string name = plr !is null ? string(plr.pev.netname) : "\\disconnected\\";
if (connectedOnly and plr is null) {
continue;
}
float levelTime = g_Engine.time - 60; // substract some time for loading/downloading
float percentActive = activity.totalActivity / levelTime;
if (activity.totalActivity > levelTime || percentActive > 1.0f) {
percentActive = 1.0f;
}
bool wasActiveEnough = percentActive >= MIN_ACTIVE_TIME;
g_prev_map_activity[steamid] = int(percentActive*100);
if (wasActiveEnough) {
activePlayers.insertLast(SteamName(steamid, name));
}
}
return activePlayers;
}
array<SteamName> getAllPlayers() {
array<SteamName> activePlayers;
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
activePlayers.insertLast(SteamName(getPlayerUniqueId(plr), plr.pev.netname));
}
return activePlayers;
}
// logic here should be kept in sync with db_setup.py
void updatePlayerStats() {
g_nextmap_players.resize(0);
g_nextmap_players = getActivePlayers();
for (uint i = 0; i < g_nextmap_players.size(); i++) {
string steamid = g_nextmap_players[i].steamid;
PlayerMapHistory@ history = cast<PlayerMapHistory@>(g_player_map_history[steamid]);
if (!history.loaded) {
println("Can't update stats for " + steamid + " yet. Still loading.");
continue;
}
MapStat@ mapStat = history.stats.get(g_Engine.mapname);
mapStat.total_plays += 1;
mapStat.last_played = DateTime().ToUnixTimestamp();
}
}
void writeActivePlayerStats() {
if (!g_anyone_joined) {
// map was probably double-loaded. Re-use the previous map choice.
return;
}
print("Updating player map stats...");
g_prev_map_activity.clear();
g_previous_stats_map = g_Engine.mapname;
updatePlayerStats();
array<string>@ idKeys = g_player_activity.getKeys();
for (uint m = 0; m < idKeys.length(); m++) {
string steamid = idKeys[m];
PlayerMapHistory@ history = cast<PlayerMapHistory@>(g_player_map_history[steamid]);
if (history is null or !history.loaded) {
println("Can't write stats for " + steamid + " yet. Still loading.");
continue;
}
string path = getPlayerDbPath(steamid);
File@ f = g_FileSystem.OpenFile(path, OpenFile::WRITE);
if (f is null or !f.IsOpen()) {
println("Failed to open player map stats file: " + path + "\n");
return;
}
array<array<HashMapEntryMapStat>>@ buckets = @history.stats.buckets;
for (uint i = 0; i < buckets.size(); i++) {
for (uint k = 0; k < buckets[i].size(); k++) {
MapStat@ stat = buckets[i][k].value;
f.Write(buckets[i][k].key + " " + stat.last_played + " " + stat.total_plays + " " + stat.rating + "\n");
}
}
f.Close();
//println("Wrote player stat file: " + path);
}
println("DONE");
}
string formatLastPlayedTime(int seconds) {
int daysPerMonth = 30; // accurate enough for a "time since played" message
int minutes = (seconds / 60) % 60;
int hours = (seconds / (60*60)) % 24;
int days = (seconds / (60*60*24)) % daysPerMonth;
int months = (seconds / (60*60*24*daysPerMonth));
int years = (seconds / (60*60*24*365));
if (years > 1) {
if (months >= 6) {
years += 1;
}
return "" + years + " year" + (years != 1 ? "s" : "");
} else if (months > 3) {
if (days >= daysPerMonth/2) {
months += 1;
}
return "" + months + " month" + (months != 1 ? "s" : "");
} else if (days > 0) {
if (hours >= 12) {
days += 1;
}
days += months*daysPerMonth;
return "" + days + " day" + (days != 1 ? "s" : "");
} else if (hours > 0) {
if (minutes >= 30) {
hours += 1;
}
return "" + hours + " hour" + (hours != 1 ? "s" : "");
} else {
return "" + minutes + " minute" + (minutes != 1 ? "s" : "");
}
}
CBasePlayer@ getPlayerById(string targetId) {
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
string steamid = getPlayerUniqueId(plr);
if (steamid == targetId) {
return @plr;
}
}
return null;
}