|
| 1 | +# |
| 2 | +# Copyright 2018 The boardgame.io Authors |
| 3 | +# |
| 4 | +# Use of this source code is governed by a MIT-style |
| 5 | +# license that can be found in the LICENSE file or at |
| 6 | +# https://opensource.org/licenses/MIT. |
| 7 | +# |
| 8 | +# pylint: disable=invalid-name,import-error,no-self-use |
| 9 | + |
| 10 | +""" |
| 11 | +Boardgame.io python client. |
| 12 | +""" |
| 13 | + |
| 14 | +import logging |
| 15 | +import socketIO_client as io |
| 16 | + |
| 17 | +class Namespace(io.BaseNamespace): |
| 18 | + """ |
| 19 | + SocketIO namespace providing handlers for events |
| 20 | + of the connection with the boardgame.io server. |
| 21 | + """ |
| 22 | + log = logging.getLogger('client.namespace') |
| 23 | + |
| 24 | + def __init__(self, *args): |
| 25 | + io.BaseNamespace.__init__(self, *args) |
| 26 | + self.bot = None |
| 27 | + self.previous_state_id = None |
| 28 | + self.actions = [] |
| 29 | + |
| 30 | + def set_bot_info(self, bot): |
| 31 | + """ |
| 32 | + Provides access to the Bot class that owns the connection. |
| 33 | + FIXME: is made necessary since socketio does not provide (yet) a way |
| 34 | + to pass extra arguments to the ctor of the namespace at creation. |
| 35 | + """ |
| 36 | + self.bot = bot |
| 37 | + return self |
| 38 | + |
| 39 | + def on_connect(self): |
| 40 | + """ Handle connection event. """ |
| 41 | + self.log.info('connected') # to game <%s>' % self.bot.game_id) |
| 42 | + |
| 43 | + def on_disconnect(self): |
| 44 | + """ Handle disconnection event. """ |
| 45 | + self.log.info('disconnected') |
| 46 | + def on_reconnect(self): |
| 47 | + """ Handle reconnection event. """ |
| 48 | + self.log.info('reconnected') |
| 49 | + |
| 50 | + def on_sync(self, *args): |
| 51 | + """ Handle serve 'sync' event. """ |
| 52 | + game_id = args[0] |
| 53 | + state = args[1] |
| 54 | + state_id = state['_stateID'] |
| 55 | + ctx = state['ctx'] |
| 56 | + |
| 57 | + # is it my game and my turn to play? |
| 58 | + if game_id == self.bot.game_id: |
| 59 | + if not self.previous_state_id or state_id >= self.previous_state_id: |
| 60 | + |
| 61 | + self.previous_state_id = state_id |
| 62 | + self.log.debug('state = %s', str(state)) |
| 63 | + G = state['G'] |
| 64 | + |
| 65 | + if 'gameover' in ctx: |
| 66 | + # game over |
| 67 | + self.bot.gameover(G, ctx) |
| 68 | + |
| 69 | + elif self.bot.player_id in ctx['actionPlayers']: |
| 70 | + self.log.info('phase is %s', ctx['phase']) |
| 71 | + if not self.actions: |
| 72 | + # plan next actions |
| 73 | + self.actions = self.bot.think(G, ctx) |
| 74 | + if not isinstance(self.actions, list): |
| 75 | + self.actions = [self.actions] |
| 76 | + if self.actions: |
| 77 | + # pop next action |
| 78 | + action = self.actions.pop(0) |
| 79 | + self.log.info('sent action: %s', action['payload']) |
| 80 | + self.emit('action', action, state_id, game_id, |
| 81 | + self.bot.player_id) |
| 82 | + |
| 83 | + |
| 84 | +class Bot(object): |
| 85 | + """ |
| 86 | + Base class for boardgame.io bot. |
| 87 | + """ |
| 88 | + log = logging.getLogger('client.bot') |
| 89 | + |
| 90 | + def __init__(self, server='localhost', port='8000', |
| 91 | + options=None): |
| 92 | + """ |
| 93 | + Connect to server with given game name, id and player id. |
| 94 | + Request initial synchronization. |
| 95 | + """ |
| 96 | + opts = {'game_name' : 'default', |
| 97 | + 'game_id' : 'default', |
| 98 | + 'player_id' : '1', |
| 99 | + 'num_players': 2} |
| 100 | + opts.update(options or {}) |
| 101 | + self.game_id = opts['game_name'] + ':' + opts['game_id'] |
| 102 | + self.player_id = opts['player_id'] |
| 103 | + self.num_players = opts['num_players'] |
| 104 | + |
| 105 | + # open websocket |
| 106 | + socket = io.SocketIO(server, port, wait_for_connection=False) |
| 107 | + self.io_namespace = socket.define(Namespace, '/'+opts['game_name']) |
| 108 | + self.io_namespace.set_bot_info(self) |
| 109 | + self.socket = socket |
| 110 | + |
| 111 | + # request initial sync |
| 112 | + self.io_namespace.emit('sync', self.game_id, self.player_id, self.num_players) |
| 113 | + |
| 114 | + def _create_action(self, action, typ, args=None): |
| 115 | + if not args: |
| 116 | + args = [] |
| 117 | + return { |
| 118 | + 'type': action, |
| 119 | + 'payload': { |
| 120 | + 'type': typ, |
| 121 | + 'args': args, |
| 122 | + 'playerID': self.player_id |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + def make_move(self, typ, *args): |
| 127 | + """ Create MAKE_MOVE action. """ |
| 128 | + return self._create_action('MAKE_MOVE', typ, list(args)) |
| 129 | + |
| 130 | + def game_event(self, typ): |
| 131 | + """ Create GAME_EVENT action. """ |
| 132 | + return self._create_action('GAME_EVENT', typ) |
| 133 | + |
| 134 | + def listen(self, timeout=1): |
| 135 | + """ |
| 136 | + Listen and handle server events: when it is the bot's turn to play, |
| 137 | + method 'think' will be called with the game state and context. |
| 138 | + Return after 'timeout' seconds if no events. |
| 139 | + """ |
| 140 | + self.socket.wait(seconds=timeout) |
| 141 | + |
| 142 | + def think(self, _G, _ctx): |
| 143 | + """ |
| 144 | + To be overridden by the user. |
| 145 | + Shall return a list of actions, instantiated with make_move(). |
| 146 | + """ |
| 147 | + assert False |
| 148 | + |
| 149 | + def gameover(self, _G, _ctx): |
| 150 | + """ |
| 151 | + To be overridden by the user. |
| 152 | + Shall handle game over. |
| 153 | + """ |
| 154 | + assert False |
0 commit comments