From 83e51ba9b5ae64566dc97ca77e953cbba42c5cea Mon Sep 17 00:00:00 2001 From: Enrique Sandoval Date: Fri, 3 May 2019 14:22:48 -0700 Subject: [PATCH] Added aliases and alias creation Allowed for aliases to specify different names for wyrmprints, dragons, or adventurers Built in aliases come by default for event characters New aliases can be created with the alias command --- controller.py | 29 ++++++++++++++++++++++++++ master.db | Bin 536576 -> 536576 bytes models/adventurer.py | 40 ++++++++++++++++++++++++++++++++++++ models/alias.py | 47 +++++++++++++++++++++++++++++++++++++++++++ models/dragon.py | 33 ++++++++++++++++++++++++++++++ models/wyrmprint.py | 31 ++++++++++++++++++++++++++++ view.py | 16 +++++++++++++++ 7 files changed, 196 insertions(+) create mode 100644 models/alias.py 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 fc348e4f3adad9e6a80d6621655e635b824764ff..e269c5e3691075c61375dbe8056e582d18ca5af9 100644 GIT binary patch del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k3H{ z>kJGSp2&mV+sGKt;+-75s)|FDaV_;)RiYLrT<9fEkoOWB6LP=>ReQ+{D{K+Eq$g(ZF#J}-;0Z6o3VyW|D!I3GL27f`wv{< BfYty2 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, \