Created
March 12, 2020 23:32
-
-
Save njdart/3c680c714f5b55041e50600db2190bf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, redirect | |
from uuid import uuid4 | |
app = Flask(__name__) | |
# TODO: persist to disk | |
user_state = {} | |
def make_map(): | |
return { | |
"main_hall": { | |
"pretty": "Main Hall", | |
"description": """ | |
<p> | |
You are in a long hallway, lined with portraits and pictures. Underfoot is an old wooden floor, topped with the occasional rug. | |
</p> | |
<p> | |
Your back is to the main door. To your <b>right</b> is the living room, to your <b>left</b>, the kitchen. <b>Ahead</b> leads to the lounge. | |
</p> | |
""", | |
"directions": { | |
"left": "kitchen", | |
"right": "living_room", | |
"forward": "lounge", | |
}, | |
"items": [] | |
}, | |
"kitchen": { | |
"pretty": "Kitchen", | |
"description": """ | |
""", | |
"directions": { | |
"back": "main_hall", | |
}, | |
"items": [] | |
}, | |
"living_room": { | |
"pretty": "Living Room", | |
"description": """ | |
""", | |
"directions": { | |
"back": "main_hall", | |
}, | |
"items": [] | |
}, | |
"lounge": { | |
"pretty": "Lounge", | |
"description": """ | |
""", | |
"directions": { | |
"back": "main_hall", | |
}, | |
"items": [] | |
} | |
} | |
def add_user(): | |
user_id = str(uuid4()) | |
user_state[user_id] = { | |
# the map. This contins info about all the locations, where the location | |
# leads to, what items the location contains, etc | |
# We store it on the player as they can modify it (eg, take an item and | |
# put it in their inventory) | |
"map": make_map(), | |
# the current location in the map | |
"location": "main_hall", | |
# The player's inventory | |
"inventory": [], | |
} | |
return user_id | |
def user_not_found(): | |
return """ | |
<html> | |
<head> | |
<title>Not Found</title> | |
</head> | |
<body> | |
<h1>Oops, looks like you took a wrong turn</h1> | |
<a href="/">Start a new game</a> | |
</body> | |
</html> | |
""", 404 | |
def format_location_directions(user_id, user): | |
directions = "" | |
current_location = user["map"][user["location"]] | |
for direction, destination in current_location["directions"].items(): | |
directions += "<div>" | |
location_pretty = user["map"][destination]["pretty"] | |
directions += '<a href="/game/{user_id}/{direction}">{direction}</a>, to the {location_pretty}'.format( | |
user_id=user_id, direction=direction, location_pretty=location_pretty) | |
directions += "</div>" | |
return directions | |
def format_inventory(inventory): | |
# TODO | |
return str(inventory) | |
###################### | |
# Flask Web routes | |
###################### | |
@app.route('/') | |
def index(): | |
user_id = add_user() | |
# 302 is a temporary redirect | |
return redirect("/game/{}".format(user_id), code=302) | |
@app.route('/game/<user_id>/<direction>') | |
def move_left(user_id, direction): | |
if user_id not in user_state: | |
return user_not_found() | |
user = user_state[user_id] | |
location = user["map"][user["location"]] | |
if direction in location["directions"]: | |
user["location"] = location["directions"][direction] | |
return redirect("/game/{}".format(user_id), code=302) | |
@app.route('/game/<user_id>') | |
def game_screen(user_id): | |
if user_id not in user_state: | |
return user_not_found() | |
user = user_state[user_id] | |
location = user["map"][user["location"]] | |
location_pretty = location["pretty"] | |
location_description = location["description"] | |
location_directions = format_location_directions(user_id, user) | |
inventory = format_inventory(user["inventory"]) | |
return """ | |
<html> | |
<head> | |
<title>The Game Dungeon</title> | |
</head> | |
<body> | |
<h1>{location}</h1> | |
{location_description} | |
<hr /> | |
{location_directions} | |
</hr /> | |
<h1>Inventory</h1> | |
{inventory} | |
<hr /> | |
<a href="/">Start a new game</a> | |
</body> | |
</html> | |
""".format(location=location_pretty, location_description=location_description, location_directions=location_directions, inventory=inventory) | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment