This repository has been archived by the owner on May 30, 2024. It is now read-only.
forked from jav/pybotwar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
300 lines (246 loc) · 8.6 KB
/
main.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
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
# Copyright 2009-2014 Lee Harr
#
# This file is part of pybotwar.
# http://pybotwar.googlecode.com/
#
# Pybotwar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pybotwar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pybotwar. If not, see <http://www.gnu.org/licenses/>.
import time
try:
import conf
except ImportError:
import util
util.makeconf()
import stats
stats.dbcheck()
raise SystemExit
import util
try:
util.setup_conf()
except ImportError:
print 'Trying to use Qt settings per conf.py'
print 'but unable to import PyQt4 modules.'
print
print 'Check PyQt4 installation, or change conf.py'
print 'to have use_qt_settings = False'
raise SystemExit
import viewselect
def check_pybox2d_version():
pybox2d_error = False
try:
import pkg_resources
pkg_resources.require('Box2D==%s'%conf.pybox2d_version)
except ImportError:
pass
except pkg_resources.VersionConflict:
pass
try:
import Box2D
except ImportError:
pybox2d_error = True
else:
if not Box2D.__version__ == conf.pybox2d_version:
pybox2d_error = True
if pybox2d_error:
print 'Unable to import PyBox2D'
print 'requires version', conf.pybox2d_version
print
print 'If running with Qt interface, try clearing Qt settings with:'
print 'python main.py -S'
raise SystemExit
def setup_logging(level='info'):
import logging
import logging.handlers
logger = logging.getLogger('PybotwarLogger')
logger.setLevel(logging.INFO)
handler = logging.handlers.RotatingFileHandler(
'appdebug.log', maxBytes=1000000, backupCount=3)
logger.addHandler(handler)
def run_supertournament(nbattles):
robots = conf.robots
nrobots = len(robots)
import datetime
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
from itertools import combinations
combos = []
for n in range(2, nrobots+1):
combos.extend(combinations(robots, n))
import sys
py = sys.executable
main = os.path.abspath(__file__)
cmd = '%s %s -t "%s" -g -n %s --robots %s'
for combo in combos:
rstr = ' '.join(combo)
cmdstr = cmd % (py, main, dt, nbattles, rstr)
print cmdstr
os.system(cmdstr)
if __name__ == '__main__':
import sys
import os
os.chdir(os.path.split(os.path.abspath(__file__))[0])
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-T', '--testmode', dest='testmode',
action='store_true', default=False,
help='run in test mode')
parser.add_argument('-t', '--tournament', dest='tournament',
nargs='?', const=True, default=False,
help='run a tournament')
parser.add_argument('-n', '--battles', dest='nbattles',
action='store', type=int, default=5,
help='number of battles in tournament')
parser.add_argument('--supertournament', dest='supertournament',
action='store_true', default=False,
help='run a supertournament')
parser.add_argument('-g', '--no-graphics', dest='nographics',
action='store_true', default=False,
help='non graphics mode')
parser.add_argument('-Q', '--pyqt-graphics', dest='pyqtgraphics',
action='store_true', default=False,
help='enable PyQt interface')
parser.add_argument('-P', '--pygsear-graphics', dest='pygseargraphics',
action='store_true', default=False,
help='enable Pygsear interface')
parser.add_argument('-D', '--upgrade-db', dest='upgrade_db',
action='store_true', default=False,
help='upgrade database (WARNING! Deletes database!)')
parser.add_argument('-S', '--reset-qt-settings', dest='qtreset',
action='store_true', default=False,
help='reset Qt settings')
parser.add_argument('-B', '--app-debug', dest='appdebug',
action='store_true', default=False,
help='enable app debug log')
parser.add_argument('--robots', dest='robots', nargs='+',
metavar='ROBOT',
help='list of robots to load')
options = parser.parse_args()
testmode = options.testmode
tournament = options.tournament
nbattles = options.nbattles
supertournament = options.supertournament
nographics = options.nographics
pyqtgraphics = options.pyqtgraphics
pygseargraphics = options.pygseargraphics
upgrade_db = options.upgrade_db
qtreset = options.qtreset
appdebug = options.appdebug
robots = options.robots
if robots is not None:
conf.robots = robots
gmodes = nographics + pyqtgraphics + pygseargraphics
if appdebug:
setup_logging()
if gmodes > 1:
print 'must select ONE of -g, -Q, or -P'
import sys
sys.exit(0)
if tournament and supertournament:
print 'must select one of --tournament or --supertournament'
import sys
sys.exit(0)
elif supertournament:
run_supertournament(nbattles)
import sys
sys.exit(0)
elif nographics:
viewselect.select_view_module('none')
elif pyqtgraphics:
viewselect.select_view_module('pyqt')
elif pygseargraphics:
viewselect.select_view_module('pygame')
else:
# default view type is PyQt
pyqtgraphics = True
viewselect.select_view_module('pyqt')
view = viewselect.get_view_module()
from game import Game
import world
import stats
def dbcheck():
if not stats.dbcheck():
print 'Run pytbotwar with -D switch to upgrade database.'
print 'WARNING: This will delete your current database!'
import sys
sys.exit(0)
def reset_qt_settings():
from PyQt4 import QtCore
QtCore.QCoreApplication.setOrganizationName('pybotwar.googlecode.com')
QtCore.QCoreApplication.setOrganizationDomain('pybotwar.googlecode.com')
QtCore.QCoreApplication.setApplicationName('pybotwar')
settings = QtCore.QSettings()
settings.clear()
print 'Qt settings cleared.'
import sys
sys.exit(0)
def runmain():
if qtreset:
reset_qt_settings()
check_pybox2d_version()
if upgrade_db:
print 'Upgrading Database'
stats.dbremove()
stats.dbopen()
return
dbcheck()
stats.dbopen()
global tournament
global nbattles
if not pyqtgraphics and not tournament:
tournament = True
nbattles = 1
if tournament and pyqtgraphics:
print 'When using PyQt interface, run tournaments from GUI.'
elif tournament:
if tournament is True:
import datetime
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
else:
dt = tournament
print 'Beginning tournament with %s battles.' % nbattles
for battle in range(nbattles):
print 'Battle', battle+1
game = Game(testmode, dt)
game.run()
world.Robot.nrobots = 0
view.Robot.nrobots = 0
results = stats.tournament_results(dt)
print;print;print;
print 'Tournament Results'
print nbattles, 'battles between', len(results), 'robots'
print
for line in results:
print line[1], ':', line[4], 'wins', ':', line[6], 'outlasted', ':', line[7], 'dmg caused', ':', line[8], 'kills'
elif pyqtgraphics:
import qt4view
qt4view.run(testmode)
else:
game = Game(testmode)
game.run()
stats.dbclose()
if not tournament:
stats.dbopen()
stats.top10()
# Clean up log directory if not in test mode
logdir = os.path.join(conf.base_dir, conf.logdir)
if not testmode and os.path.exists(logdir):
for f in os.listdir(logdir):
fpath = os.path.join(logdir, f)
try:
os.remove(fpath)
except OSError:
pass
if __name__ == '__main__':
try:
runmain()
except KeyboardInterrupt:
pass