Skip to content

Commit

Permalink
some attack automation endpoint bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zhudotexe committed Oct 3, 2019
1 parent a0aaad9 commit 4975bf1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
27 changes: 12 additions & 15 deletions blueprints/characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from app import mdb
from lib.discord import get_user_info
from lib.utils import jsonify
from lib.validation import is_valid_automation
from lib.validation import is_valid_automation, check_automation

characters = Blueprint('characters', __name__)

Expand All @@ -20,26 +20,23 @@ def character_list():
def meta():
user = get_user_info()
data = list(mdb.characters.find({"owner": user.id},
["upstream", "active", "name", "description", "image", "levels"]))
["upstream", "active", "name", "description", "image", "levels", "import_version"]))
return jsonify(data)


@characters.route('/<_id>/attacks', methods=["GET"])
def attacks(_id):
@characters.route('/<upstream>/attacks', methods=["GET"])
def attacks(upstream):
"""Returns a character's overriden attacks."""
user = get_user_info()
char_id = ObjectId(_id)
data = mdb.characters.find_one({"owner": user.id, "_id": char_id},
data = mdb.characters.find_one({"owner": user.id, "upstream": upstream},
["overrides"])
return jsonify(data['attacks'])
return jsonify(data['overrides']['attacks'])


@characters.route('/<_id>/attacks', methods=["POST"])
def put_attacks(_id):
"""Sets a character's attack overrides. Must POST a list of attacks."""
@characters.route('/<upstream>/attacks', methods=["PUT"])
def put_attacks(upstream):
"""Sets a character's attack overrides. Must PUT a list of attacks."""
user = get_user_info()
char_id = ObjectId(_id)

the_attacks = request.json

# validation
Expand All @@ -48,17 +45,17 @@ def put_attacks(_id):

for attack in the_attacks:
if not all((isinstance(attack, dict),
set(attack.keys()) == {"name", "automation"},
set(attack.keys()) == {"name", "automation", "_v"},
is_valid_automation(attack['automation']))):
return "Invalid attack", 400

# write
response = mdb.characters.update_one(
{"owner": user.id, "_id": char_id},
{"owner": user.id, "upstream": upstream},
{"$set": {"overrides.attacks": the_attacks}}
)

# respond
if not response.modified_count:
if not response.matched_count:
return "Character not found", 404
return "Attacks updated."
13 changes: 9 additions & 4 deletions lib/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def ensure_spell_keys(spell):
def is_valid_automation(automation):
try:
check_automation(automation)
except AssertionError:
except:
return False
return True

Expand Down Expand Up @@ -69,15 +69,17 @@ def check_damage(effect):
if 'higher' in effect:
check_higher(effect['higher'])
if 'cantripScale' in effect:
assert isinstance(effect['cantripScale'], bool), "CantripScale must be boolean"
assert isinstance(effect['cantripScale'], bool) or effect['cantripScale'] is None, \
"CantripScale must be boolean"


def check_temphp(effect):
assert 'amount' in effect, "TempHP effect must have amount"
if 'higher' in effect:
check_higher(effect['higher'])
if 'cantripScale' in effect:
assert isinstance(effect['cantripScale'], bool), "CantripScale must be boolean"
assert isinstance(effect['cantripScale'], bool) or effect['cantripScale'] is None, \
"CantripScale must be boolean"


def check_ieffect(effect):
Expand All @@ -99,7 +101,8 @@ def check_roll(effect):
if 'higher' in effect:
check_higher(effect['higher'])
if 'cantripScale' in effect:
assert isinstance(effect['cantripScale'], bool), "CantripScale must be boolean"
assert isinstance(effect['cantripScale'], bool) or effect['cantripScale'] is None, \
"CantripScale must be boolean"
if 'hidden' in effect:
assert isinstance(effect['hidden'], bool), "Hidden must be boolean"

Expand All @@ -122,6 +125,8 @@ def check_text(effect):


def check_higher(higher):
if higher is None:
return
for k, v in higher.items():
assert isinstance(k, (str, int)), "Higher level key must be int or string"
assert isinstance(v, str), "Higher level value must be string"
Expand Down

0 comments on commit 4975bf1

Please sign in to comment.