Skip to content

Commit

Permalink
Adjusted aliases, can edit and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
eesandoval committed May 6, 2019
1 parent 83e51ba commit e7e5e39
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
24 changes: 11 additions & 13 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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


Expand Down
Binary file modified master.db
Binary file not shown.
53 changes: 53 additions & 0 deletions models/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
6 changes: 3 additions & 3 deletions view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e7e5e39

Please sign in to comment.