Files
cyoa-discord/server.py
T

42 lines
1.3 KiB
Python

import json
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route("/")
def mainPage():
return open("main.html").read()
@app.route("/<path:path>")
def game(path):
if "Discord" not in str(request.user_agent):
return mainPage()
gamecode = request.args.get('code')[:-1]
gameName = request.path[1:]
print(gameName)
return buildHTML(gamecode,gameName)
def buildHTML(gamecode, gameName):
with open("stories/" + gameName.replace(".", "") + ".json", "r") as f:
print("\n\n")
story = json.loads(f.read())
nodeName = "start"
for action in gamecode:
nodeName = story[nodeName]["choices"][int(action) - 1]["next"]
print(action, nodeName)
info = story[nodeName]
currText = info["text"]
choices = ""
for i in range(len(info["choices"])):
choice = info["choices"][i]
choices += f"{(i + 1) * 10}: " + choice["text"] + "\\n"
print(str(request.user_agent) + "\n",currText,choices)
with open('page.html', "r") as page:
return page.read().replace("{{CurrText}}", currText.replace('"', '\\"')).replace("{{Choices}}", choices.replace('"', '\\"'))
if __name__ == '__main__':
app.run(debug=True, port=1387)