questable_bot/server.py

376 lines
10 KiB
Python
Raw Permalink Normal View History

2019-03-13 14:00:35 +01:00
#!/usr/bin/env python3
import questable
import sqlite3
2019-03-13 14:22:44 +01:00
import errors
2019-03-16 05:48:09 +01:00
from flask import Flask, jsonify, request, redirect
2019-05-31 07:11:28 +02:00
from flask_cors import CORS
2019-03-13 14:00:35 +01:00
app = Flask(__name__)
2019-05-31 07:11:28 +02:00
CORS(app)
2019-03-13 14:00:35 +01:00
db = sqlite3.connect("questable.db", check_same_thread=False)
2019-03-13 14:22:44 +01:00
# Returns the player object if valid token
def get_player(db):
2019-03-13 14:00:35 +01:00
try:
2019-03-13 19:33:49 +01:00
token = request.values['token']
2019-03-13 14:00:35 +01:00
except (AttributeError):
2019-03-13 14:22:44 +01:00
return False
2019-03-13 14:00:35 +01:00
except (KeyError):
2019-03-13 14:22:44 +01:00
return False
return questable.get_player_from_token(db, token)
# /auth.
def auth(db):
if get_player(db) is False:
return jsonify({"success": False})
2019-03-13 14:00:35 +01:00
else:
2019-03-13 14:22:44 +01:00
return jsonify({"success": True})
2019-03-13 14:00:35 +01:00
app.add_url_rule('/auth', '/auth', lambda: auth(db), methods=['GET'])
2019-03-13 14:22:44 +01:00
# /player
def player(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
2019-03-16 09:53:51 +01:00
noq = len(player.get_quests(None))
nosq = len(player.get_side_quests(None))
2019-03-13 14:22:44 +01:00
return jsonify({
"xp": player.get_points(),
2019-03-16 09:53:51 +01:00
"quests_completed": noq - len(player.get_quests()),
"total_quests": noq,
"side_quests_completed": nosq - len(player.get_side_quests()),
"total_side_quests": nosq,
2019-03-13 14:22:44 +01:00
})
app.add_url_rule('/player', '/player', lambda: player(db), methods=['GET'])
2019-03-13 14:42:45 +01:00
2019-03-13 15:19:55 +01:00
def dictify_quest(quest):
2019-03-13 14:42:45 +01:00
return {
"id": quest.QID,
"name": quest.name,
"difficulty": quest.diff,
"priority": quest.imp,
"state": [False, True][quest.state]
}
# /get_quests
def get_quests(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
2019-03-13 15:19:55 +01:00
quests = list(map(dictify_quest, player.get_quests()))
2019-03-13 14:42:45 +01:00
return jsonify(quests)
app.add_url_rule('/get_quests', '/get_quests', lambda: get_quests(db),
methods=['GET'])
# /get_side_quests
def get_side_quests(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
2019-03-13 15:19:55 +01:00
side_quests = list(map(dictify_quest, player.get_side_quests()))
2019-03-13 14:42:45 +01:00
return jsonify(side_quests)
app.add_url_rule('/get_side_quests', '/get_side_quests',
lambda: get_side_quests(db), methods=['GET'])
2019-03-13 15:19:55 +01:00
# /get_quest
def get_quest(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
try:
2019-03-13 20:19:42 +01:00
qid = request.values['id']
2019-03-13 15:19:55 +01:00
except(KeyError):
return jsonify(errors._400), 400
2019-03-16 10:09:44 +01:00
quest = player.get_quest(qid)
if quest is False:
2019-03-13 15:19:55 +01:00
return jsonify(errors._404), 404
2019-03-15 06:46:24 +01:00
2019-03-13 15:19:55 +01:00
return jsonify(dictify_quest(quest))
app.add_url_rule('/get_quest', '/get_quest', lambda: get_quest(db),
methods=['GET'])
# /get_side_quest
def get_side_quest(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
try:
2019-03-13 19:33:49 +01:00
qid = request.values['id']
2019-03-13 15:19:55 +01:00
except(KeyError):
return jsonify(errors._400), 400
side_quest = player.get_side_quest(qid)
2019-03-15 06:46:24 +01:00
2019-03-16 10:09:44 +01:00
if side_quest is False:
2019-03-13 15:19:55 +01:00
return jsonify(errors._404), 404
2019-03-15 06:46:24 +01:00
2019-03-13 15:19:55 +01:00
return jsonify(dictify_quest(side_quest))
app.add_url_rule('/get_side_quest', '/get_side_quest',
lambda: get_side_quest(db), methods=['GET'])
2019-03-13 19:33:49 +01:00
# /add_quest
def add_quest(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
x = player.get_quests(None)
if len(x) == 0:
qid = 1
else:
x.sort(key=lambda i: i.QID, reverse=True)
qid = x[0].QID + 1
try:
name = request.values['name']
2019-03-13 20:19:42 +01:00
imp = int(request.values['priority'])
diff = int(request.values['difficulty'])
2019-03-13 19:33:49 +01:00
except (KeyError):
return jsonify(errors._400), 400
2019-03-13 20:19:42 +01:00
except (ValueError):
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 20:19:42 +01:00
if imp not in [1, 2, 3] or diff not in [1, 2, 3]:
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 19:33:49 +01:00
quest = questable.add_quest(db, player.CHAT_ID, qid, name, imp, diff, 0)
return jsonify(dictify_quest(quest))
app.add_url_rule('/add_quest', '/add_quest', lambda: add_quest(db),
methods=['POST'])
# /add_side_quest
def add_side_quest(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
x = player.get_side_quests(None)
if len(x) == 0:
qid = 1
else:
x.sort(key=lambda i: i.QID, reverse=True)
qid = x[0].QID + 1
try:
name = request.values['name']
2019-03-13 20:19:42 +01:00
imp = int(request.values['priority'])
diff = int(request.values['difficulty'])
2019-03-13 19:33:49 +01:00
except (KeyError):
return jsonify(errors._400), 400
2019-03-13 20:19:42 +01:00
except (ValueError):
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 20:19:42 +01:00
if imp not in [1, 2, 3] or diff not in [1, 2, 3]:
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 19:33:49 +01:00
quest = questable.add_side_quest(db, player.CHAT_ID, qid, name, imp,
diff, 0)
return jsonify(dictify_quest(quest))
app.add_url_rule('/add_side_quest', '/add_side_quest',
lambda: add_side_quest(db), methods=['POST'])
2019-03-13 20:19:42 +01:00
# /update_quest
def update_quest(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
try:
qid = request.values['id']
except(KeyError):
return jsonify(errors._400), 400
available_keys = [i for i in ['name', 'difficulty', 'priority', 'state']
if i in request.values.keys()]
if len(available_keys) == 0:
return jsonify(errors._400), 400
quest = questable.get_quest(db, player.CHAT_ID, qid)
2019-03-14 15:42:24 +01:00
if quest is False:
return jsonify(errors._404), 404
2019-03-13 20:19:42 +01:00
if quest.state == 1:
return jsonify(dictify_quest(quest))
for i in available_keys:
try:
if i == "name":
quest.name = request.values["name"]
elif i == "difficulty":
diff = int(request.values["difficulty"])
if diff in [1, 2, 3]:
quest.diff = diff
else:
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 20:19:42 +01:00
elif i == "priority":
imp = int(request.values["priority"])
if imp in [1, 2, 3]:
quest.imp = imp
else:
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 20:19:42 +01:00
elif i == "state":
state = (True if str(request.values['state']).lower()
in ["1", "true"] else False)
2019-03-13 20:19:42 +01:00
if state is True:
quest.state = 1
else:
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 20:19:42 +01:00
except (ValueError):
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 20:19:42 +01:00
2019-03-19 12:23:04 +01:00
if quest.state == 1:
points = 55 + 10*quest.imp + 15*quest.diff
player.add_points(points)
2019-03-13 20:19:42 +01:00
quest.update_db()
return jsonify(dictify_quest(quest))
app.add_url_rule('/update_quest', '/update_quest', lambda: update_quest(db),
methods=['POST'])
# /update_side_quest
def update_side_quest(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
try:
qid = request.values['id']
except(KeyError):
return jsonify(errors._400), 400
available_keys = [i for i in ['name', 'difficulty', 'priority', 'state']
if i in request.values.keys()]
if len(available_keys) == 0:
return jsonify(errors._400), 400
quest = questable.get_side_quest(db, player.CHAT_ID, qid)
2019-03-14 15:42:24 +01:00
if quest is False:
return jsonify(errors._404), 404
2019-03-13 20:19:42 +01:00
if quest.state == 1:
return jsonify(dictify_quest(quest))
for i in available_keys:
try:
if i == "name":
quest.name = request.values["name"]
elif i == "difficulty":
diff = int(request.values["difficulty"])
if diff in [1, 2, 3]:
quest.diff = diff
else:
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 20:19:42 +01:00
elif i == "priority":
imp = int(request.values["priority"])
if imp in [1, 2, 3]:
quest.imp = imp
else:
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 20:19:42 +01:00
elif i == "state":
state = (True if str(request.values['state']).lower()
in ["1", "true"] else False)
2019-03-13 20:19:42 +01:00
if state is True:
quest.state = 1
else:
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 20:19:42 +01:00
except (ValueError):
2019-03-16 10:16:47 +01:00
return jsonify(errors._400_bv), 400
2019-03-13 20:19:42 +01:00
2019-03-19 12:23:04 +01:00
if quest.state == 1:
points = 10*quest.imp + 15*quest.diff
player.add_points(points)
2019-03-13 20:19:42 +01:00
quest.update_db()
return jsonify(dictify_quest(quest))
app.add_url_rule('/update_side_quest', '/update_side_quest',
lambda: update_side_quest(db), methods=['POST'])
2019-03-15 06:46:24 +01:00
# /delete_quest
def delete_quest(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
try:
qid = request.values['id']
except(KeyError):
return jsonify(errors._400), 400
try:
quest = questable.get_quest(db, player.CHAT_ID, qid)
except Exception:
return jsonify(errors._404), 404
if quest.state == 1:
return jsonify({"success": False})
quest.delete_from_db()
return jsonify({"success": True})
app.add_url_rule('/delete_quest', '/delete_quest',
lambda: delete_quest(db), methods=['DELETE'])
# /delete_side_quest
def delete_side_quest(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
try:
qid = request.values['id']
except(KeyError):
return jsonify(errors._400), 400
try:
side_quest = questable.get_side_quest(db, player.CHAT_ID, qid)
except Exception:
return jsonify(errors._404), 404
if side_quest.state == 1:
return jsonify({"success": False})
side_quest.delete_from_db()
return jsonify({"success": True})
app.add_url_rule('/delete_side_quest', '/delete_side_quest',
lambda: delete_side_quest(db), methods=['DELETE'])
2019-03-16 05:48:09 +01:00
@app.route('/')
def redirect_to_docs():
return redirect("https://questable.webionite.com/api", code=301)