Skip to content

Commit

Permalink
RF: Send final game state with to network players
Browse files Browse the repository at this point in the history
This makes it easier for backends/middleends to capture the last state
that a player receives without having to parse it and ensure that it
actually contains a state.

A corollary to not doing that kind of deep package inspection is that,
once a game has been started, we cannot distinguish between control
messages and game info (set_initial/get_move) messages anymore and have
to assume that everything is a message that needs to be forwarded to the
backend player (unless we change our ZMQ protocoll to include an
additional control channel).
  • Loading branch information
Debilski committed Oct 13, 2023
1 parent 512d963 commit cf4c2e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 9 additions & 2 deletions pelita/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,18 @@ def prepare_bot_state(game_state, idx=None):
"""

bot_initialization = game_state.get('turn') is None and idx is not None
bot_finalization = game_state.get('turn') is not None and idx is not None

if bot_initialization:
# We assume that we are in get_initial phase
turn = idx
bot_turn = None
seed = game_state['rnd'].randint(0, sys.maxsize)
elif bot_finalization:
# Called for remote players in _exit
turn = idx
bot_turn = None
seed = None
else:
turn = game_state['turn']
bot_turn = game_state['turn'] // 2
Expand Down Expand Up @@ -977,9 +983,10 @@ def check_exit_remote_teams(game_state):
""" If the we are gameover, we want the remote teams to shut down. """
if game_state['gameover']:
_logger.info("Gameover. Telling teams to exit.")
for team in game_state['teams']:
for idx, team in enumerate(game_state['teams']):
try:
team._exit()
team_game_state = prepare_bot_state(game_state, idx=idx)
team._exit(team_game_state)
except AttributeError:
pass

Expand Down
10 changes: 8 additions & 2 deletions pelita/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,24 @@ def get_move(self, game_state):
except ZMQClientError:
raise

def _exit(self):
def _exit(self, game_state=None):
# We only want to exit once.
if getattr(self, '_sent_exit', False):
return

if game_state:
payload = {'game_state': game_state}
else:
payload = {}

try:
# TODO: make zmqconnection stateful. set flag when already disconnected
# For now, we simply check the state of the socket so that we do not send
# over an already closed socket.
if self.zmqconnection.socket.closed:
return
# TODO: Include final state with exit message
self.zmqconnection.send("exit", {}, timeout=1)
self.zmqconnection.send("exit", payload, timeout=1)
self._sent_exit = True
except ZMQUnreachablePeer:
_logger.info("Remote Player %r is already dead during exit. Ignoring.", self)
Expand Down

0 comments on commit cf4c2e1

Please sign in to comment.