Add experimental aggregate API

This commit is contained in:
Young-Lord
2022-01-21 22:55:23 +08:00
parent cc4ac192e7
commit 355ec3557f
6 changed files with 135 additions and 27 deletions

View File

@@ -21,6 +21,7 @@ from udpserver.udp_main import link_play
import os
import sys
from multiprocessing import Process, Pipe
import requests
app = Flask(__name__)
@@ -121,6 +122,11 @@ def error_return(error_code, extra={}): # 错误返回
"error_code": error_code
})
def success_return(value):
return jsonify({
"success": True,
"value": value
})
@app.route('/')
def hello():
@@ -190,23 +196,63 @@ def register():
else:
return error_return(error_code)
# 集成式请求,没想到什么好办法处理,就先这样写着
@app.route(add_url_prefix('/compose/aggregate'), methods=['GET'])
@app.route(add_url_prefix('/purchase/bundle/pack'), methods=['GET'])
@server.auth.auth_required(request)
def aggregate(user_id):
calls = request.args.get('calls')
if calls == '[{ "endpoint": "/user/me", "id": 0 }]': # 极其沙雕的判断我猜get的参数就两种
r = server.info.arc_aggregate_small(user_id)
else:
r = server.info.arc_aggregate_big(user_id)
return jsonify(r)
def bundle_pack(user_id):
return success_return(server.info.get_purchase_pack(user_id))
@app.route(add_url_prefix('/game/info'), methods=['GET'])
def game_info():
return success_return(server.info.get_game_info())
@app.route(add_url_prefix('/present/me'), methods=['GET'])
@server.auth.auth_required(request)
def present_info(user_id):
return success_return(server.info.get_user_present(user_id))
if Config.USE_EXPERIMENTAL_AGGREGATE:
@app.route(add_url_prefix('/compose/aggregate'), methods=['GET'])
@server.auth.auth_required(request)
def myaggregate(user_id):
# 只有才会requests.request('http://localhost')
url_base:str='http'
if Config.SSL_CERT and Config.SSL_KEY:
url_base+='s'
url_base+='://'
url_base+='localhost:'+Config.PORT
finally_response={'success':True,'value':[]}
for i in json.loads(request.args.get('calls')):
resp_t=requests.request(request.method,\
url_base+add_url_prefix(i.get('endpoint')),\
headers=request.headers
).content.decode()
resp=json.loads(resp_t)
if hasattr(resp,'get') and resp.get('success') is False:
finally_response={'success':False,'error_code':7,'extra':{"id":i.get('id'),'error_code':resp.get('error_code')}}
if "extra" in resp:
finally_response['extra']['extra']=resp['extra']
return jsonify(finally_response)
# I don't sure if the behavior is correct
finally_response['value'].append({'id':i.get('id'),'value':resp.get('value') if hasattr(resp,'get') else resp})
print(finally_response)
return jsonify(finally_response)
else:
# 集成式请求,没想到什么好办法处理,就先这样写着
@app.route(add_url_prefix('/compose/aggregate'), methods=['GET'])
@server.auth.auth_required(request)
def aggregate(user_id):
calls = request.args.get('calls')
if calls == '[{ "endpoint": "/user/me", "id": 0 }]': # 极其沙雕的判断我猜get的参数就两种
r = server.info.arc_aggregate_small(user_id)
else:
r = server.info.arc_aggregate_big(user_id)
return jsonify(r)
@app.route(add_url_prefix('/user/me'), methods=['GET']) # 用户信息给baa查分器用的
@server.auth.auth_required(request)
def user_me(user_id):
r = server.info.arc_aggregate_small(user_id)
r = server.info.arc_aggregate_small(user_id) # TODO:独立函数
if r['success']:
r['value'] = r['value'][0]['value']
return jsonify(r)
@@ -614,10 +660,11 @@ def world_one(user_id, map_id):
@server.auth.auth_required(request)
def download_song(user_id):
song_ids = request.args.getlist('sid')
if server.arcdownload.is_able_download(user_id):
url_flag=json.loads(request.args.get('url','true'))
if server.arcdownload.is_able_download(user_id) or not url_flag:
re = {}
if not song_ids:
re = server.arcdownload.get_all_songs(user_id)
re = server.arcdownload.get_all_songs(user_id, url_flag=url_flag)
else:
re = server.arcdownload.get_some_songs(user_id, song_ids)

View File

