diff --git a/controller.py b/controller.py index 2ac5aa3..eb3df71 100644 --- a/controller.py +++ b/controller.py @@ -26,7 +26,7 @@ from models.adventurer import Adventurer from models.wyrmprint import Wyrmprint from models.dragon import Dragon -import models.alias +from models.alias import create_update_alias, delete_alias async def query(criteria): try: @@ -199,32 +199,30 @@ async def process_dragon_reaction(emoji, dragon, message): elif emoji == "\U0001F509": # 2 unbinds await process_dragon(dragon.name, 2, message) -async def create_alias(message): - print(message) - if ',' not in message or len(message.split(',')) != 2: - await view.show_missing_criteria("Missing alias, name, or format") +async def handle_alias(message): + if ',' not in message or len(message.split(',')) < 1: + result = delete_alias(message) + await view.show_completed_alias(result) return alias = message.split(',')[0].strip() name = message.split(',')[1].strip() alias_type = 0 - print(name) - print(alias) aliased_id = Adventurer.get_adventurer_id(name) if aliased_id != 0: - models.alias.create_alias(aliased_id, alias, alias_type) - await view.show_completed_alias() + result = create_update_alias(aliased_id, alias, alias_type) + await view.show_completed_alias(result) return alias_type += 1 aliased_id = Wyrmprint.get_wyrmprint_id(name) if aliased_id != 0: - models.alias.create_alias(aliased_id, alias, alias_type) - await view.show_completed_alias() + result = create_update_alias(aliased_id, alias, alias_type) + await view.show_completed_alias(result) return alias_type += 1 aliased_id = Dragon.get_dragon_id(name) if aliased_id != 0: - models.alias.create_alias(aliased_id, alias, alias_type) - await view.show_completed_alias() + result = create_update_alias(aliased_id, alias, alias_type) + await view.show_completed_alias(result) return diff --git a/master.db b/master.db index e269c5e..7883e70 100644 Binary files a/master.db and b/master.db differ diff --git a/models/alias.py b/models/alias.py index d864449..d948270 100644 --- a/models/alias.py +++ b/models/alias.py @@ -36,6 +36,47 @@ INSERT INTO Aliases (DragonID, AliasText) VALUES (?, ?) ''' +update_adventurer_alias_text = ''' + UPDATE Aliases + SET AdventurerID = ? + WHERE AliasText = ? COLLATE NOCASE +''' + +update_wyrmprint_alias_text = ''' + UPDATE Aliases + SET WyrmprintID = ? + WHERE AliasText = ? COLLATE NOCASE +''' + +update_dragon_alias_text = ''' + UPDATE Aliases + SET DragonID = ? + WHERE AliasText = ? COLLATE NOCASE +''' + +find_alias_text = ''' + SELECT 1 FROM Aliases WHERE AliasText = ? COLLATE NOCASE +''' + +delete_alias_text = ''' + DELETE FROM Aliases WHERE AliasText = ? COLLATE NOCASE +''' + + +def delete_alias(text): + with Database("master.db") as db: + db.execute(delete_alias_text, (text,)) + return "Deleted" + + +def create_update_alias(alias_id, text, alias_type): + with Database("master.db") as db: + resultset = db.query(find_alias_text, (text,)) + if resultset is not None and len(resultset) > 0: + return update_alias(alias_id, text, alias_type) + else: + return create_alias(alias_id, text, alias_type) + def create_alias(alias_id, text, alias_type): with Database("master.db") as db: @@ -45,3 +86,15 @@ def create_alias(alias_id, text, alias_type): db.execute(create_wyrmprint_alias_text, (alias_id, text,)) elif alias_type == 2: db.execute(create_dragon_alias_text, (alias_id, text,)) + return "Created" + + +def update_alias(alias_id, text, alias_type): + with Database("master.db") as db: + if alias_type == 0: + db.execute(update_adventurer_alias_text, (alias_id, text,)) + elif alias_type == 1: + db.execute(update_wyrmprint_alias_text, (alias_id, text,)) + elif alias_type == 2: + db.execute(update_dragon_alias_text, (alias_id, text,)) + return "Updated" diff --git a/view.py b/view.py index 86ceda5..7362cfd 100644 --- a/view.py +++ b/view.py @@ -120,7 +120,7 @@ async def update(context): async def alias(context): message = handle_context(context) await client.send_typing(channel) - await controller.create_alias(message) + await controller.handle_alias(message) def handle_context(context): @@ -427,8 +427,8 @@ async def show_completed_update(): @client.event -async def show_completed_alias(): - await client.send_message(channel, "Alias created") +async def show_completed_alias(status): + await client.send_message(channel, "Alias {0}".format(status)) @client.event