-
Notifications
You must be signed in to change notification settings - Fork 0
/
tnnt-scoreboard.pl
executable file
·145 lines (109 loc) · 3.05 KB
/
tnnt-scoreboard.pl
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
#!/usr/bin/env perl
#=============================================================================
# THE NOVEMBER NETHACK TOURNAMENT
# """""""""""""""""""""""""""""""
# Scoreboard generator.
#=============================================================================
use v5.10;
use FindBin qw($Bin);
use lib "$Bin";
use JSON;
use Try::Tiny;
use Moo;
use TNNT::Cmdline;
use TNNT::Config;
use TNNT::Source;
use TNNT::ClanList;
use TNNT::Score;
use TNNT::Template;
#use Data::Dumper;
$| = 1;
#--- process command-line arguments
my $cmd;
try {
$cmd = TNNT::Cmdline->instance();
} catch {
print STDERR "Invalid command-line arguments\n";
exit(1);
};
#--- check for & create a lock file
if(-f '/tmp/tnnt-scoreboard.lock') {
open(my $fh_lock, '<', '/tmp/tnnt-scoreboard.lock');
die "lockfile /tmp/tnnt-scoreboard.lock exists, but we cannot open it" if !$fh_lock;
my @lines;
while(my $l = <$fh_lock>) {
chomp($l);
push @lines, $l;
}
close($fh_lock) if $fh_lock;
my $pid = int($lines[0]);
if (kill 0, $pid) {
die "tnnt-scoreboard: Another instance running, aborting\n";
} else {
warn "lockfile /tmp/tnnt-scoreboard.lock found, but process $pid doesn't exist\n";
unlink '/tmp/tnnt-scoreboard.lock';
}
}
open(my $fh_lock, '>', '/tmp/tnnt-scoreboard.lock')
or die "tnnt-scoreboard: Failed to create a lock file, aborting\n";
print $fh_lock $$;
close($fh_lock);
#--- load configuration file
my $cfg = TNNT::Config->instance(config_file => "$Bin/config.json");
$cfg->config();
#--- load clan list
my $clans = TNNT::ClanList->instance();
#--- initialize sources
my @sources;
$cfg->iter_sources(sub {
push(@sources, TNNT::Source->new(@_));
});
#--- perform reading of the xlogfiles
my $score = TNNT::Score->new();
for my $src (@sources) {
$src->read(sub { $score->add_game($_[0]) });
}
#--- compile the scoreboard
$score->process();
my $data = $score->export();
#--- record tournament phase
my $phase = $cfg->time_phase();
if($phase == 0) {
$data->{'aux'}{'phase'} = 'during';
} elsif($phase == -1) {
$data->{'aux'}{'phase'} = 'before';
} elsif($phase == 1) {
$data->{'aux'}{'phase'} = 'after';
}
#--- output JSON data
if(defined (my $out = $cmd->json())) {
my $js = JSON->new()->pretty(1);
if($out eq '') {
print $js->encode($data);
} else {
open(my $fh, '>', $out) or die "Cannot open file '$out'";
print $fh $js->encode($data);
close($fh);
}
}
#--- process the templates
if($cmd->html()) {
my $t = TNNT::Template->new(data => $data);
$t->process();
$t->process('players', 'player', $data->{'players'}{'ordered'});
$t->process('clans', 'clan', $data->{'clans'}{'ordered'});
}
#--- write out merged xlogfile
if($cmd->coalesce()) {
my $file_fin = $cmd->coalesce();
my $file_tmp = $file_fin . ".$$";
open(my $fh, '>', $file_tmp) or die "Cannot open file '$file_tmp'";
$score->export_xlogfile(sub {
print $fh $_[0], "\n";
});
close($fh);
rename($file_tmp, $file_fin)
or die "Failed to rename a file ($file_tmp -> $file_fin)";
}
#--- clear the lock file
unlink('/tmp/tnnt-scoreboard.lock');