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