moth/bin/run-tanks

62 lines
1.8 KiB
Python
Executable File

#! /usr/bin/python
import optparse
import os
import shutil
import socket
import time
from ctf import pointscli, teams, paths
from tanks import Pflanzarr
running = True
def run_tanks(basedir, turns, barren_points=True):
try:
p = Pflanzarr.Pflanzarr(basedir)
p.run(turns)
winner = p.winner or teams.house
except Pflanzarr.NotEnoughPlayers:
if not barren_points:
return
winner = teams.house
pointscli.award('tanks', winner, 1)
winnerFile = open(os.path.join(basedir, 'winner'),'w')
winnerFile.write(winner)
winnerFile.close()
# Fake being a flag, so the most recent winner shows up on the
# scoreboard.
try:
open(os.path.join(paths.VAR, 'flags', 'tanks'), 'w').write(winner)
except IOError:
pass
def main():
parser = optparse.OptionParser('%prog [options] DATA_DIR')
parser.add_option('-1', '--once',
action='store_true', dest='once',
help='Run only once')
parser.add_option('-b', '--no-barren-points',
action='store_false', dest='barren', default=True,
help="Don't award points if there aren't enough players")
parser.add_option('-t', '--max-turns',
type='int', dest='turns', default=500,
help='Maximum number of turns per round')
parser.add_option('-s', '--sleep-time',
type='int', dest='sleep', default=60,
help='Wait SLEEP seconds between turns (default %default)')
opts, args = parser.parse_args()
if (len(args) != 1):
parser.error('Wrong number of arguments')
while running:
run_tanks(args[0], opts.turns, opts.barren)
if opts.once:
break
time.sleep(opts.sleep)
if __name__ == '__main__':
main()