From 1afc6921915b59a0c79b13d9fad839779cda02bf Mon Sep 17 00:00:00 2001 From: Kislenko Maksim Date: Sat, 27 Jul 2019 12:27:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BB=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BC=D0=B5=D1=80=D1=82=D0=B8=20?= =?UTF-8?q?=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=86=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- paperio/local_runner/game_objects/game.py | 27 ++++++++++++++++++--- paperio/local_runner/game_objects/player.py | 12 +++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/paperio/local_runner/game_objects/game.py b/paperio/local_runner/game_objects/game.py index 216eb6d..3a7dbab 100644 --- a/paperio/local_runner/game_objects/game.py +++ b/paperio/local_runner/game_objects/game.py @@ -88,36 +88,54 @@ def __init__(self, clients): self.losers = [] self.bonuses = [] self.game_log = [] + self.events = [] self.tick = 1 + def append_event(self, event, p1, p2=None): + row = { + 'tick_num': self.tick, + 'event': event, + 'player': p1.get_state_for_event(), + } + if p2: + row['other'] = p2.get_state_for_event() + self.events.append(row) + def check_loss(self, player, players): is_loss = False if player.y < 0 + round(WIDTH / 2): is_loss = True + self.append_event('faced the border', player) if player.y > WINDOW_HEIGHT - round(WIDTH / 2): is_loss = True + self.append_event('faced the border', player) if player.x < 0 + round(WIDTH / 2): is_loss = True + self.append_event('faced the border', player) if player.x > WINDOW_WIDTH - round(WIDTH / 2): is_loss = True + self.append_event('faced the border', player) for p in players: if (p.x, p.y) in player.lines[:-1]: if p != player: p.tick_score += LINE_KILL_SCORE is_loss = True + self.append_event('line crossed by other player', player, p) for p in players: if is_intersect((player.x, player.y), (p.x, p.y)) and p != player: if len(player.lines) >= len(p.lines): is_loss = True + self.append_event('faced with other player', player, p) if len(player.territory.points) == 0: is_loss = True + self.append_event('has no territory', player) return is_loss @@ -134,7 +152,8 @@ def send_game_start(self): def send_game_end(self): self.game_log.append({ - 'type': 'end_game' + 'type': 'end_game', + 'events': self.events }) for player in self.players: player.send_message('end_game', {}) @@ -179,7 +198,7 @@ def get_bonuses_states(self): return [b.get_state() for b in self.bonuses] def collision_resolution(self, players_to_captured): - p_to_c = {p: c for p, c in players_to_captured.items() if not p.is_ate(players_to_captured)} + p_to_c = {p: c for p, c in players_to_captured.items() if not p.is_ate(players_to_captured)[0]} res = {p: copy.copy(c) for p, c in p_to_c.items()} for p1, captured1 in p_to_c.items(): for p2, captured2 in p_to_c.items(): @@ -226,8 +245,9 @@ async def game_loop(self, *args, **kwargs): players_to_captured = self.collision_resolution(players_to_captured) for player in self.players: - is_loss = player.is_ate(players_to_captured) + is_loss, p = player.is_ate(players_to_captured) if is_loss: + self.append_event('eaten by other player', player, p) self.losers.append(player) for player in self.players: @@ -248,6 +268,7 @@ async def game_loop(self, *args, **kwargs): if p != player: if any([is_intersect((p.x, p.y), point) for point in line]): self.losers.append(p) + self.append_event('killed by saw', p, player) Saw.log.append({ 'player': player.id, 'loser': p.id, diff --git a/paperio/local_runner/game_objects/player.py b/paperio/local_runner/game_objects/player.py index 440216c..8991ee6 100644 --- a/paperio/local_runner/game_objects/player.py +++ b/paperio/local_runner/game_objects/player.py @@ -99,6 +99,14 @@ def get_state(self): 'bonuses': self.get_bonuses_state() } + def get_state_for_event(self): + return { + 'id': self.id, + 'direction': self.direction, + 'lines_length': len(self.lines), + 'position': (self.x, self.y), + } + async def get_command(self, tick): if self.is_disconnected: return @@ -190,5 +198,5 @@ def is_ate(self, players_to_captured): position, is_move = self.get_position() if self != p and position in captured and \ (is_move or self.get_prev_position() in captured): - return True - return False + return True, p + return False, None