Made server and client 1

This commit is contained in:
2026-05-08 23:24:42 +01:00
parent 7db9406ced
commit 7e5874ffbb
14 changed files with 52 additions and 1 deletions
+2 -1
View File
@@ -1,2 +1,3 @@
/target /server/target
*.swp *.swp
.venv
+50
View File
@@ -0,0 +1,50 @@
import requests
# Loop and get new api
def main():
response = {}
while True:
try:
response = api_get()
match response["action_type"]:
case "output":
character = get_character(response["character"])
output(character, response["content"])
case "choice":
user_choice = choice(response["choices"])
case "end":
exit()
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")
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()
print(response)
return response
main()
View File
View File
View File
View File
View File