@@ -1,2 +1,3 @@
flask
cryptography
requests

View File

@@ -26,14 +26,14 @@ def int2b(x):
def calc_stamina(max_stamina_ts, curr_stamina):
# 计算体力,返回剩余体力数值
stamina = int(server.info.MAX_STAMINA - (max_stamina_ts -
int(time.time()*1000)) / server.info.STAMINA_RECOVER_TICK)
stamina = int(Config.MAX_STAMINA - (max_stamina_ts -
int(time.time()*1000)) / Config.STAMINA_RECOVER_TICK)
if stamina >= server.info.MAX_STAMINA:
if curr_stamina >= server.info.MAX_STAMINA:
if stamina >= Config.MAX_STAMINA:
if curr_stamina >= Config.MAX_STAMINA:
stamina = curr_stamina
else:
stamina = server.info.MAX_STAMINA
stamina = Config.MAX_STAMINA
if stamina < 0:
stamina = 0
@@ -245,8 +245,8 @@ def play_world_song(user_id, args):
return {}
stamina = calc_stamina(max_stamina_ts, stamina) - \
info['stamina_cost'] * stamina_multiply
max_stamina_ts = now + server.info.STAMINA_RECOVER_TICK * \
(server.info.MAX_STAMINA - stamina)
max_stamina_ts = now + Config.STAMINA_RECOVER_TICK * \
(Config.MAX_STAMINA - stamina)
c.execute('''update user set max_stamina_ts=?, stamina=? where user_id=?''',
(max_stamina_ts, stamina, user_id))
r = {
@@ -600,11 +600,11 @@ def add_stamina(c, user_id, add_stamina):
if x and x[0] is not None and x[1] is not None:
stamina = calc_stamina(x[0], x[1]) + add_stamina
max_stamina_ts = now - \
(stamina-server.info.MAX_STAMINA) * \
server.info.STAMINA_RECOVER_TICK
(stamina-Config.MAX_STAMINA) * \
Config.STAMINA_RECOVER_TICK
else:
max_stamina_ts = now
stamina = server.info.MAX_STAMINA
stamina = Config.MAX_STAMINA
c.execute('''update user set max_stamina_ts=?, stamina=? where user_id=?''',
(max_stamina_ts, stamina, user_id))

View File

@@ -213,7 +213,7 @@ def char_use_core(user_id, character_id, amount):
x = c.fetchone()
if x:
exp, level = calc_level_up(
c, user_id, character_id, x[0], amount*server.info.CORE_EXP)
c, user_id, character_id, x[0], amount*Config.CORE_EXP)
c.execute('''update user_char set level=?, exp=? where user_id=? and character_id=?''',
(level, exp, user_id, character_id))
server.item.claim_user_item(

View File

@@ -7,9 +7,6 @@ import server.item
import time
from setting import Config
MAX_STAMINA = 12
STAMINA_RECOVER_TICK = 1800000
CORE_EXP = 250
def int2b(x):
@@ -214,6 +211,28 @@ def arc_aggregate_small(user_id):
return r
def get_purchase_pack(user_id):
# 返回曲包数据
with Connect() as c:
return server.arcpurchase.get_purchase(c, 'pack')
def get_game_info():
# 返回游戏基本信息
r={
"max_stamina": Config.MAX_STAMINA,
"stamina_recover_tick": Config.STAMINA_RECOVER_TICK,
"core_exp": Config.CORE_EXP,
"curr_ts": int(time.time()*1000),
"level_steps": server.character.get_level_steps(),
"world_ranking_enabled": True,
"is_byd_chapter_unlocked": True
}
return r
def get_user_present(user_id):
# 返回礼品信息
with Connect() as c:
return server.arcpurchase.get_user_present(c, user_id)
def arc_aggregate_big(user_id):
# 返回比较全的用户数据

View File

@@ -251,3 +251,44 @@ class Config():
'''
--------------------
'''
'''
--------------------
体力上限
TODO
'''
MAX_STAMINA = 12
'''
--------------------
'''
'''
--------------------
恢复1格体力所需时间
TODO
'''
STAMINA_RECOVER_TICK = 1800000
'''
--------------------
'''
'''
--------------------
1个以太之滴增加的经验值
TODO
'''
CORE_EXP = 250
'''
--------------------
'''
'''
--------------------
使用实验性的 aggregate 接口
TODO
'''
USE_EXPERIMENTAL_AGGREGATE = True
'''
--------------------
'''