From 9f52b2840ffe25528ea362d9ce1b92cb8de2e5a0 Mon Sep 17 00:00:00 2001 From: chimchooree Date: Sat, 4 Mar 2023 21:17:32 -0600 Subject: [PATCH] added support for dialogue trees using decorated functions --- server.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/server.py b/server.py index 4ebcde3..e60f38b 100644 --- a/server.py +++ b/server.py @@ -5,6 +5,42 @@ 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: @@ -13,10 +49,39 @@ async def check(websocket): 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({"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. "})) - websockets.broadcast(connected, json.dumps({"type": "action", "clear_box": "true", "actions": ["\"Where am I?\"","Talk to Godot Head","Catch New Pet","Leave"]})) + await websocket.send(json.dumps({ + "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"}))