Files
happening/test-client/main.py
T
deadvey cc3eca857d strings in variables can now be received by get_string_token
and inputs can be directly assigned to a variable
2026-05-26 20:35:57 +01:00

72 lines
1.6 KiB
Python

import requests
import os
import time
import sys
debug = False
try:
if sys.argv[1] == "debug": debug = True
except:
debug = False
# Loop and get new api
def main():
response = {}
id = -1
while True:
try:
response = api_get()
if debug: print(response)
match response["action_type"]:
case "output":
character = get_character(response["character"])
output(character, response["content"])
case "choice":
user_choice = choice(response["choices"])
time.sleep(0.5)
continue
case "end":
print("Exitting successfully")
requests.post("127.0.0.1:20264", json=0);
os._exit(0)
except:
print("Server not up or cannot be reached")
input() # Enter to go to next loop (testing)
# Make choice
def choice(choices):
api_url = "http://localhost:20264/choice"
for (index,choice) in enumerate(choices):
print(f"{index}: {choice}")
try:
choice = int(input())
except:
choice = 0
print("Invalid choice, defaulting to 0")
requests.post(api_url, json=choice);
def get_input():
api_url = "http://localhost:20264/input"
input_string = input()
requests.post(api_url, json=input_string);
# Character outputs text to the user
def output(character, text):
print(character["name"], "says")
print(text)
# Get user from the backend
def get_character(character):
if character.lower() == "narrator":
return {"name": "narrator"}
api_url = f"http://localhost:20264/character/{character}"
return requests.get(api_url).json()
# Normal API request
def api_get():
api_url = "http://localhost:20264/happening/"
response = requests.get(api_url).json()
return response
main()