Skip to content

Commit

Permalink
add steps for statistic purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed Oct 31, 2019
1 parent 038eaa2 commit 30cc977
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def __init__(self, level=1, lives=LIVES, timeout=TIMEOUT, size=MAP_SIZE):
self._running = False
self._timeout = timeout
self._score = 0
self._step = 0
self._total_steps = 0
self._state = {}
self._initial_lives = lives
self.map = Map(size=size, empty=True)
Expand All @@ -154,6 +156,10 @@ def running(self):
def score(self):
return self._score

@property
def total_steps(self):
return self._total_steps

def start(self, player_name):
logger.debug("Reset world")
self._player_name = player_name
Expand All @@ -168,6 +174,7 @@ def start(self, player_name):

def stop(self):
logger.info("GAME OVER")
self._total_steps += self._step
self._running = False

def next_level(self, level):
Expand All @@ -179,6 +186,7 @@ def next_level(self, level):
logger.info("NEXT LEVEL")
self.map = Map(level=level, size=self.map.size, enemies=len(LEVEL_ENEMIES[level]))
self._bomberman.respawn()
self._total_steps += self._step
self._step = 0
self._bombs = []
self._powerups = []
Expand Down
4 changes: 3 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, level, lives, timeout, grading):
def save_highscores(self):
# update highscores
logger.debug("Save highscores")
logger.info("FINAL SCORE <%s>: %s", self.current_player.name, self.game.score)
logger.info("FINAL SCORE <%s>: %s with %s steps", self.current_player.name, self.game.score, self.game.total_steps)

self._highscores.append((self.current_player.name, self.game.score))
self._highscores = sorted(self._highscores, key=lambda s: -1 * s[1])[
Expand Down Expand Up @@ -129,6 +129,7 @@ async def mainloop(self):
try:
if self.grading:
game_rec["score"] = self.game.score
game_rec["total_steps"] = self.game.total_steps
game_rec["level"] = self.game.map.level
requests.post(self.grading, json=game_rec)
except:
Expand Down Expand Up @@ -162,6 +163,7 @@ async def mainloop(self):

game_loop_task = asyncio.ensure_future(g.mainloop())

logger.info(f"Listenning @ {args.bind}:{args.port}")
websocket_server = websockets.serve(g.incomming_handler, args.bind, args.port)

loop = asyncio.get_event_loop()
Expand Down

0 comments on commit 30cc977

Please sign in to comment.