mirror of
https://github.com/Lost-MSth/Arcaea-server.git
synced 2026-02-08 08:47:32 +08:00
Fix a bug and add a new thing
- Add support for logging Arcaea's errors - Fix a bug when world maps' data don't have some unnecessary parts the client of iOS may break down
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import functools
|
||||
from functools import wraps
|
||||
from traceback import format_exc
|
||||
|
||||
from core.api_user import APIUser
|
||||
from core.error import ArcError, NoAccess, PostError
|
||||
from core.sql import Connect
|
||||
from flask import current_app
|
||||
from setting import Config
|
||||
|
||||
from .api_code import error_return
|
||||
@@ -11,7 +13,7 @@ from .api_code import error_return
|
||||
def role_required(request, powers=[]):
|
||||
'''api token验证,写成了修饰器'''
|
||||
def decorator(view):
|
||||
@functools.wraps(view)
|
||||
@wraps(view)
|
||||
def wrapped_view(*args, **kwargs):
|
||||
try:
|
||||
request.json # 检查请求json格式
|
||||
@@ -56,7 +58,7 @@ def request_json_handle(request, required_keys=[], optional_keys=[]):
|
||||
'''
|
||||
|
||||
def decorator(view):
|
||||
@functools.wraps(view)
|
||||
@wraps(view)
|
||||
def wrapped_view(*args, **kwargs):
|
||||
|
||||
data = {}
|
||||
@@ -73,3 +75,21 @@ def request_json_handle(request, required_keys=[], optional_keys=[]):
|
||||
|
||||
return wrapped_view
|
||||
return decorator
|
||||
|
||||
|
||||
def api_try(view):
|
||||
'''替代try/except,记录`ArcError`为warning'''
|
||||
@wraps(view)
|
||||
def wrapped_view(*args, **kwargs):
|
||||
try:
|
||||
data = view(*args, **kwargs)
|
||||
if data is None:
|
||||
return error_return()
|
||||
else:
|
||||
return data
|
||||
except ArcError as e:
|
||||
if Config.ALLOW_WARNING_LOG:
|
||||
current_app.logger.warning(format_exc())
|
||||
return error_return(e, e.status)
|
||||
|
||||
return wrapped_view
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
from core.error import ArcError, NoData
|
||||
from core.error import NoData
|
||||
from core.song import Song
|
||||
from core.sql import Connect, Query, Sql
|
||||
from flask import Blueprint, request
|
||||
|
||||
from .api_auth import request_json_handle, role_required
|
||||
from .api_code import error_return, success_return
|
||||
from .api_auth import api_try, request_json_handle, role_required
|
||||
from .api_code import success_return
|
||||
from .constant import Constant
|
||||
|
||||
bp = Blueprint('songs', __name__, url_prefix='/songs')
|
||||
@@ -12,37 +12,31 @@ bp = Blueprint('songs', __name__, url_prefix='/songs')
|
||||
|
||||
@bp.route('/<string:song_id>', methods=['GET'])
|
||||
@role_required(request, ['select', 'select_song_info'])
|
||||
@api_try
|
||||
def songs_song_get(user, song_id):
|
||||
'''查询歌曲信息'''
|
||||
with Connect() as c:
|
||||
try:
|
||||
s = Song(c, song_id).select()
|
||||
return success_return(s.to_dict())
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
s = Song(c, song_id).select()
|
||||
return success_return(s.to_dict())
|
||||
|
||||
|
||||
@bp.route('', methods=['GET'])
|
||||
@role_required(request, ['select', 'select_song_info'])
|
||||
@request_json_handle(request, optional_keys=Constant.QUERY_KEYS)
|
||||
@api_try
|
||||
def songs_get(data, user):
|
||||
'''查询全歌曲信息'''
|
||||
A = ['song_id', 'name']
|
||||
B = ['song_id', 'name', 'rating_pst',
|
||||
'rating_prs', 'rating_ftr', 'rating_byn']
|
||||
with Connect() as c:
|
||||
try:
|
||||
query = Query(A, A, B).from_data(data)
|
||||
x = Sql(c).select('chart', query=query)
|
||||
r = []
|
||||
for i in x:
|
||||
r.append(Song(c).from_list(i))
|
||||
query = Query(A, A, B).from_data(data)
|
||||
x = Sql(c).select('chart', query=query)
|
||||
r = []
|
||||
for i in x:
|
||||
r.append(Song(c).from_list(i))
|
||||
|
||||
if not r:
|
||||
raise NoData(api_error_code=-2)
|
||||
if not r:
|
||||
raise NoData(api_error_code=-2)
|
||||
|
||||
return success_return([x.to_dict() for x in r])
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
return success_return([x.to_dict() for x in r])
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
from base64 import b64decode
|
||||
|
||||
from core.api_user import APIUser
|
||||
from core.error import ArcError, PostError
|
||||
from core.error import PostError
|
||||
from core.sql import Connect
|
||||
from flask import Blueprint, request
|
||||
|
||||
from .api_auth import request_json_handle, role_required
|
||||
from .api_auth import api_try, request_json_handle, role_required
|
||||
from .api_code import error_return, success_return
|
||||
|
||||
bp = Blueprint('token', __name__, url_prefix='/token')
|
||||
@@ -13,6 +13,7 @@ bp = Blueprint('token', __name__, url_prefix='/token')
|
||||
|
||||
@bp.route('', methods=['POST'])
|
||||
@request_json_handle(request, required_keys=['auth'])
|
||||
@api_try
|
||||
def token_post(data):
|
||||
'''
|
||||
登录,获取token\
|
||||
@@ -27,17 +28,14 @@ def token_post(data):
|
||||
name, password = auth_decode.split(':', 1)
|
||||
|
||||
with Connect() as c:
|
||||
try:
|
||||
user = APIUser(c)
|
||||
user.login(name, password, request.remote_addr)
|
||||
return success_return({'token': user.token, 'user_id': user.user_id})
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
user = APIUser(c)
|
||||
user.login(name, password, request.remote_addr)
|
||||
return success_return({'token': user.token, 'user_id': user.user_id})
|
||||
|
||||
|
||||
@bp.route('', methods=['GET'])
|
||||
@role_required(request, ['select_me', 'select'])
|
||||
@api_try
|
||||
def token_get(user):
|
||||
'''判断登录有效性'''
|
||||
return success_return()
|
||||
@@ -45,13 +43,10 @@ def token_get(user):
|
||||
|
||||
@bp.route('', methods=['DELETE'])
|
||||
@role_required(request, ['change_me', 'select_me', 'select'])
|
||||
@api_try
|
||||
def token_delete(user):
|
||||
'''登出'''
|
||||
with Connect() as c:
|
||||
try:
|
||||
user.c = c
|
||||
user.logout()
|
||||
return success_return()
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
user.c = c
|
||||
user.logout()
|
||||
return success_return()
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
from core.error import ArcError, InputError, NoAccess, NoData
|
||||
from core.error import InputError, NoAccess, NoData
|
||||
from core.score import Potential, UserScoreList
|
||||
from core.sql import Connect, Query, Sql
|
||||
from core.user import UserInfo, UserRegister
|
||||
from flask import Blueprint, request
|
||||
|
||||
from .api_auth import request_json_handle, role_required
|
||||
from .api_auth import api_try, request_json_handle, role_required
|
||||
from .api_code import error_return, success_return
|
||||
from .constant import Constant
|
||||
|
||||
@@ -14,59 +14,54 @@ bp = Blueprint('users', __name__, url_prefix='/users')
|
||||
@bp.route('', methods=['POST'])
|
||||
@role_required(request, ['change'])
|
||||
@request_json_handle(request, ['name', 'password', 'email'])
|
||||
@api_try
|
||||
def users_post(data, _):
|
||||
'''注册一个用户'''
|
||||
with Connect() as c:
|
||||
new_user = UserRegister(c)
|
||||
try:
|
||||
new_user.set_name(data['name'])
|
||||
new_user.set_password(data['password'])
|
||||
new_user.set_email(data['email'])
|
||||
new_user.register()
|
||||
return success_return({'user_id': new_user.user_id, 'user_code': new_user.user_code})
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
new_user.set_name(data['name'])
|
||||
new_user.set_password(data['password'])
|
||||
new_user.set_email(data['email'])
|
||||
new_user.register()
|
||||
return success_return({'user_id': new_user.user_id, 'user_code': new_user.user_code})
|
||||
|
||||
|
||||
@bp.route('', methods=['GET'])
|
||||
@role_required(request, ['select'])
|
||||
@request_json_handle(request, optional_keys=Constant.QUERY_KEYS)
|
||||
@api_try
|
||||
def users_get(data, user):
|
||||
'''查询全用户信息'''
|
||||
A = ['user_id', 'name', 'user_code']
|
||||
B = ['user_id', 'name', 'user_code', 'join_date',
|
||||
'rating_ptt', 'time_played', 'ticket', 'world_rank_score']
|
||||
with Connect() as c:
|
||||
try:
|
||||
query = Query(A, A, B).from_data(data)
|
||||
x = Sql(c).select('user', query=query)
|
||||
r = []
|
||||
for i in x:
|
||||
r.append(UserInfo(c).from_list(i))
|
||||
query = Query(A, A, B).from_data(data)
|
||||
x = Sql(c).select('user', query=query)
|
||||
r = []
|
||||
for i in x:
|
||||
r.append(UserInfo(c).from_list(i))
|
||||
|
||||
if not r:
|
||||
raise NoData(api_error_code=-2)
|
||||
if not r:
|
||||
raise NoData(api_error_code=-2)
|
||||
|
||||
return success_return([{
|
||||
'user_id': x.user_id,
|
||||
'name': x.name,
|
||||
'join_date': x.join_date,
|
||||
'user_code': x.user_code,
|
||||
'rating_ptt': x.rating_ptt/100,
|
||||
'character_id': x.character.character_id,
|
||||
'is_char_uncapped': x.character.is_uncapped,
|
||||
'is_char_uncapped_override': x.character.is_uncapped_override,
|
||||
'is_hide_rating': x.is_hide_rating,
|
||||
'ticket': x.ticket
|
||||
} for x in r])
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
return success_return([{
|
||||
'user_id': x.user_id,
|
||||
'name': x.name,
|
||||
'join_date': x.join_date,
|
||||
'user_code': x.user_code,
|
||||
'rating_ptt': x.rating_ptt/100,
|
||||
'character_id': x.character.character_id,
|
||||
'is_char_uncapped': x.character.is_uncapped,
|
||||
'is_char_uncapped_override': x.character.is_uncapped_override,
|
||||
'is_hide_rating': x.is_hide_rating,
|
||||
'ticket': x.ticket
|
||||
} for x in r])
|
||||
|
||||
|
||||
@bp.route('/<int:user_id>', methods=['GET'])
|
||||
@role_required(request, ['select', 'select_me'])
|
||||
@api_try
|
||||
def users_user_get(user, user_id):
|
||||
'''查询用户信息'''
|
||||
if user_id <= 0:
|
||||
@@ -76,16 +71,13 @@ def users_user_get(user, user_id):
|
||||
return error_return(NoAccess('No permission', api_error_code=-1), 403)
|
||||
|
||||
with Connect() as c:
|
||||
try:
|
||||
u = UserInfo(c, user_id)
|
||||
return success_return(u.to_dict())
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
u = UserInfo(c, user_id)
|
||||
return success_return(u.to_dict())
|
||||
|
||||
|
||||
@bp.route('/<int:user_id>/b30', methods=['GET'])
|
||||
@role_required(request, ['select', 'select_me'])
|
||||
@api_try
|
||||
def users_user_b30_get(user, user_id):
|
||||
'''查询用户b30'''
|
||||
if user_id <= 0:
|
||||
@@ -95,21 +87,18 @@ def users_user_b30_get(user, user_id):
|
||||
return error_return(NoAccess('No permission', api_error_code=-1), 403)
|
||||
|
||||
with Connect() as c:
|
||||
try:
|
||||
x = UserScoreList(c, UserInfo(c, user_id))
|
||||
x.query.limit = 30
|
||||
x.select_from_user()
|
||||
r = x.to_dict_list()
|
||||
rating_sum = sum([i.rating for i in x.scores])
|
||||
return success_return({'user_id': user_id, 'b30_ptt': rating_sum / 30, 'data': r})
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
x = UserScoreList(c, UserInfo(c, user_id))
|
||||
x.query.limit = 30
|
||||
x.select_from_user()
|
||||
r = x.to_dict_list()
|
||||
rating_sum = sum([i.rating for i in x.scores])
|
||||
return success_return({'user_id': user_id, 'b30_ptt': rating_sum / 30, 'data': r})
|
||||
|
||||
|
||||
@bp.route('/<int:user_id>/best', methods=['GET'])
|
||||
@role_required(request, ['select', 'select_me'])
|
||||
@request_json_handle(request, optional_keys=Constant.QUERY_KEYS)
|
||||
@api_try
|
||||
def users_user_best_get(data, user, user_id):
|
||||
'''查询用户所有best成绩'''
|
||||
if user_id <= 0:
|
||||
@@ -119,19 +108,16 @@ def users_user_best_get(data, user, user_id):
|
||||
return error_return(NoAccess('No permission', api_error_code=-1), 403)
|
||||
|
||||
with Connect() as c:
|
||||
try:
|
||||
x = UserScoreList(c, UserInfo(c, user_id))
|
||||
x.query.from_data(data)
|
||||
x.select_from_user()
|
||||
r = x.to_dict_list()
|
||||
return success_return({'user_id': user_id, 'data': r})
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
x = UserScoreList(c, UserInfo(c, user_id))
|
||||
x.query.from_data(data)
|
||||
x.select_from_user()
|
||||
r = x.to_dict_list()
|
||||
return success_return({'user_id': user_id, 'data': r})
|
||||
|
||||
|
||||
@bp.route('/<int:user_id>/r30', methods=['GET'])
|
||||
@role_required(request, ['select', 'select_me'])
|
||||
@api_try
|
||||
def users_user_r30_get(user, user_id):
|
||||
'''查询用户r30'''
|
||||
|
||||
@@ -142,9 +128,5 @@ def users_user_r30_get(user, user_id):
|
||||
return error_return(NoAccess('No permission', api_error_code=-1), 403)
|
||||
|
||||
with Connect() as c:
|
||||
try:
|
||||
p = Potential(c, UserInfo(c, user_id))
|
||||
return success_return({'user_id': user_id, 'r10_ptt': p.recent_10 / 10, 'data': p.recent_30_to_dict_list()})
|
||||
except ArcError as e:
|
||||
return error_return(e)
|
||||
return error_return()
|
||||
p = Potential(c, UserInfo(c, user_id))
|
||||
return success_return({'user_id': user_id, 'r10_ptt': p.recent_10 / 10, 'data': p.recent_30_to_dict_list()})
|
||||
|
||||
Reference in New Issue
Block a user