-
Notifications
You must be signed in to change notification settings - Fork 17
/
gamerunner.py
executable file
·187 lines (153 loc) · 5.34 KB
/
gamerunner.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
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
#!/usr/bin/env python2
import os
import sys
import os.path as op
path = op.dirname(op.dirname(op.realpath('__file__')))
print (path)
sys.path.append(path)
from subprocess import Popen
import argparse
from src.game.game import Game
from src.server.server import MMServer
import src.misc_constants as miscConstants
import src.game.game_constants as gameConstants
FNULL = open(os.devnull, 'w')
parameters = None
client_list = list()
def launch_clients():
if parameters.client:
numberOfClients = len(parameters.client)
for client in parameters.client:
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'clients/', client)
if os.name == "nt":
path = path.replace("\\","/")
launch_client(path)
else:
numberOfClients = 0
for x in range(numberOfClients, parameters.teams):
path = os.path.join(os.getcwd(), parameters.defaultClient)
if os.name == "nt":
path = path.replace("\\","/")
launch_client(path)
def launch_client(client, port=None):
c = Client_program(client, port)
client_list.append(c)
c.run()
def launch_client_test_game(client, port):
launch_client(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, client), port)
launch_client(os.path.join(os.getcwd(), parameters.defaultClient), port)
def parse_args():
parser = argparse.ArgumentParser(
description="Launch the server with 2 clients which "
+ "connect to it.")
parser.add_argument(
"-u", "--port",
help="Specify the port the server runs on. " +
"Default: {0}".format(miscConstants.port),
default=miscConstants.port,
type=int)
parser.add_argument(
"-l", "--log",
help="Specify where the game log will be written to. " +
"For example, ./gamerunner.py --log LOG.out. Default: {0}".
format(miscConstants.logFile),
default=miscConstants.logFile)
parser.add_argument(
"-c", "--client",
help="Specifies one or two clients to run. " +
"Example: ./gamerunner.py -c default -c python ",
action="append")
parser.add_argument(
"-d", "--defaultClient",
help="The default client to use when others aren't specified."
"Default: {0}".format(miscConstants.defaultClient),
default=os.path.join(*miscConstants.defaultClient.split("/")))
args = parser.parse_args()
args.teams = gameConstants.numPlayers
if args.teams != 2:
sys.stdout.write(parser.format_usage())
print ("{0}: error: Cannot run with less than two players".format(
parser.prog))
exit(1)
if args.client and len(args.client) > args.teams:
sys.stdout.write(parser.format_usage())
print ("{0}: error: More clients specified than players".format(
parser.prog))
exit(1)
return args
# A simple logger that writes things to a file
class Logger(object):
def __init__(self, filename):
# Logs
self.filename = filename
self.turns = []
if not os.path.exists("gamerunner"):
os.mkdir("gamerunner")
open(filename, 'w').close()
def print_stuff(self, data):
self.turns.append(data)
def write_to_file(self):
print("Game finished - writing log to file")
with open(self.filename, 'a') as outfile:
for turn in self.turns:
outfile.write(turn + "\n")
def main():
global parameters
parameters = parse_args()
sys.stdout.write("Creating server with {0} players, ".format(parameters.teams))
print ("Running server on port {0}\n".format(parameters.port))
print ("Writing log to {0}".format(parameters.log))
fileLog = Logger(parameters.log)
print ("Starting Game")
my_game = Game()
serv = MMServer(parameters.teams, my_game, logger=fileLog)
serv.run(parameters.port, launch_clients)
fileLog.write_to_file()
class Client_program(object):
"""
This object holds and manages the processes for the
connecting teams
"""
first = True
def __init__(self, client_path, port=None):
"""
path of the client to run
"""
self.client_path = client_path
self.port = port
def run(self):
"""
"""
try:
commands = []
if os.name == "nt":
path = os.path.join(self.client_path, "client.py").replace("\\","/")
commands += ["python", path]
else:
path = os.path.join(self.client_path, "run.sh")
commands += ["sh", path]
commands += ["localhost", str(self.port or parameters.port)]
self.bot = Popen(commands, cwd=self.client_path)
except OSError as e:
msg = "the player {} failed to start with error {}".format(
self.client_path, e)
print (msg)
raise ClientFailedToRun(msg)
def kill(self):
if not self.bot.poll():
try:
self.bot.kill()
except OSError:
pass
def stop(self):
try:
self.bot.terminate()
except OSError:
pass
class ClientFailedToRun(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
if __name__ == "__main__":
main()