Skip to content

Commit

Permalink
[Enhance] API song ranklist
Browse files Browse the repository at this point in the history
- Add an API endpoint for getting the rank list of a song's chart #81
- Make the `run.bat` script pause when meeting an error #82
  • Loading branch information
Lost-MSth committed Dec 19, 2022
1 parent 426f65e commit 7324c9b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
27 changes: 26 additions & 1 deletion latest version/api/songs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from core.error import NoData
from core.error import NoData, InputError
from core.rank import RankList
from core.song import Song
from core.sql import Connect, Query, Sql
from flask import Blueprint, request
Expand Down Expand Up @@ -40,3 +41,27 @@ def songs_get(data, user):
raise NoData(api_error_code=-2)

return success_return([x.to_dict() for x in r])


@bp.route('/<string:song_id>/<int:difficulty>/rank', methods=['GET'])
@role_required(request, ['select', 'select_song_rank', 'select_song_rank_top'])
@request_json_handle(request, optional_keys=['limit'])
@api_try
def songs_song_difficulty_rank_get(data, user, song_id, difficulty):
'''查询歌曲某个难度的成绩排行榜,和游戏内接口相似,只允许limit'''
if difficulty not in [0, 1, 2, 3]:
raise InputError('Difficulty must be 0, 1, 2 or 3')
limit = data.get('limit', 20)
if not isinstance(limit, int):
raise InputError('Limit must be int')
if user.role.only_has_powers(['select_song_rank_top'], ['select', 'select_song_rank']):
# 限制低权限只能查询前20名
if limit > 20 or limit < 0:
limit = 20
with Connect() as c:
rank_list = RankList(c)
rank_list.song.set_chart(song_id, difficulty)
rank_list.limit = limit
# 不检查歌曲是否存在,不存在返回的是空列表
rank_list.select_top()
return success_return(rank_list.to_dict_list())
13 changes: 13 additions & 0 deletions latest version/core/api_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ def has_power(self, power_id: str) -> bool:
'''判断role是否有power'''
return any(power_id == i.power_id for i in self.powers)

def only_has_powers(self, power_ids: list, anti_power_ids: list = None) -> bool:
'''判断role是否全有power_ids里的power,且没有anti_power_ids里的任何一个power'''
flags = [False] * len(power_ids)
if anti_power_ids is None:
anti_power_ids = []
for i in self.powers:
if i.power_id in anti_power_ids:
return False
for j, k in enumerate(power_ids):
if i.power_id == k:
flags[j] = True
return all(flags)

def select_from_id(self, role_id: int = None) -> 'Role':
'''用role_id查询role'''
if role_id is not None:
Expand Down
2 changes: 1 addition & 1 deletion latest version/core/constant.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .config_manager import Config

ARCAEA_SERVER_VERSION = 'v2.10.1'
ARCAEA_SERVER_VERSION = 'v2.10.1.1'


class Constant:
Expand Down
6 changes: 3 additions & 3 deletions latest version/database/init/arc_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ class InitData:
role_caption = ['系统', '管理员', '用户', '查询接口']

power = ['system', 'select', 'select_me', 'change', 'change_me', 'grant',
'grant_inf', 'select_song_rank', 'select_song_info']
'grant_inf', 'select_song_rank', 'select_song_info', 'select_song_rank_top']
power_caption = ['系统权限', '总体查询权限', '自我查询权限', '总体修改权限',
'自我修改权限', '授权权限', '下级授权权限', '歌曲排行榜查询权限', '歌曲信息查询权限']
'自我修改权限', '授权权限', '下级授权权限', '歌曲排行榜查询权限', '歌曲信息查询权限', '歌曲排行榜有限查询权限']

role_power = {'system': power,
'admin': ['select', 'select_me', 'change_me', 'grant_inf', 'select_song_rank', 'select_song_info'],
'admin': ['select', 'select_me', 'change_me', 'grant_inf', 'select_song_rank_top', 'select_song_info'],
'user': ['select_me', 'change_me', 'select_song_rank', 'select_song_info'],
'selector': ['select']
}
3 changes: 2 additions & 1 deletion latest version/run.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cd /d %~dp0
:: Develop server
python -B main.py
python -B main.py
pause

0 comments on commit 7324c9b

Please sign in to comment.