forked from funkaoshi/randomcharacter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.py
140 lines (113 loc) · 3.79 KB
/
create.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from flask import request, Response, redirect, render_template, url_for, Flask
import adventuregame
import character
import characterclass
import dangertime
import dice
import fifth
import json
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
SYSTEMS = {
'lbb': character.LBBCharacter,
'holmes': character.HolmesCharacter,
'basic': character.BasicCharacter,
'pahvelorn': character.PahvelornCharacter,
'apollyon': character.ApollyonCharacter,
'carcosa': character.CarcosaCharacter,
'moc': character.MastersOfCarcosaCharacter,
'dd': character.DelvingDeeperCharacter,
'lotfp': character.LotFPCharacter
}
@app.template_filter()
def with_sign(number):
return "+{0}".format(number) if number > 0 else str(number)
@app.route('/5e/')
def fifth_edition():
return render_template("5e.html", c=fifth.Character())
@app.route('/3d6/')
def three_dee_six():
roll = [dice.xdy(3,6) for _ in range(6)]
return render_template("3d6.html", roll=roll)
@app.route('/equipment/', defaults={'system': "basic"})
@app.route('/equipment/<system>/')
def equipment(system):
system = SYSTEMS.get(system, None)
if not system:
# default to basic for unknown systems
return redirect(url_for('equipment', system='basic'))
equipment = system().equipment
return render_template("equipment.html", equipment=equipment)
@app.route('/4d6/')
def four_dee_six():
roll = [sum(sorted(dice.d(6) for _ in xrange(4))[1:]) for _ in range(6)]
return render_template("4d6.html", roll=roll)
@app.route('/adventuregame/')
def make_adventure_game_char():
return render_template("adventuregame.html", c=adventuregame.Character())
@app.route('/dangertime/')
def make_danger_time_char():
return render_template("dangertime.html", c=dangertime.Character())
@app.route('/npcs/', defaults={'number': 10})
@app.route('/npcs/<int:number>/')
def generate_npcs(number):
if number > 1000:
number = 1000
characters = [character.BasicCharacter(testing=True) for _ in xrange(number)]
return render_template("npcs.html", characters=characters)
@app.route('/')
def index():
return redirect('/basic/')
@app.route('/text/')
def index_text():
return redirect('/basic/text/')
@app.route('/<system>/', defaults={'fmt': "html"})
@app.route('/<system>/<fmt>/')
def generate(system, fmt):
if fmt == "text":
template = "plaintext.txt"
mimetype = "text/plain"
elif fmt == "html":
template = "index.html"
mimetype = "text/html"
elif fmt == "yaml":
template = "yaml.txt"
mimetype ="text/plain"
elif fmt == "json":
mimetype = "application/json"
else:
# default to HTML for unknown display formats
return redirect(url_for('generate', system=system, fmt="html"))
system = SYSTEMS.get(system, None)
if not system:
# default to basic for unknown systems
return redirect(url_for('generate', system='basic', fmt=fmt))
c = get_class(request.args.get('class'))
context = system(classname=c)
if fmt == "json":
content = json.dumps(context.to_dict())
else:
content = render_template(template, c=context)
response = Response(content, status=200, mimetype=mimetype)
return response
def get_class(classname):
"""
Verifies the supplied class paramter is a valid class name.
"""
if classname not in characterclass.VALID_CLASS_NAMES:
return None
return classname
#
# base64.b64encode(pickle.dumps(context))
#
# @app.route('/character/<b64>/')
# def restore(b64):
# enc_context = base64.b64decode(b64)
# context = pickle.loads(enc_context)
# content = render_template("index.html", c=context)
# response = Response(content, status=200)
# return response
#
if __name__ == '__main__':
app.run("0.0.0.0")