MCPcopy Index your code
hub / github.com/python-websockets/websockets / play

Function play

example/tutorial/step2/app.py:48–82  ·  view source on GitHub ↗

Receive and process moves from a player.

(websocket, game, player, connected)

Source from the content-addressed store, hash-verified

46
47
48async def play(websocket, game, player, connected):
49 """
50 Receive and process moves from a player.
51
52 """
53 async for message in websocket:
54 # Parse a "play" event from the UI.
55 event = json.loads(message)
56 assert event["type"] == "play"
57 column = event["column"]
58
59 try:
60 # Play the move.
61 row = game.play(player, column)
62 except ValueError as exc:
63 # Send an "error" event if the move was illegal.
64 await error(websocket, str(exc))
65 continue
66
67 # Send a "play" event to update the UI.
68 event = {
69 "type": "play",
70 "player": player,
71 "column": column,
72 "row": row,
73 }
74 broadcast(connected, json.dumps(event))
75
76 # If move is winning, send a "win" event.
77 if game.winner is not None:
78 event = {
79 "type": "win",
80 "player": game.winner,
81 }
82 broadcast(connected, json.dumps(event))
83
84
85async def start(websocket):

Callers 2

startFunction · 0.70
joinFunction · 0.70

Calls 3

errorFunction · 0.70
broadcastFunction · 0.50
playMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…