Complete adding quests and side quests.

Add add_name, add_imp, add_diff functions. Add states and relevant if
clauses. Add state of quest on first add.
This commit is contained in:
Ceda EI 2018-11-01 16:46:45 +05:30
parent 260d1f6fde
commit 7fca63a2ab
1 changed files with 109 additions and 3 deletions

112
bot.py
View File

@ -45,12 +45,12 @@ def add_quest(bot, update, player, type="quest"):
x.sort(key=lambda i: i.QID, reverse=True) x.sort(key=lambda i: i.QID, reverse=True)
qid = x[0].QID + 1 qid = x[0].QID + 1
# Add quest / sub_quest # Add quest / side_quest
if type == 'quest': if type == 'quest':
questable.add_quest(player.DB, player.CHAT_ID, qid) questable.add_quest(player.DB, player.CHAT_ID, qid, state=0)
player.set_state('aq', qid) player.set_state('aq', qid)
if type == 'side_quest': if type == 'side_quest':
questable.add_side_quest(player.DB, player.CHAT_ID, qid) questable.add_side_quest(player.DB, player.CHAT_ID, qid, state=0)
player.set_state('asq', qid) player.set_state('asq', qid)
# Send message # Send message
@ -61,15 +61,121 @@ def add_quest(bot, update, player, type="quest"):
bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup) bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup)
def add_name(bot, update, player, type, qid):
if type == "quest":
x = questable.get_quest(player.DB, player.CHAT_ID, qid)
player.set_state('qd', qid)
elif type == "side_quest":
x = questable.get_side_quest(player.DB, player.CHAT_ID, qid)
player.set_state('sqd', qid)
else:
raise ValueError('Not quest or side_quest')
x.name = update.message.text
x.update_db()
chat_id = update.message.chat_id
text = "How difficult is it?"
custom_keyboard = [["Low", "Medium", "High"]]
reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup)
def add_diff(bot, update, player, type, qid):
message = update.message.text
chat_id = update.message.chat_id
if message == "Low":
diff = 1
elif message == "Medium":
diff = 2
elif message == "High":
diff = 3
else:
bot.send_message(chat_id=chat_id, text="Invalid Option")
return False
if type == "quest":
x = questable.get_quest(player.DB, player.CHAT_ID, qid)
player.set_state('qi', qid)
elif type == "side_quest":
x = questable.get_side_quest(player.DB, player.CHAT_ID, qid)
player.set_state('sqi', qid)
else:
raise ValueError('Not quest or side_quest')
x.diff = diff
x.update_db()
text = "How important is it?"
custom_keyboard = [["Low", "Medium", "High"]]
reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup)
def add_imp(bot, update, player, type, qid):
message = update.message.text
chat_id = update.message.chat_id
if message == "Low":
imp = 1
elif message == "Medium":
imp = 2
elif message == "High":
imp = 3
else:
bot.send_message(chat_id=chat_id, text="Invalid Option")
return False
if type == "quest":
x = questable.get_quest(player.DB, player.CHAT_ID, qid)
player.set_state('none', 0)
elif type == "side_quest":
x = questable.get_side_quest(player.DB, player.CHAT_ID, qid)
player.set_state('none', 0)
else:
raise ValueError('Not quest or side_quest')
x.imp = imp
x.update_db()
text = "Quest Added!"
custom_keyboard = [
['Add Quest', 'Add Side Quest'],
['List Quests', 'List Side Quests']
]
reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup)
def message_handling(bot, update, db): def message_handling(bot, update, db):
text = update.message.text text = update.message.text
player = questable.player(db, update.message.chat_id) player = questable.player(db, update.message.chat_id)
state = player.get_state() state = player.get_state()
# states
# none: in the middle of nothing
# aq / asq: Added Quest: User has pressed add Quest/Side Quest
# qd / sqd: (Side) Quest difficulty: User has entered title, difficulty
# requested
# qi / sqi: (Side) Quest importance: User has entered difficulty,
# importance requested
if state["state"] == "none": if state["state"] == "none":
if text == "Add Quest": if text == "Add Quest":
add_quest(bot, update, player) add_quest(bot, update, player)
elif text == "Add Side Quest": elif text == "Add Side Quest":
add_quest(bot, update, player, "side_quest") add_quest(bot, update, player, "side_quest")
elif state["state"] == "aq":
add_name(bot, update, player, "quest", state["extra"])
elif state["state"] == "asq":
add_name(bot, update, player, "side_quest", state["extra"])
elif state["state"] == "qd":
add_diff(bot, update, player, "quest", state["extra"])
elif state["state"] == "sqd":
add_diff(bot, update, player, "side_quest", state["extra"])
elif state["state"] == "qi":
add_imp(bot, update, player, "quest", state["extra"])
elif state["state"] == "sqi":
add_imp(bot, update, player, "side_quest", state["extra"])
db = sqlite3.connect("questable.db", check_same_thread=False) db = sqlite3.connect("questable.db", check_same_thread=False)