-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattle.py
71 lines (60 loc) · 1.65 KB
/
battle.py
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
from collections import namedtuple
import time
import re
import gladiator
Game = namedtuple('Game', ['host', 'number'])
class Match(object):
def __init__(self, c1, c2, game, type=gladiator.GitGladiator):
self.competitors = c1, c2
self.game = game
self.status = 'new'
self.winner = None
self.log = None
self.type = type
self.make_gladiators()
def make_gladiators(self):
self.gladiators = [i.make_gladiator() for i in self.competitors]
def prepare(self):
self.status = 'preparing'
for i in range(2):
if not self.gladiators[i].prepare():
self.winner = 1-i
return False
self.status = 'building'
for i in range(2):
if not self.gladiators[i].build():
self.winner = 1-i
return False
return True
def run(self):
self.status = 'joining'
for i in range(2):
#give each client sufficient time to connect
time.sleep(2)
if not self.gladiators[i].run(self.game):
self.winner = 1-i
return False
self.status = 'playing'
while self.winner is None:
time.sleep(1)
for i in range(2):
g = self.gladiators[i]
if self.gladiators[i].poll():
if g.crashed:
self.winner = 1-i
break
else:
self.log = g.log
self.check_winner()
break
for g in self.gladiators:
g.terminate()
return self.winner
def check_winner(self):
log = open(self.log, 'r').read()
match = re.search("\"game-winner\" (\d+) \"[^\"]+\" (\d+)", log)
if match:
self.winner = int(match.groups()[1])
else:
self.winner = -1
return self.winner