Handle a connection from the second player: join an existing game.
(websocket, join_key)
| 118 | |
| 119 | |
| 120 | async def join(websocket, join_key): |
| 121 | """ |
| 122 | Handle a connection from the second player: join an existing game. |
| 123 | |
| 124 | """ |
| 125 | # Find the Connect Four game. |
| 126 | try: |
| 127 | game, connected = JOIN[join_key] |
| 128 | except KeyError: |
| 129 | await error(websocket, "Game not found.") |
| 130 | return |
| 131 | |
| 132 | # Register to receive moves from this game. |
| 133 | connected.add(websocket) |
| 134 | try: |
| 135 | # Send the first move, in case the first player already played it. |
| 136 | await replay(websocket, game) |
| 137 | # Receive and process moves from the second player. |
| 138 | await play(websocket, game, PLAYER2, connected) |
| 139 | finally: |
| 140 | connected.remove(websocket) |
| 141 | |
| 142 | |
| 143 | async def watch(websocket, watch_key): |