This repository was archived by the owner on Jun 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathteamstats.pl
More file actions
executable file
·740 lines (595 loc) · 19.1 KB
/
teamstats.pl
File metadata and controls
executable file
·740 lines (595 loc) · 19.1 KB
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
#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;
use experimental 'signatures', 'postderef';
use lib 'lib';
use Path::Tiny;
use JSON::MaybeXS;
use Time::Moment;
use Try::Tiny;
use Template;
use Getopt::Long;
use Data::Dumper;
use List::Util qw(first);
use TeamStats::Config;
#=== globals ==================================================================
my $js = JSON::MaybeXS->new(pretty => 1, utf8 => 1);
my $cfg2 = TeamStats::Config->instance;
my $cfg = $cfg2->config;
#=== command line options =====================================================
my $cmd_retrieve = 1; # retrieve remote logs
my $cmd_debug = 0; # debug mode
#==============================================================================
# FUNCTIONS
#==============================================================================
# Convert time as saved in DCSS logfiles to Unix epoch time. DCSS
# "start"/"end" time specification is confusing: it looks like ISO 8601, but
# the T delimiter is missing, the 'S' at the end means who-knows-what and the
# month field is 0-based.
sub to_moment
{
my $tm = shift;
$tm =~ /^(\d{4})(\d{2})(\d{2})(\d{6})S$/;
$tm = sprintf('%04d%02d%02dT%sZ', $1, $2+1, $3, $4);
return Time::Moment->from_string($tm);
}
# format duration to human readable format
sub format_duration
{
my $secs = shift;
use integer;
my $days = $secs / 86400;
$secs %= 86400;
my $hours = $secs / 3600;
$secs %= 3600;
my $minutes = $secs / 60;
$secs %= 60;
my $re = '';
if($days) {
$re = sprintf('%d, ', $days);
}
$re .= sprintf('%02d:%02d:%02d', $hours, $minutes, $secs);
return $re;
}
# morgue url
sub morgue_url
{
my $row = shift;
my $server = $row->{server};
my $player = $row->{name};
# if dump url is not defined, do nothing
return if(!exists $cfg->{servers}{$server}{morgue});
my $template = $cfg->{servers}{$server}{morgue};
# get formatted time
return if !exists $row->{end};
my $tm = to_moment($row->{end})->strftime('%Y%m%d-%H%M%S');
# perform token replacement
$template =~ $cfg2->token_replace($template);
$template =~ s/%u/$player/g;
$template =~ s/%d/$tm/g;
# save and finsh
$row->{dumpurl} = $template;
}
# in-progress dump
sub server_url
{
my ($server, $item, $player) = @_;
# return undef if dump URL is not defined
return undef if !exists $cfg->{servers}{$server}{$item};
my $template = $cfg->{servers}{$server}{$item};
# perform token replacement
$template = $cfg2->token_replace($template);
$template =~ s/%u/$player/g;
# finish
return $template;
}
# return the name of the god for which the player reached the 6* piety, but only
# if it is the first deity worshipped; in all other cases return undef
sub check_god_maxpiety ($data, $game)
{
# get the first god in the game
my $first_god = first {
$_->{name} eq $game->{name}
&& $_->{server} eq $game->{server}
&& $_->{start_epoch} == $game->{start_epoch}
&& $_->{god}
} $data->{milestones}->@*;
# get the first god.maxpiety milestone
my $max_piety = first {
$_->{start_epoch} == $game->{start_epoch}
&& $_->{type} eq 'god.maxpiety'
} $data->{milestones}->@*;
# finish
if(
$first_god && $max_piety
&& $first_god->{god} eq $max_piety->{god}
) {
return $max_piety->{god}
} else {
return undef;
}
}
# check if given god was the only god worshipped in the game; this is the
# requirement for a game to count as won with either Xom or Gozag
sub check_god_exclusivity
{
my $data = shift;
my $g = shift;
my $god = $g->{god};
if(
grep {
$_->{start_epoch} == $g->{start_epoch}
&& $_->{type} =~ /^god\./
&& ($_->{god} && $_->{god} ne $god)
} @{$data->{milestones}}
) {
return 0;
} else {
return 1;
}
}
# check if given game is atheist
sub check_atheist
{
my $data = shift;
my $g = shift;
# some shortcuts can be made
return 0 if
$g->{cls} eq 'Berserker'
|| $g->{cls} eq 'Chaos Knight'
|| $g->{cls} eq 'Abyssal Knight';
return 1 if $g->{race} eq 'Demigod';
if(
grep {
$_->{start_epoch} == $g->{start_epoch}
&& $g->{type}
&& $g->{type} =~ /^god\./
} @{$data->{milestones}}
) {
return 0
} else {
return 1
}
}
#==============================================================================
# MAIN
#==============================================================================
#=== command-line processing ==================================================
GetOptions(
'retrieve!' => \$cmd_retrieve,
'debug!' => \$cmd_debug
);
#=== load configuration =======================================================
say 'Configured clans: ', join(', ', $cfg2->clans);
say 'Configured players: ', join(', ', $cfg2->players);
#=== state initialization/loading ============================================
my $state_file = path($cfg->{state});
my $state = {};
if(-f $state_file) {
print 'State file exists, loading ... ';
$state = $js->decode($state_file->slurp_raw());
say 'done'
} else {
$state->{games} = [];
$state->{milestones} = [];
}
#=== loading of logfiles =====================================================
my $logdir = path($cfg->{logdir});
foreach my $server (keys %{$cfg->{servers}}) {
say "Processing $server";
try {
foreach my $log (qw(log milestones)) {
# get URL and localfile
my $url = $cfg->{servers}{$server}{$log}{url};
my $file = $logdir->child(
$cfg2->token_replace($cfg->{servers}{$server}{$log}{file})
);
# get our last position in the file (or 0 if none)
my $fpos = $state->{servers}{$server}{$log}{fpos} // 0;
# retrieve new data from URL
if($cmd_retrieve) {
my $r = system(sprintf($cfg->{wget}, $file, $url));
die "Failed to get $url" if $r;
}
# open the file and seek into it
open(my $fh, '<', $file) or die 'Failed to open ' . $file;
seek($fh, $fpos, 0) if $fpos;
# read the new data
my ($count_total, $count_selected) = (0,0);
while(my $line = <$fh>) {
chomp $line;
my %row;
# following regex splits the line by ':' delimiter, but ignores '::',
# which works as an escape to denote ':' in value
foreach my $fv (split(/(?<!:):(?!:)/, $line)) {
$fv =~ s/::/:/g;
my @fv = split(/=/, $fv);
$row{$fv[0]} = $fv[1] if $fv[0];
}
# rudimentary detection of malformed lines; 'v' field is the first one
# so if some mangling happens, this is very likely to go missing
next unless exists $row{v};
$count_total++;
# check for team members, ignore all other entries
#next if !(grep { $_ eq $row{name} } @{$cfg->{match}{members}});
next if !exists $row{name} || !$row{name};
next if !exists $cfg2->plr_to_clan->{lc $row{name}};
# add clan id to every row
$row{clan} = $cfg2->plr_to_clan->{lc $row{name}};
# convert dates into epoch/human readable format and match time bracket
my $tm_start = to_moment($row{start});
$row{start_epoch} = $tm_start->epoch;
next if $tm_start < $cfg2->start;
$row{start_fmt} = $tm_start->strftime('%Y-%m-%d %H:%M:%S');
if($log eq 'log') {
my $tm_end = to_moment($row{end});
last if $tm_end >= $cfg2->end;
$row{end_epoch} = $tm_end->epoch;
$row{end_fmt} = $tm_end->strftime('%Y-%m-%d %H:%M:%S');
$row{dur_fmt} = format_duration($row{dur});
} else {
my $tm_time = to_moment($row{time});
next if $tm_time < $cfg2->start;
last if $tm_time >= $cfg2->end;
$row{time_epoch} = to_moment($row{time})->epoch;
$row{milestone} =~ s/.$//;
}
# save server id
$row{server} = $server;
# get dump url
morgue_url(\%row);
$count_selected++;
# store into state
if($log eq 'log') {
push(@{$state->{games}}, \%row);
} else {
push(@{$state->{milestones}}, \%row);
}
}
printf(
" %d new lines (%d matched) in %s/%s\n",
$count_total, $count_selected, $server, $log
);
# finish
$fpos = tell($fh);
$state->{servers}{$server}{$log}{fpos} = $fpos;
close($fh);
}
};
}
#=== processing data ==========================================================
my %data = (
cfg => $cfg,
milestones => $state->{milestones}
);
my $games = $state->{games};
my $milestones = $state->{milestones};
#--- resolve "now" moment ----------------------------------------------------
my $now = $cfg2->now->epoch;
my $end_of_tourney = $cfg2->end->epoch;
$now = $end_of_tourney if $now > $end_of_tourney;
#--- add 'time_from_now' field to milestones
foreach my $ms (@$milestones) {
$ms->{time_from_now} = $now - $ms->{time_epoch};
$ms->{time_from_now_fmt} = format_duration($ms->{time_from_now});
}
#--- list of games indexed by start time -------------------------------------
foreach my $g (@$games) {
$data{games}{by_start}{$g->{start_epoch}} = $g;
}
#--- list of all clan games sorted by end time --------------------------------
foreach my $clan ($cfg2->clans) {
$data{clans}{$clan}{games}{all} = [
sort { $b->{end_epoch} <=> $a->{end_epoch} }
grep { $_->{clan} eq $clan } @$games
];
}
#--- list of won clan games --------------------------------------------------
foreach my $clan ($cfg2->clans) {
my @wins = sort {
$a->{end_epoch} <=> $b->{end_epoch}
} grep {
$_->{clan} eq $clan && $_->{ktyp} eq 'winning'
} @$games;
my @wins_allrune = sort {
$a->{end_epoch} <=> $b->{end_epoch}
} grep {
$_->{urune} == 15
} @wins;
$data{clans}{$clan}{games}{wins} = \@wins;
$data{clans}{$clan}{games}{wins_allrune} = \@wins_allrune;
}
#--- clan combos --------------------------------------------------------------
foreach my $clan ($cfg2->clans) {
my %combos;
foreach my $row (@{$data{clans}{$clan}{games}{wins}}) {
$combos{ $row->{char} }++;
}
$data{clans}{$clan}{combos} = \%combos;
}
#--- clan ghost kills ---------------------------------------------------------
foreach my $clan ($cfg2->clans) {
$data{clans}{$clan}{gkills} = grep {
$_->{clan} eq $clan
&& $_->{type} eq 'ghost'
} @$milestones;
}
#--- by-player stats ----------------------------------------------------------
foreach my $player ($cfg2->players) {
# all games
$data{players}{$player}{games}{all} = [
sort {
$a->{end_epoch} <=> $b->{end_epoch}
} grep {
$_->{name} eq $player
} @$games
];
# won games
$data{players}{$player}{games}{wins} = [
sort {
$a->{end_epoch} <=> $b->{end_epoch}
} grep {
$_->{ktyp} eq 'winning'
} @{$data{players}{$player}{games}{all}}
];
# sort per-player game lists by score
$data{players}{$player}{games}{by_score} = [
sort {
$b->{sc} <=> $a->{sc}
} @{$data{players}{$player}{games}{all}}
];
# milestones
my @milestones_by_player = sort {
$a->{time_epoch} <=> $b->{time_epoch}
} grep {
$_->{name} eq $player
} @$milestones;
# find all servers player has started a game on; we record number of games
# player has started on each server and then create a sorted list; sorted
# list is useful in the web where we can list most used servers first
foreach my $ms (@milestones_by_player) {
next if $ms->{type} ne 'begin';
$data{players}{$player}{servers}{$ms->{server}}++;
}
$data{players}{$player}{servers} = [ sort {
$data{players}{$player}{servers}{$b} cmp $data{players}{$player}{servers}{$a}
} keys %{$data{players}{$player}{servers}} ];
foreach my $server (@{$data{players}{$player}{servers}}) {
$data{players}{$player}{dumps}{$server}
= server_url($server, 'dump', $player);
$data{players}{$player}{watch}{$server}
= server_url($server, 'watch', $player);
}
}
#--- last milestone per (player, server)
foreach my $ms (sort { $a->{time_epoch} <=> $b->{time_epoch} } @$milestones) {
$data{players}{$ms->{name}}{last_milestones}{$ms->{server}} = $ms;
}
#--- in progress games
# to get games in progress, we scan the last milestone per (player, server)
# found in the previous step and see if there's corresponding game in the games
# log; if there isn't, the milestone belongs to an unfinished, on-going game
#
# The result of this code is stored in "clan.in_progress" and
# "player.PLR.in_progress". The stored entities are references to the last
# milestone entries of ongoing games.
foreach my $player ($cfg2->players) {
# ignore players without any recorded milestones
next if !exists $data{players}{$player}{last_milestones};
# get player's clan association
my $clan = $cfg2->plr_to_clan->{lc $player};
# initialize in progress lists
$data{players}{$player}{in_progress} = [];
$data{clans}{$clan}{in_progress} = []
if !exists $data{clans}{$clan}{in_progress};
# iterate over servers player has milestones on
foreach my $srv (keys %{$data{players}{$player}{last_milestones}}) {
my $ms = $data{players}{$player}{last_milestones}{$srv};
if(!exists $data{games}{by_start}{$ms->{start_epoch}}) {
push(@{$data{clans}{$clan}{in_progress}}, $ms);
push(@{$data{players}{$player}{in_progress}}, $ms);
}
}
# sort current player's games in progress list
$data{players}{$player}{in_progress} = [
sort {
$a->{time_from_now} <=> $b->{time_from_now}
} @{$data{players}{$player}{in_progress}}
];
}
# sort all clans' games in progress list
foreach my $clan ($cfg2->clans) {
$data{clans}{$clan}{in_progress} = [
sort {
$a->{time_from_now} <=> $b->{time_from_now}
} @{$data{clans}{$clan}{in_progress}}
]
}
#--- best clan games ----------------------------------------------------------
foreach my $clan ($cfg2->clans) {
# best turncount
$data{clans}{$clan}{games}{wins_by_turncount} = [
sort {
$a->{turn} <=> $b->{turn}
} grep {
$_->{clan} eq $clan && $_->{ktyp} eq 'winning'
} @$games
];
# best realtime
$data{clans}{$clan}{games}{wins_by_realtime} = [
sort {
$a->{dur} <=> $b->{dur}
} grep {
$_->{clan} eq $clan && $_->{ktyp} eq 'winning'
} @$games
];
# highest score
$data{clans}{$clan}{games}{by_score} = [
sort {
$b->{sc} <=> $a->{sc}
} grep {
$_->{clan} eq $clan
} @$games
];
# lowest xl win
$data{clans}{$clan}{games}{wins_by_xl} = [
sort {
if($a->{xl} == $b->{xl}) {
$a->{turn} <=> $b->{turn}
} else {
$a->{xl} <=> $b->{xl}
}
}
grep {
$_->{clan} eq $clan && $_->{ktyp} eq 'winning'
} @$games
];
# lowest xl rune
$data{clans}{$clan}{games}{by_xlrune} = [
sort {
if($a->{xl} == $b->{xl}) {
$a->{turn} <=> $b->{turn}
} else {
$a->{xl} <=> $b->{xl}
}
} grep {
$_->{clan} eq $clan
&& $_->{type} eq 'rune'
&& $_->{urune} == 1
} @$milestones
];
}
#--- runes -------------------------------------------------------------------
# runes collection status, both for individual players and clan as a whole
foreach my $ms (@$milestones) {
next if $ms->{type} ne 'rune';
$ms->{milestone} =~ /\b(\w+)\srune\b/;
my $rune = $1;
my $clan = $cfg2->plr_to_clan->{lc $ms->{name}};
$data{clans}{$clan}{runes}{$rune}++;
$data{players}{$ms->{name}}{runes}{$rune}++;
}
#--- god championed & won ------------------------------------------------------
foreach my $g (@$games) {
my $clan = $cfg2->plr_to_clan->{lc $g->{name}};
my $champion_of = check_god_maxpiety(\%data, $g);
my $god = $g->{god};
# game god was championed in the game
if($champion_of) {
$data{clans}{$clan}{godpiety}{$champion_of}++;
$data{players}{$g->{name}}{godpiety}{$champion_of}++;
# if the game was won AND the god championed is the same as the god the game
# was won with mark this as 'god won'
if($g->{ktyp} eq 'winning' && $champion_of eq $god) {
$data{clans}{$clan}{godwin}{$god}++;
$data{players}{$g->{name}}{godwin}{$god}++;
}
}
}
#--- god won (special cases) ---------------------------------------------------
# Xom and Gozag are won only when the player never worships any other god.
foreach my $g (@$games) {
next if $g->{ktyp} ne 'winning';
my $god = $g->{god};
my $clan = $cfg2->plr_to_clan->{lc $g->{name}};
if(!$god) {
if(check_atheist(\%data, $g)) {
$data{clans}{$clan}{godwin}{'No god'}++;
$data{players}{$g->{name}}{godwin}{'No god'}++;
}
} elsif($god eq 'Xom' || $god eq 'Gozag' || $god eq 'Ignis') {
if(check_god_exclusivity(\%data, $g)) {
$data{clans}{$clan}{godwin}{$god}++;
$data{players}{$g->{name}}{godwin}{$god}++;
}
}
}
#--- uniques -----------------------------------------------------------------
# uniques harvest status, both for individual players and clan as a whole
foreach my $clan ($cfg2->clans) {
$data{clans}{$clan}{uniques} = {};
}
foreach my $player ($cfg2->players) {
$data{players}{$player}{uniques} = {};
}
foreach my $ms (@$milestones) {
next if $ms->{type} ne 'uniq';
my $clan = $cfg2->plr_to_clan->{lc $ms->{name}};
my $msg = $ms->{milestone};
$msg =~ s/\d+-headed\s//;
$msg =~ s/Royal Jelly/royal jelly/;
$msg =~ /killed\s(.*)$/;
my $unique = $1;
next if !$unique;
# mapping unique names
$unique = $cfg->{game}{uniquesmap}{$unique} if (
exists $cfg->{game}{uniquesmap}
&& exists $cfg->{game}{uniquesmap}{$unique}
);
$data{clans}{$clan}{uniques}{$unique}++;
$data{players}{$ms->{name}}{uniques}{$unique}++;
}
#--- generation time, phase ---------------------------------------------------
$data{gentime} = $cfg2->now->strftime('%Y-%m-%d %H:%M:%S');
$data{phase} = $cfg2->phase;
#--- countdown
# format the actual countdown string for server-side rendered countdown and
# create list of countdown targets (in epoch format) for the front-side
# countdown JavaScript code
my @count_to = $cfg2->count_to;
if($data{phase} ne 'after') {
use integer;
my $s = $cfg2->now->delta_seconds($count_to[0]);
my $d = $s / 86400; $s %= 86400;
my $h = $s / 3600; $s %= 3600;
my $m = $s / 60; $s %= 60;
if($d) {
$data{countdown} = sprintf('%dd, %02d:%02d:%02d', $d, $h, $m, $s);
} else {
$data{countdown} = sprintf('%02d:%02d:%02d', $h, $m, $s);
}
} else {
$data{countdown} = sprintf('%02d:%02d:%02d', 0, 0, 0);
}
$data{count_to} = join(',', map { $_->epoch } @count_to);
#=== generate HTML pages ======================================================
my $tt = Template->new(
'OUTPUT_PATH' => $cfg2->htmldir,
'INCLUDE_PATH' => 'templates',
'RELATIVE' => 1
);
$tt->process(
'index.tt',
\%data,
'index.html'
) or die;
foreach my $clan ($cfg2->clans) {
$data{clan} = $clan;
$data{clanname} = $cfg->{clans}{$clan}{name};
$tt->process(
'clan.tt',
\%data,
"clan-$clan.html"
) or die;
$tt->process(
'games.tt',
\%data,
"games-$clan.html"
) or die;
}
foreach my $player ($cfg2->players) {
$data{player} = $player;
$data{clan} = $cfg2->plr_to_clan->{lc $player};
$data{clanname} = $cfg->{clans}{$data{clan}}{name};
$tt->process(
'player.tt',
\%data,
"$player.html"
) or die;
}
#=== save state ===============================================================
say "Saving state";
my $state_new = $state_file->sibling($state_file->basename . ".$$");
$state_new->spew_raw($js->encode($state));
$state_new->move($state_file);