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-13 14:00:35 +01:00
|
|
|
from flask import Flask, jsonify, request
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
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
|
|
|
|
return jsonify({
|
|
|
|
"xp": player.get_points(),
|
|
|
|
"quests_completed": len(player.get_quests()),
|
|
|
|
"total_quests": len(player.get_quests(None)),
|
|
|
|
"side_quests_completed": len(player.get_side_quests()),
|
|
|
|
"total_side_quests": len(player.get_side_quests(None)),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
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 19:33:49 +01:00
|
|
|
qid = request.values['qid']
|
2019-03-13 15:19:55 +01:00
|
|
|
except(KeyError):
|
|
|
|
return jsonify(errors._400), 400
|
|
|
|
|
|
|
|
quest = player.get_quest(qid)
|
|
|
|
if quest is False:
|
|
|
|
return jsonify(errors._404), 404
|
|
|
|
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)
|
|
|
|
if side_quest is False:
|
|
|
|
return jsonify(errors._404), 404
|
|
|
|
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']
|
|
|
|
imp = request.values['priority']
|
|
|
|
diff = request.values['difficulty']
|
|
|
|
except (KeyError):
|
|
|
|
return jsonify(errors._400), 400
|
|
|
|
|
|
|
|
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']
|
|
|
|
imp = request.values['priority']
|
|
|
|
diff = request.values['difficulty']
|
|
|
|
except (KeyError):
|
|
|
|
return jsonify(errors._400), 400
|
|
|
|
|
|
|
|
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'])
|