mirror of https://github.com/dirtbags/moth.git
Make run_tanks use asyncore
This commit is contained in:
parent
1ba4f341f6
commit
a1faaed722
|
@ -3,23 +3,45 @@
|
||||||
import optparse
|
import optparse
|
||||||
import shutil
|
import shutil
|
||||||
import time
|
import time
|
||||||
|
import asyncore
|
||||||
|
import asynchat
|
||||||
from tanks import Pflanzarr
|
from tanks import Pflanzarr
|
||||||
|
|
||||||
T = 60*5
|
T = 60*5
|
||||||
MAX_HIST = 30
|
MAX_HIST = 30
|
||||||
HIST_STEP = 100
|
HIST_STEP = 100
|
||||||
|
key = 'tanks:::2bac5e912ff2e1ad559b177eb5aeecca'
|
||||||
|
|
||||||
parser = optparse.OptionParser('DATA_DIR easy|medium|hard MAX_TURNS')
|
class Flagger(asynchat.async_chat):
|
||||||
opts, args = parser.parse_args()
|
"""Use to connect to flagd and submit the current flag holder."""
|
||||||
if (len(args) != 3) or (args[1] not in ('easy', 'medium', 'hard')):
|
|
||||||
parser.error('Wrong number of arguments')
|
|
||||||
try:
|
|
||||||
turns = int(args[2])
|
|
||||||
except:
|
|
||||||
parser.error('Invalid number of turns')
|
|
||||||
|
|
||||||
while True:
|
def __init__(self, addr, auth):
|
||||||
start = time.time()
|
asynchat.async_chat.__init__(self)
|
||||||
|
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
self.connect((addr, 6668))
|
||||||
|
self.push(auth + '\n')
|
||||||
|
self.flag = None
|
||||||
|
|
||||||
|
def handle_read(self):
|
||||||
|
msg = self.recv(4096)
|
||||||
|
raise ValueError("Flagger died: %r" % msg)
|
||||||
|
|
||||||
|
def handle_error(self):
|
||||||
|
# If we lose the connection to flagd, nobody can score any
|
||||||
|
# points. Terminate everything.
|
||||||
|
asyncore.close_all()
|
||||||
|
asynchat.async_chat.handle_error(self)
|
||||||
|
|
||||||
|
def set_flag(self, team):
|
||||||
|
if team:
|
||||||
|
eteam = team
|
||||||
|
else:
|
||||||
|
eteam = ''
|
||||||
|
self.push(eteam + '\n')
|
||||||
|
self.flag = team
|
||||||
|
|
||||||
|
|
||||||
|
def run_tanks():
|
||||||
p = Pflanzarr.Pflanzarr(args[0], args[1])
|
p = Pflanzarr.Pflanzarr(args[0], args[1])
|
||||||
p.run(turns)
|
p.run(turns)
|
||||||
|
|
||||||
|
@ -38,6 +60,32 @@ while True:
|
||||||
if highest - MAX_HIST > num and not (num % HIST_STEP == 0):
|
if highest - MAX_HIST > num and not (num % HIST_STEP == 0):
|
||||||
shutil.rmtree(os.path.join(path, num))
|
shutil.rmtree(os.path.join(path, num))
|
||||||
|
|
||||||
diff = time.time() - start
|
try:
|
||||||
if diff - T > 0:
|
winner = open('/var/lib/tanks/winner').read().strip()
|
||||||
time.sleep( diff - T )
|
except:
|
||||||
|
winner = None
|
||||||
|
flagger.set_flag(winner)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = optparse.OptionParser('DATA_DIR easy|medium|hard MAX_TURNS')
|
||||||
|
opts, args = parser.parse_args()
|
||||||
|
if (len(args) != 3) or (args[1] not in ('easy', 'medium', 'hard')):
|
||||||
|
parser.error('Wrong number of arguments')
|
||||||
|
try:
|
||||||
|
turns = int(args[2])
|
||||||
|
except:
|
||||||
|
parser.error('Invalid number of turns')
|
||||||
|
|
||||||
|
|
||||||
|
flagger = Flagger('localhost', key)
|
||||||
|
lastrun = 0
|
||||||
|
while True:
|
||||||
|
asyncore.loop(60, count=1)
|
||||||
|
now = time.time()
|
||||||
|
if now - lastrun >= 60:
|
||||||
|
run_tanks()
|
||||||
|
lastrun = now
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in New Issue