questable_bot/server.py

49 lines
1.2 KiB
Python
Raw 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-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:
token = request.args['token']
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'])