Files
happening/client/main.py
T
deadvey 20174e697c removed unwraps and added a method to Results called .unwrap_or_exit(error_message, error_code)
This tries to unwrap and if it can't then it outputs the error message with the error!() macro
(log library) and exits with the error code.  This is to be used instead of expect and is for
fatal errors
2026-05-15 19:19:17 +01:00

54 lines
1.2 KiB
Python

import requests
import os
# Loop and get new api
def main():
response = {}
while True:
try:
response = api_get()
print(response)
match response["action_type"]:
case "output":
character = get_character(response["character"])
output(character, response["content"])
case "choice":
user_choice = choice(response["choices"])
continue
case "end":
print("Exitting successfully")
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);
# 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):
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()