/
BeastArena.py
170 lines (140 loc) · 5.56 KB
/
BeastArena.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Game import Game
from UrwidVisualisation import UrwidVisualisation
from Server import Server
from Config import Config
from GameWinners import GameWinners
import os, logging, sys, threading, time, multiprocessing
class beast_arena(threading.Thread):
'''
main module where everything happens
'''
def __init__(self):
'''
basic constructor
'''
threading.Thread.__init__(self)
#self.geneticBeastsRegistered = False
self.urwid = None
self.gamecount = 1
self.game = None
self.running = False
self.server = None
self.winners = GameWinners()
self.useUrwid = Config.__getUseUrwidVisualisation__()
self.useNetworking = Config.__getUseNetworking__()
#self.geneticBeastTraining = Config.__geneticBeastTraining__()
#self.geneticBeasts = []
self.log = logging.getLogger('beast-arena-logging')
def runUrwid(self):
"""
runs all urwid loops.
should be called after run() is started as program will stay in urwid main loop
till finished
"""
while not self.game:
time.sleep(0.05)
self.urwid = UrwidVisualisation(self.game)
# update content Loop:
self.urwid.start()
#main urwid loop:
self.urwid.runLoop()
def stop(self):
"""
stops run() method by setting running=False
"""
self.running = False
def setupLogging(self):
"""
initial setup of python built-in logging module
"""
self.log.setLevel(logging.INFO)
#adding streamHandler with console as target if no gui is enabled
if not self.useUrwid:
streamHandler = logging.StreamHandler(sys.stdout)
streamHandler.setLevel(logging.INFO)
self.log.addHandler(streamHandler)
if not os.path.exists('log'):
os.makedirs('log')
fileHandler = logging.FileHandler('log/beast-arena.log')
fileHandler.setLevel(logging.WARNING)
self.log.addHandler(fileHandler)
def setGeneticBeasts(self, geneticBeasts):
self.geneticBeasts = geneticBeasts
def run(self):
"""
main-method of Game.py
if networking is enabled the first part will be proceeded
"""
self.running = True
self.setupLogging()
while self.running:
self.game = Game()
if self.useUrwid:
while not self.urwid:
time.sleep(0.1)
self.urwid.changeGame(self.game)
#loop for enabled networking
if self.useNetworking:
#first time server initialisation
if self.server == None:
self.server = Server(self.game)
self.game.server = self.server
self.game.server.start()
#for all games following first one
self.game.server = self.server
self.server.game = self.game
self.log.info('Game start will be: ' + str(self.game.startTime))
while self.running:
if time.time() >= self.game.startTimeMillis and not (self.game.gameStarted):
if len(self.game.beastObjectMap) > 0:
self.log.info('Starting Game No. %s (' + str(self.game.startTime) + ')...',
str(self.gamecount))
self.game.start()
self.gamecount += 1
else:
self.log.info('Canceled start of scheduled Game (' + str(
self.game.startTime) + '): no client registered!')
self.game.gameFinished = True
break
time.sleep(0.05)
elif self.running:
self.log.info('number of current game: ' + str(self.gamecount))
self.gamecount += 1
self.game.start()
while self.running:
if not self.running or self.game.gameFinished:
break
time.sleep(0.1)
self.winners.addWinner(self.game.rankingList, self.gamecount - 1)
time.sleep(2) # pause between two games
# def addBeast(self, beast):
# self.geneticBeasts.append(beast)
# print("registered: " + str(len(self.geneticBeasts)))
# if len(self.geneticBeasts) == multiprocessing.cpu_count():
# self.geneticBeastsRegistered = True
if __name__ == '__main__':
try:
game = beast_arena()
game.start()
while not game.game:
time.sleep(0.01)
if game.game.enableUrwidVisualisation:
game.runUrwid()
else:
while game.running:
time.sleep(0.01)
except KeyboardInterrupt, SystemExit:
game.log.info("\nCaught KeyboardInterrupt, exiting...")
game.log.debug("Currently running threads:")
for thread in threading.enumerate():
game.log.debug(' ' + str(thread))
if not isinstance(thread, threading._MainThread) and thread.running:
thread.stop()
thread.join()
game.log.debug('Threads after stop() & join():')
for thread in threading.enumerate():
game.log.debug(' ' + str(thread))
game.log.debug('Exiting beast-arena MainThread...')
game.log.info('beast-arena stopped: %s games played.', str(game.gamecount))