import asyncio import json import websockets connected = [] IDs = {} class Dialogue: def __init__(self): self.options = {} def add(self, func, key = None): key = key if key else func.__name__ self.options[key] = func return func async def __call__(self, key, websocket): await self.options[key](self, websocket) dialogue = Dialogue() @dialogue.add async def action_rename(dialogue, websocket): await websocket.send(json.dumps({"type": "prose", "message": "Hmm. I will name you Cat. \n You are now named Cat."})) @dialogue.add async def action_leave(dialogue, websocket): await websocket.send(json.dumps({"type": "prose", "message": "Where would you like to go?"})) await websocket.send(json.dumps({ "type": "action", "clear_box": True, "actions": ["action_leave1", "action_leave2"] })) @dialogue.add async def action_leave1(dialogue, websocket): await websocket.send(json.dumps({"type": "prose", "message": "We're in area1"})) @dialogue.add async def action_leave2(dialogue, websocket): await websocket.send(json.dumps({"type": "prose", "message": "We're in area2"})) async def check(websocket): await websocket.send(json.dumps({"type": "test", "message": "test packet from server"})) async for original in websocket: packet = json.loads(original) print(packet) if packet["type"] == "user_joined": IDs[websocket] = packet["name"] websockets.broadcast(connected, json.dumps({"type": "server_message", "message": IDs[websocket] + " joined"})) websockets.broadcast(connected, json.dumps({ # should be await websocket.send so only client sees own action prose "type": "action", "clear_box": False, "actions": ["action_leave", "action_rename"] })) elif packet["type"] == "action": await dialogue(packet["action"], websocket) #websockets.broadcast(connected, json.dumps({"type": "view", "loc": "blueland"})) #websockets.broadcast(connected, json.dumps({"type": "portrait", "npc": "pink"})) #websockets.broadcast(connected, json.dumps({"type": "prose", "clear_box": "true", "message": "You are in Chatland. "})) #action4a = {"cond": "wallet >= $50", "action": "\"No deal.\"", "line": "Suit yourself.", "portrait": "blue", "end": "True"} #action4b = {"cond": "wallet >= $50", "action": "\"Deal.\"", "line": "You are now authorized to chat anywhere. \nYou spend $50.\n You gain Chat License.", "portrait": "blue", "call": "spend", "call": "gain_license", "end": "True"} #action4c = {"cond": "wallet < $50", "action": "\"I can't afford that!\"", "line": "Loser. Don't talk to me.", "portrait": "blue", "end": "True"} #action3 = {"action": "\"Can you authorize me?\"", "line": "Of course...For $50.", "portrait": "blue", "actions": [action4a, action4b, action4c]} #action2b = {"action": "\"Can we not chat elsewhere?\"", "line": "Oh no no no no no. \nIt is prohibited without proper authorization.", "portrait": "blue", "actions": [action3]} #action2a = {"action": "\"Why?\"", "line": "Best accoustics on the island!", "portrait": "blue"} #action1 = {"action": "\"Where am I?\"", "line": "This is where people chat.", "portrait": "blue", "actions": [action2a, action2b]} #rename = {"action": "Rename myself", "call": "rename_user", "line": "Hmm. I will name you Cat. \n You are now named Cat."} #catch2a = {"action": "Yes", "call": "add_pet", "line": "You now own a Glow Worm."} #catch2a = {"action": "No", "line": "You let him go."} #catch = {"action": "Catch a pet", "line": "You find a Glow Worm. Do you catch him?", "actions": [catch2a, catch2b]} #leave2a = {"action": "Blueland", "call": "travel"} #leave2a = {"action": "Pinkland", "call": "travel"} #leave2a = {"action": "Yellowland", "call": "travel"} #leave = {"action": "Leave", "line": "Where would you like to go?", "actions": [leave2a, leave2b, leave2c]} #websockets.broadcast(connected, json.dumps({"type": "action", "clear_box": "true", "actions": [action1,rename,catch,leave]})) elif packet["type"] == "shout" or packet['type'] == "server_message": websockets.broadcast(connected, json.dumps({"type": "shout", "name": IDs[websocket], "message": packet["message"]})) websockets.broadcast(connected, json.dumps({"type": "view", "loc": "yellowland"})) websockets.broadcast(connected, json.dumps({"type": "portrait", "npc": "blue"})) async def handler(websocket): while True: try: connected.append(websocket) await check(websocket) except websockets.exceptions.ConnectionClosed: websockets.broadcast(connected, json.dumps({"type": "server_message", "message": IDs[websocket] + " joined"})) break finally: if websocket in connected: connected.remove(websocket) async def main(): async with websockets.serve(handler, "", 8080): await asyncio.Future() # run forever if __name__ == "__main__": asyncio.run(main())