-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
298 lines (275 loc) · 8.89 KB
/
application.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import json
import urllib
import uuid
import random
import logging
from random import Random
from places.place import PlaceSource
from dungeons.dungeon_api import DungeonSource
from flask_cors import CORS
from werkzeug.exceptions import BadRequest
from collections import Counter
from flask.logging import default_handler
from utils.library import monster_manual
from encounters.encounter_api import EncounterSource
from encounters.encounter_set import EncounterSetSource
from flask_restplus import Resource, Api, reqparse
from flask import Flask, jsonify, redirect, url_for, request, render_template
import sys
from logging.config import dictConfig
dictConfig(
{
"version": 1,
"formatters": {
"default": {
"format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s",
}
},
"handlers": {
"wsgi": {
"class": "logging.StreamHandler",
"stream": "ext://flask.logging.wsgi_errors_stream",
"formatter": "default",
}
},
"root": {"level": "INFO", "handlers": ["wsgi"]},
}
)
application = Flask(__name__)
cors = CORS(application)
api = Api(
application,
version="0.1",
title="Encounter REST API",
description="REST-ful API for encounters",
)
parser = reqparse.RequestParser()
parser.add_argument(
"character_levels",
type=int,
action="split",
required=True,
help="Comma-separated list of character levels.",
)
parser.add_argument(
"monster_sets",
action="split",
required=True,
help="Comma-separated list of valid monster sets.",
)
parser.add_argument(
"difficulty",
type=str,
required=False,
help="Desired difficulty for encounter - medium, difficult, hard. Outcome not guaranteed.",
)
encounter_set_parser = reqparse.RequestParser()
encounter_set_parser.add_argument(
"character_levels",
type=int,
action="split",
required=True,
help="Comma-separated list of character levels.",
)
encounter_set_parser.add_argument(
"monster_set", required=False, help="Monster set.")
tag_parser = reqparse.RequestParser()
tag_parser.add_argument(
"all_tags",
action="split",
required=False,
help="Required tags - all must be present",
)
tag_parser.add_argument(
"any_tags",
action="split",
required=False,
help="Required tags - one must be present",
)
tag_parser.add_argument(
"none_tags",
action="split",
required=False,
help="Excluded tags - none must be present",
)
dungeon_parser = reqparse.RequestParser()
dungeon_parser.add_argument(
"level", type=int, required=True, help="Average level of party"
)
dungeon_parser.add_argument(
"terrain", type=str, required=False, help="Terrain type for dungeon setting."
)
dungeon_parser.add_argument(
"dungeon_type", type=str, required=False, help="Base type of dungeon."
)
dungeon_parser.add_argument(
"main_antagonist",
type=str,
required=False,
help="Monster set of main dungeon antagonist.",
)
dungeon_parser.add_argument(
"guid", type=str, required=False, help="GUID to intialise random state"
)
dungeon_tags_parser = reqparse.RequestParser()
dungeon_tags_parser.add_argument(
"level", type=int, required=False, help="Average level of party"
)
@api.route("/monster-sets")
class MonsterSets(Resource):
@api.doc(parser=tag_parser)
def get(self):
"""Returns list "monster_set" matching supplied tags."""
args = tag_parser.parse_args()
return monster_manual.get_monster_sets(
all_tags=args["all_tags"],
any_tags=args["any_tags"],
none_tags=args["none_tags"],
)
@api.route("/encounter-tags")
class EncounterTags(Resource):
def get(self):
"""List of valid tags for monster sets"""
return monster_manual.get_tags()
@api.route("/encounter")
class Encounter(Resource):
@api.doc(parser=parser)
def get(self):
"""Returns a random encounter meeting the supplied specifications."""
args = parser.parse_args()
character_level_dict = Counter(args["character_levels"])
monster_sets = args["monster_sets"]
difficulty = args["difficulty"]
if not args["difficulty"] is None and args["difficulty"] not in [
"medium",
"easy",
"hard",
]:
raise BadRequest("Invalid difficulty value")
if not all(
[monster_set in monster_manual.monster_sets for monster_set in monster_sets]
):
raise BadRequest("One or more invalid monster sets in request")
if not all([level <= 20 for level in character_level_dict.keys()]):
raise BadRequest("Maximum character level is 20")
source = EncounterSource(
character_level_dict=character_level_dict, monster_sets=monster_sets
)
encounter = source.get_encounter(difficulty=difficulty)
return encounter
@api.route("/dungeon")
class Dungeon(Resource):
@api.doc(parser=dungeon_parser)
def get(self):
"""Returns JSON representation of a random dungeon"""
args = dungeon_parser.parse_args()
level = args["level"]
if args.get("guid"):
guid = args["guid"]
else:
guid = str(uuid.uuid4())
url = str(level) + "&guid=%s" % guid
if args.get("templates"):
templates = args["templates"].split(",")
url += "&templates=%s" % args["templates"]
else:
templates = None
if args.get("terrain"):
terrain = urllib.parse.unquote(args["terrain"])
url += "&terrain=%s" % args["terrain"]
else:
terrain = None
if request.args.get("dungeon_type"):
base_type = request.args["dungeon_type"]
url += "&dungeon_type=%s" % base_type
else:
base_type = None
if args.get("main_antagonist"):
main_antagonist = urllib.parse.unquote(args["main_antagonist"])
url += "&main_antagonist=%s" % args["main_antagonist"]
else:
main_antagonist = None
base_type = args.get("dungeon_type")
state = Random(guid)
application.logger.debug(
"Making a dungeon %s %s %s %s"
% (level, base_type, main_antagonist, terrain)
)
d = DungeonSource(
level,
random_state=state,
base_type=base_type,
main_antagonist=main_antagonist,
templates=templates,
terrain=terrain,
)
module = d.get_dungeon()
return jsonify({"dungeon": module, "url": url})
@api.route("/dungeon-tags")
class DungeonTags(Resource):
@api.doc(parser=dungeon_tags_parser)
def get(self):
args = dungeon_tags_parser.parse_args()
if args.get("level"):
level = args["level"]
else:
level = None
"""JSON blob of valid dungeon parameters"""
any_tags = [
"dungeon-explorer",
"undead",
"beast",
"evil",
"cave-dweller",
"guardian",
"aberration",
]
none_tags = ["rare"]
valid_params = {
"terrain": [
"forest",
"desert",
"mountains",
"arctic",
"plains",
"hills",
"jungle",
"swamp",
],
"dungeontype": [
"mine",
"temple",
"stronghold",
"tomb",
"cave",
"treasure vault",
"sewer",
],
"antagonists": monster_manual.get_monster_sets(
any_tags=any_tags, none_tags=none_tags
),
"in_level_antagonists": monster_manual.get_monster_sets(level=level),
}
return jsonify(valid_params)
@api.route("/encounter_set")
class EncounterSet(Resource):
@api.doc(parser=encounter_set_parser)
def get(self):
"""Returns a random encounter meeting the supplied specifications."""
args = encounter_set_parser.parse_args()
character_level_dict = Counter(args["character_levels"])
monster_set = args.get("monster_set")
if monster_set is not None and monster_set not in monster_manual.monster_sets:
raise BadRequest("One or more invalid monster sets in request")
if monster_set is None:
monster_set = random.choice(
list(monster_manual.monster_sets.keys()))
if not all([level <= 20 for level in character_level_dict.keys()]):
raise BadRequest("Maximum character level is 20")
source = EncounterSetSource(
character_level_dict=character_level_dict, monster_sets=[
monster_set]
)
return source.get_encounters()
if __name__ == "__main__":
application.run(host="0.0.0.0", debug=True)