-
Notifications
You must be signed in to change notification settings - Fork 1
/
fight-club-slave.pl
executable file
·305 lines (231 loc) · 5.92 KB
/
fight-club-slave.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
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
#! /usr/bin/perl
use strict;
use warnings;
use Cwd;
use POSIX qw/setsid/;
use Fcntl qw/SEEK_SET/;
use IO::Pty::Easy;
use CSplat::Termcast;
use Getopt::Long;
use threads;
use threads::shared;
my @queued_fights : shared;
my @queued_cancels : shared;
my @bad_requests;
my @fight_results;
my $current_fight;
my $MIN_DELAY = 25;
my $MAX_FIGHT_TIME_SECONDS = 10 * 60;
local $SIG{CHLD} = sub { };
# Force canonical term size.
$ENV{LINES} = 24;
$ENV{COLUMNS} = 80;
my %opt;
GetOptions(\%opt, 'local', 'req=s');
my $CRAWL_HOME = $ENV{CRAWL_HOME} or die "CRAWL_HOME must be set!\n";
my $ARENA_REQ_FILE = $opt{req} or die "request filename not specified";
my $ARENA_RESULT = 'arena.result';
open my $AR, '<', $ARENA_REQ_FILE or die "Cannot open $ARENA_REQ_FILE: $!";
my $TV = CSplat::Termcast->new(name => 'FightClub',
passfile => 'fightclub.pwd',
local => $opt{local});
wait_for_requests();
sub wait_for_requests {
my $termc = threads->new(\&arena_tv);
$termc->detach;
while (1) {
sleep 1;
my $pos = tell($AR);
my $req = <$AR>;
# If we don't have a complete line, seek back to where we started
# and retry.
if (!defined($req) || $req !~ /\n$/) {
seek($AR, $pos, SEEK_SET);
next;
}
s/\s+$//, s/^\s+// for $req;
if ($req =~ /^(\S+): (.*)/) {
run_arena($1, $2);
}
}
}
sub run_arena {
my ($who, $what) = @_;
if (lc($what) !~ /\bcancel\b/) {
push @queued_fights, "$who: $what";
}
else {
push @queued_cancels, $what;
}
}
sub show_errors {
return unless @bad_requests;
$TV->write("\e[1;31mErrors\e[0m\r\n");
for my $err (@bad_requests) {
$TV->write("$err\r\n");
}
$TV->write("\r\n");
@bad_requests = ();
}
sub show_queue {
my @fights = @queued_fights;
unless (@fights) {
$TV->title("[waiting]");
return;
}
@fights = @fights[0 .. 4] if @fights > 5;
$TV->write("\e[1;37mComing up\e[0m\r\n");
my $first = 0;
for my $fight (@fights) {
$TV->write("$fight\r\n");
}
$TV->write("\r\n");
}
sub show_results {
my $max = 10 - @queued_fights;
$max = 5 if $max < 5;
my @results = reverse @fight_results;
return unless @results;
@results = @results[0 .. ($max - 1)] if @results > $max;
$TV->write("\e[1;32mPrevious Fights\e[0m\r\n");
for my $res (@results) {
$TV->write(sprintf("%-7s %s {%s}\r\n",
$res->[1], $res->[0], $res->[2]));
}
$TV->write("\r\n");
}
sub announce_arena {
$TV->clear();
$TV->write("\e[1H\e[1;34mFight Club\e[0m\r\n");
$TV->write("Use !fight on ##crawl to make a request\r\n\r\n");
show_errors();
show_queue();
show_results();
}
sub arena_tv {
announce_arena();
while (1) {
sleep 1;
next unless @queued_fights || @queued_cancels;
announce_arena();
handle_cancels();
my $fight = shift @queued_fights;
next unless $fight;
play_fight($fight);
$TV->reset();
announce_arena();
sleep 1 if @bad_requests;
}
}
sub strip_space {
my $text = shift;
s/^\s+//, s/\s+$//, s/\s+/ /g for $text;
$text
}
sub handle_cancels {
return unless @queued_cancels;
my $cancel_current;
my @cancels = @queued_cancels;
@queued_cancels = ();
for my $cancel (@cancels) {
if ($cancel eq 'cancel') {
@queued_fights = ();
return 1;
}
$cancel =~ s/\bcancel\b//g;
my $stripped = lc(strip_space($cancel));
@queued_fights = grep(lc(strip_space($_)) ne $stripped, @queued_fights);
$cancel_current = 1 if lc(strip_space($current_fight)) eq $stripped;
}
$cancel_current
}
sub strip_junk {
my $text = shift;
for ($text) {
s/\bno_summons\b//g;
s/\s+/ /g;
}
$text
}
sub get_qualifiers {
my $term = shift;
my @quals;
if ($term =~ /\b(no_summons)\b/) {
push @quals, $1;
}
@quals
}
sub record_arena_result {
my $who = shift;
return unless -f $ARENA_RESULT;
open my $inf, '<', $ARENA_RESULT or return;
my $fight_spec = <$inf>;
if ($fight_spec =~ /^err: (.*)$/) {
push @bad_requests, $1;
return;
}
my $line = <$inf>;
return unless $line && $line =~ /\n$/;
chomp $line;
if ($line =~ /^(\d+)-(\d+)$/) {
my ($a, $b) = ($1, $2);
my (@teams) = map(strip_space($_),
split(/ v /, strip_junk($current_fight)));
my @qualifiers = get_qualifiers($current_fight);
return unless @teams == 2;
my $name = join(" v ", $b > $a ? reverse(@teams) : @teams);
my $result = $b > $a ? "$b - $a" : "$a - $b";
$name = "$name (" . join(", ", @qualifiers) . ")" if @qualifiers;
push @fight_results, [ $name, $result, $who ];
}
}
sub fight_ok {
my ($who, $what) = @_;
if ($what =~ /delay:0/i && $what =~ /test\s+spawner/i) {
push @bad_requests, "$what: no test spawners with delay:0, kthx.";
return undef;
}
1
}
sub play_fight {
my $fight = shift;
my ($who, $what) = $fight =~ /^(\S+): (.*)/;
return unless fight_ok($who, $what);
$current_fight = $what;
# Get rid of low-delay CPU-wastage.
$what =~ s/delay:(\d+)/ "delay:" . ($1 < $MIN_DELAY ? $MIN_DELAY : $1) /ge;
$TV->clear();
$TV->title("$current_fight");
my $pty = IO::Pty::Easy->new;
my $home_dir = getcwd();
chdir "$CRAWL_HOME/source";
unlink $ARENA_RESULT;
$pty->spawn("./crawl.build", "-arena", $what);
my $fight_started_at = time;
my $fight_too_long;
while ($pty->is_active) {
if ((time - $fight_started_at) > $MAX_FIGHT_TIME_SECONDS) {
$fight_too_long = 1;
last;
}
if (handle_cancels()) {
kill 9 => $pty->pid() if $pty->pid();
last;
}
my $read = $pty->read(1);
next unless defined $read;
last if length($read) == 0;
$TV->write($read);
}
kill 9 => $pty->pid() if $pty->pid();
$pty->close();
if ($fight_too_long) {
$TV->reset();
$TV->write("\"$what\" exceeded the time limit of ${MAX_FIGHT_TIME_SECONDS}s");
sleep 4;
}
record_arena_result($who);
# XXX: Don't really need this.
chdir $home_dir;
undef $current_fight;
}