Contact me to be added to this repository. Push the creations you make for D&D, and they will be displayed in a nice website. See the website for how to contribute.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

183 lines
6.2 KiB

import sys
from flask import Flask, render_template, request, redirect
from items import get_items, get_worlds, get_monsters
from dungeon import genGridDungeonB64
from random import choice, sample
import re
import json
from shutil import copyfile
if sys.version_info.major < 3:
# XXX OOF
reload(sys)
sys.setdefaultencoding('utf8')
app = Flask(__name__)
@app.route('/style.css')
def style():
return render_template('style.css')
@app.route('/')
def index():
featureType = choice(["Item","Place","Spell"])
item = choice(get_items("../"+featureType+"s/*"))
return render_template('index.html',featureType=featureType,featureText=item)
@app.route('/search', methods=['POST'])
def search():
typ = request.form['type']
query = request.form['inputText']
print(typ + ": " + query)
results = []
for direct in ["../Items/*","../Places/*","../Spells/*"]:
objects = get_items(direct)
for (name,heads,body) in objects:
if typ == "name":
if query.lower() in name.lower():
results.append((name,heads,body))
elif typ == "rarity":
if "rarity" in [h[0].lower() for h in heads]:
if query.lower() in [h[1].lower() for h in heads if h[0].lower() == "rarity"]:
results.append((name,heads,body))
elif typ == "aura":
if "aura" in [h[0].lower() for h in heads]:
if query.lower() in [h[1].lower() for h in heads if "aura" in h[0].lower()][0]:
results.append((name,heads,body))
elif typ == "text":
if query.lower() in body.lower():
results.append((name,heads,body))
print("Number of results: " + str(len(results)))
featureType = choice(["Item","Place","Spell"])
item = choice(get_items("../"+featureType+"s/*"))
return render_template('index.html',featureType=featureType,featureText=item, searchResults=results)
@app.route("/marches")
def marches():
return render_template('marches.html', worlds=get_worlds())
@app.route("/marches/maps/<world>")
def marchMap(world):
#image_map = [("Temple of Constant Explosions",10,20,35),("Deep Gnome Workshop",50,40,20)] #(place,x,y,r)
mfile = open("../Worlds/"+ world + "/map.json",'r')
mData = mfile.read()
image_map = [ (x['name'],x['x'],x['y'],x['r']) for x in json.loads(mData)['places']]
return render_template("marchMap.html",worldname=world,imap=image_map)
@app.route("/marches/worlds/<world>")
def marchWorld(world):
worldDir = "../Worlds/"+world + "/"
#copy <worldname>.png from world.png into static
copyfile(worldDir+"world.png","static/"+world+".png")
#player_list = ["INVALID1","INVALID2","INVALID3"]
empty = re.compile('^\s*$')
pfile = open(worldDir + "players.txt",'r')
pData = pfile.read().split('\n')
player_list = [ x for x in pData if not empty.match(x)]
pfile.close()
#image_map = [(10,20,35),(50,40,20)] #("place name",x,y,r)
#image_map = [("Temple of Constant Explosions",10,20,35),("Deep Gnome Workshop",50,40,20)] #(place,x,y,r)
#log_list = ["Raiding Cosi"] # :: [AdventureName]
log_list = json.load(open(worldDir + "log.json",'r')).keys()
return render_template("marchWorld.html",worldname=world,players=player_list,logs=log_list)
@app.route("/marches/worlds/<world>/<logname>")
def marchLog(world,logname):
print(logname)
#... code
#logname is title of adventure
#log type is (DateString,X,Y,[(Name,Text)])
worldDir = "../Worlds/"+world + "/"
lookup = json.load(open(worldDir + "log.json", 'r'))
logFile = open(worldDir+ "logs/" + lookup[logname],'r')
logData = logFile.read().split('\n')
name = logData[0].split(":")[1]
date = logData[1].split(":")[1]
X = logData[2].split(":")[1]
Y = logData[3].split(":")[1]
logs = []
breakLine = re.compile("^---*$")
for i in range(4,len(logData)):
if len(logData[i]) == 0:
continue
if breakLine.match(logData[i]):
char = logData[i+1]
text = ""
i += 1 #adjust offset in file. We are now at the player's name
s = logData[i] #s will be the next line of the player's entry
while len(s) > 0: #while there is more written text
s = logData[i+1] #grab line of text
text += "\n"+s
i += 1
logs.append((char,text))
return render_template("marchLog.html",worldname=world,log=(name,date,X,Y,logs))
def bucket_route(uri, dir, sing=None):
if sing is None:
sing = dir[:-1] # minus "s"
path = '../' + dir + '/*'
root = '/' + uri
def render_bucket():
return render_template('listing.html', title=dir, uri=uri, items=get_items(path))
app.add_url_rule(root, 'render_' + uri, render_bucket)
def random_bucket():
nm = choice(get_items(path))[0]
return redirect(root + '#' + nm)
app.add_url_rule(root + '/random', 'random_' + uri, random_bucket)
def bucket_table(amt):
try:
return render_template('table.html', title=sing + ' Table', items=sample(get_items(path), amt), roll=amt)
except ValueError:
return render_template('table.html', title='you dun goofed', error="Can't construct that table (not enough items?)")
app.add_url_rule(root + '/table/<int:amt>', dir + '_table', bucket_table)
bucket_route('items', 'Items')
bucket_route('places', 'Places')
bucket_route('characters', 'Characters')
bucket_route('spells', 'Spells')
bucket_route('feats', 'Feats')
@app.route("/monsters")
def render_monsters():
return render_template('monsters.html',monsters=get_monsters())
@app.route("/monsters/random")
def random_monster():
m = choice(get_monsters())
return redirect("/monsters#"+m.name)
@app.route("/dungeon")
def dungeon():
return render_template('dungeon.html')
@app.route("/dungeonGen", methods=["POST"])
def genDungeon():
x = int(request.form['x'])
y = int(request.form['y'])
m = int(request.form['m'])
s = int(request.form['s'])
c = int(request.form['c'])
enc = str(genGridDungeonB64(x,y,m,s,c))[2:-1]
return render_template('image.html',data=enc)
if __name__ == "__main__":
app.run()