diff --git a/controller.py b/controller.py index aefc1fa..2ac5aa3 100644 --- a/controller.py +++ b/controller.py @@ -26,6 +26,7 @@ from models.adventurer import Adventurer from models.wyrmprint import Wyrmprint from models.dragon import Dragon +import models.alias async def query(criteria): try: @@ -198,6 +199,34 @@ 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") + 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() + 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() + 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() + return + def start(): view.start_discord_bot() diff --git a/master.db b/master.db index fc348e4..e269c5e 100644 Binary files a/master.db and b/master.db differ diff --git a/models/adventurer.py b/models/adventurer.py index 3dac3eb..48ada9f 100644 --- a/models/adventurer.py +++ b/models/adventurer.py @@ -58,6 +58,14 @@ def find_adventurers(element=None, weapon=None, skill=None, ability=None, adventurers.append(Adventurer(adventurer[0])) return adventurers + @staticmethod + def get_adventurer_id(name): + with Database("master.db") as db: + result = db.query(Adventurer.id_query_text, (name,)) + if result is None or result == []: + return 0 + return int(result[0][0]) + def __init__(self, name, level=None): self.name = name with Database("master.db") as db: @@ -70,6 +78,8 @@ def __init__(self, name, level=None): def _get_adventurer(self, db): result = db.query(Adventurer.adventurer_query_text, (self.name,)) + if result is None or result == []: + result = db.query(Adventurer.alias_query_text, (self.name,)) if result is None or result == []: return False result = result[0] @@ -162,3 +172,33 @@ def _get_abilities(self, db, level=None): INNER JOIN Abilities Ab ON Ab.AbilityID = AA.AbilityID WHERE 1=1 ''' + alias_query_text = ''' + SELECT A.AdventurerID + , A.Title + , A.Rarity + , ET.Name AS "ElementType" + , WT.Name AS "WeaponType" + , A.MaxHP + , A.MaxSTR + , A.MaxCoOp + , A.Defense + , A.ReleaseDate + , UT.Name AS "UnitType" + , A.Name + , A.Limited + FROM Adventurers A + INNER JOIN ElementTypes ET ON ET.ElementTypeID = A.ElementTypeID + INNER JOIN WeaponTypes WT ON WT.WeaponTypeID = A.WeaponTypeID + INNER JOIN UnitTypes UT ON UT.UnitTypeID = A.UnitTypeID + INNER JOIN Aliases AL ON AL.AdventurerID = A.AdventurerID + AND AL.AdventurerID IS NOT NULL + WHERE AL.AliasText LIKE '%' || ? || '%' COLLATE NOCASE + ORDER BY A.ReleaseDate ASC + LIMIT 1 + ''' + id_query_text = ''' + SELECT AdventurerID + FROM Adventurers + WHERE Name LIKE '%' || ? || '%' COLLATE NOCASE + LIMIT 1 + ''' diff --git a/models/alias.py b/models/alias.py new file mode 100644 index 0000000..d864449 --- /dev/null +++ b/models/alias.py @@ -0,0 +1,47 @@ +''' +MIT License + +Copyright (c) 2019 Enrique Sandoval + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +''' +from models.database import Database + + +create_adventurer_alias_text = ''' + INSERT INTO Aliases (AdventurerID, AliasText) VALUES (?, ?) +''' + +create_wyrmprint_alias_text = ''' + INSERT INTO Aliases (WyrmprintID, AliasText) VALUES (?, ?) +''' + +create_dragon_alias_text = ''' + INSERT INTO Aliases (DragonID, AliasText) VALUES (?, ?) +''' + + +def create_alias(alias_id, text, alias_type): + with Database("master.db") as db: + if alias_type == 0: + db.execute(create_adventurer_alias_text, (alias_id, text,)) + elif alias_type == 1: + db.execute(create_wyrmprint_alias_text, (alias_id, text,)) + elif alias_type == 2: + db.execute(create_dragon_alias_text, (alias_id, text,)) diff --git a/models/dragon.py b/models/dragon.py index 35c2a7b..0dba949 100644 --- a/models/dragon.py +++ b/models/dragon.py @@ -58,6 +58,14 @@ def find_dragons(element=None, skill=None, ability=None, rarity=None, dragons.append(Dragon(dragon[0], level)) return dragons + @staticmethod + def get_dragon_id(name): + with Database("master.db") as db: + result = db.query(Dragon.id_query_text, (name,)) + if result is None or result == []: + return 0 + return int(result[0][0]) + def __init__(self, name, level): self.name = name with Database("master.db") as db: @@ -71,6 +79,8 @@ def __init__(self, name, level): def _get_dragon(self, db): result = db.query(Dragon.dragon_query_text, (self.name,)) + if result is None or result == []: + result = db.query(Dragon.alias_query_text, (self.name,)) if result is None or result == []: return False result = result[0] @@ -148,3 +158,26 @@ def _get_abilities(self, db, level): INNER JOIN Abilities A ON A.AbilityID = DA.AbilityID WHERE 1=1 ''' + alias_query_text = ''' + SELECT D.DragonID + , ET.Name AS "ElementTypeName" + , D.Rarity + , D.HP + , D.STR + , D.ReleaseDate + , D.Name + , D.Limited + FROM Dragons D + INNER JOIN ElementTypes ET ON ET.ElementTypeID = D.ElementTypeID + INNER JOIN Aliases AL ON AL.DragonID = D.DragonID + AND AL.DragonID IS NOT NULL + WHERE AL.AliasText LIKE '%' || ? || '%' COLLATE NOCASE + ORDER BY D.ReleaseDate ASC + LIMIT 1 + ''' + id_query_text = ''' + SELECT DragonID + FROM Dragons + WHERE Name LIKE '%' || ? || '%' COLLATE NOCASE + LIMIT 1 + ''' diff --git a/models/wyrmprint.py b/models/wyrmprint.py index 568082c..0afa124 100644 --- a/models/wyrmprint.py +++ b/models/wyrmprint.py @@ -50,6 +50,14 @@ def find_wyrmprints(ability=None, rarity=None, level=3): wyrmprints.append(Wyrmprint(wyrmprint[0], wyrmprint[1])) return wyrmprints + @staticmethod + def get_wyrmprint_id(name): + with Database("master.db") as db: + result = db.query(Wyrmprint.id_query_text, (name,)) + if result is None or result == []: + return 0 + return int(result[0][0]) + def __init__(self, name, level=3): self.name = name with Database("master.db") as db: @@ -61,6 +69,8 @@ def __init__(self, name, level=3): def _get_wyrmprint(self, db): result = db.query(Wyrmprint.wyrmprint_query_text, (self.name,)) + if result is None or result == []: + result = db.query(Wyrmprint.alias_query_text, (self.name,)) if result is None or result == []: return False result = result[0] @@ -111,3 +121,24 @@ def _get_abilities(self, db, level): INNER JOIN Abilities A ON A.AbilityID = WA.AbilityID WHERE 1=1 ''' + alias_query_text = ''' + SELECT W.WyrmprintID + , W.Rarity + , W.MaxHP + , W.MaxSTR + , W.ReleaseDate + , W.Name + , W.Limited + FROM Wyrmprints W + INNER JOIN Aliases AL ON AL.WyrmprintID = W.WyrmprintID + AND AL.WyrmprintID IS NOT NULL + WHERE AL.AliasText LIKE '%' || ? || '%' COLLATE NOCASE + ORDER BY W.ReleaseDate ASC + LIMIT 1 + ''' + id_query_text = ''' + SELECT WyrmprintID + FROM Wyrmprints + WHERE Name LIKE '%' || ? || '%' COLLATE NOCASE + LIMIT 1 + ''' diff --git a/view.py b/view.py index 2fdfacd..86ceda5 100644 --- a/view.py +++ b/view.py @@ -112,6 +112,17 @@ async def update(context): await show_completed_update() +@client.command(name="alias", + description="Creates a new alias to search by", + brief="Creates a new alias to search by", + aliases=["al", "ali", "alia"], + pass_context=True) +async def alias(context): + message = handle_context(context) + await client.send_typing(channel) + await controller.create_alias(message) + + def handle_context(context): global channel channel = context.message.channel @@ -415,6 +426,11 @@ async def show_completed_update(): await client.send_message(channel, "Update complete") +@client.event +async def show_completed_alias(): + await client.send_message(channel, "Alias created") + + @client.event async def clear_active_messages(): global all_active_messages, active_adventurer_messages, \