mirror of
https://github.com/Lost-MSth/Arcaea-server.git
synced 2026-02-11 02:17:28 +08:00
Change something about aggregate
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
from flask import Flask, json, request, jsonify, make_response, send_from_directory
|
from flask import Flask, json, request, jsonify, send_from_directory
|
||||||
from logging.config import dictConfig
|
from logging.config import dictConfig
|
||||||
from setting import Config
|
from setting import Config
|
||||||
import base64
|
import base64
|
||||||
@@ -21,9 +21,10 @@ from udpserver.udp_main import link_play
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from multiprocessing import Process, Pipe
|
from multiprocessing import Process, Pipe
|
||||||
if Config.USE_EXPERIMENTAL_AGGREGATE:
|
|
||||||
from urllib.parse import parse_qs, urlparse
|
|
||||||
from werkzeug.datastructures import ImmutableMultiDict
|
from urllib.parse import parse_qs, urlparse
|
||||||
|
from werkzeug.datastructures import ImmutableMultiDict
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -39,6 +40,14 @@ app.register_blueprint(api.api_main.bp)
|
|||||||
|
|
||||||
conn1, conn2 = Pipe()
|
conn1, conn2 = Pipe()
|
||||||
|
|
||||||
|
map_dict = {'/user/me': 'user_me',
|
||||||
|
'/purchase/bundle/pack': 'bundle_pack',
|
||||||
|
'/serve/download/me/song': 'download_song',
|
||||||
|
'/game/info': 'game_info',
|
||||||
|
'/present/me': 'present_info',
|
||||||
|
'/world/map/me': 'world_all',
|
||||||
|
'/score/song/friend': 'song_score_friend'}
|
||||||
|
|
||||||
|
|
||||||
def add_url_prefix(url, strange_flag=False):
|
def add_url_prefix(url, strange_flag=False):
|
||||||
# 给url加前缀,返回字符串
|
# 给url加前缀,返回字符串
|
||||||
@@ -124,16 +133,17 @@ def error_return(error_code, extra={}): # 错误返回
|
|||||||
"error_code": error_code
|
"error_code": error_code
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def success_return(value):
|
def success_return(value):
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"success": True,
|
"success": True,
|
||||||
"value": value
|
"value": value
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def hello():
|
def hello():
|
||||||
return "Hello World!"
|
return "Hello World!"
|
||||||
# 自定义路径
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/favicon.ico', methods=['GET']) # 图标
|
@app.route('/favicon.ico', methods=['GET']) # 图标
|
||||||
@@ -198,66 +208,84 @@ def register():
|
|||||||
else:
|
else:
|
||||||
return error_return(error_code)
|
return error_return(error_code)
|
||||||
|
|
||||||
@app.route(add_url_prefix('/purchase/bundle/pack'), methods=['GET'])
|
|
||||||
|
@app.route(add_url_prefix('/purchase/bundle/pack'), methods=['GET']) # 曲包信息
|
||||||
@server.auth.auth_required(request)
|
@server.auth.auth_required(request)
|
||||||
def bundle_pack(user_id):
|
def bundle_pack(user_id):
|
||||||
return success_return(server.info.get_purchase_pack(user_id))
|
return success_return(server.info.get_purchase_pack(user_id))
|
||||||
|
|
||||||
@app.route(add_url_prefix('/game/info'), methods=['GET'])
|
|
||||||
|
@app.route(add_url_prefix('/game/info'), methods=['GET']) # 系统信息
|
||||||
def game_info():
|
def game_info():
|
||||||
return success_return(server.info.get_game_info())
|
return success_return(server.info.get_game_info())
|
||||||
|
|
||||||
@app.route(add_url_prefix('/present/me'), methods=['GET'])
|
|
||||||
|
@app.route(add_url_prefix('/present/me'), methods=['GET']) # 用户奖励信息
|
||||||
@server.auth.auth_required(request)
|
@server.auth.auth_required(request)
|
||||||
def present_info(user_id):
|
def present_info(user_id):
|
||||||
return success_return(server.info.get_user_present(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'])
|
@app.route(add_url_prefix('/compose/aggregate'), methods=['GET']) # 集成式请求
|
||||||
def aggregate_experimental():
|
def aggregate():
|
||||||
global request
|
try:
|
||||||
# ()都不requests.request('http://localhost')
|
#global request
|
||||||
finally_response={'success':True,'value':[]}
|
finally_response = {'success': True, 'value': []}
|
||||||
request_=request
|
#request_ = request
|
||||||
for i in json.loads(request_.args.get('calls')):
|
get_list = json.loads(request.args.get('calls'))
|
||||||
url=add_url_prefix(i.get('endpoint'))
|
if len(get_list) > 10:
|
||||||
request.args=ImmutableMultiDict({key:value[0] for key,value in parse_qs(urlparse(url).query).items()})
|
# 请求太多驳回
|
||||||
url=urlparse(url).path
|
return error_return(108)
|
||||||
urls = app.url_map.bind("example.com", "/")
|
|
||||||
resp_t=app.view_functions[urls.match(url,"GET")[0]]()
|
for i in get_list:
|
||||||
if hasattr(resp_t,"response"):
|
endpoint = i['endpoint']
|
||||||
resp_t=resp_t.response[0].decode().rstrip('\n')
|
url = add_url_prefix(endpoint)
|
||||||
resp=json.loads(resp_t)
|
request.args = ImmutableMultiDict(
|
||||||
if hasattr(resp,'get') and resp.get('success') is False:
|
{key: value[0] for key, value in parse_qs(urlparse(url).query).items()})
|
||||||
finally_response={'success':False,'error_code':7,'extra':{"id":i.get('id'),'error_code':resp.get('error_code')}}
|
|
||||||
|
resp_t = app.view_functions[map_dict[urlparse(endpoint).path]]()
|
||||||
|
|
||||||
|
if hasattr(resp_t, "response"):
|
||||||
|
resp_t = resp_t.response[0].decode().rstrip('\n')
|
||||||
|
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['id'], 'error_code': resp.get('error_code')}}
|
||||||
if "extra" in resp:
|
if "extra" in resp:
|
||||||
finally_response['extra']['extra']=resp['extra']
|
finally_response['extra']['extra'] = resp['extra']
|
||||||
request=request_# 无实际影响
|
#request = request_
|
||||||
return jsonify(finally_response)
|
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})
|
finally_response['value'].append(
|
||||||
request=request_# 无实际影响
|
{'id': i.get('id'), 'value': resp['value'] if hasattr(resp, 'get') else resp})
|
||||||
|
|
||||||
|
#request = request_
|
||||||
return jsonify(finally_response)
|
return jsonify(finally_response)
|
||||||
else:
|
except KeyError:
|
||||||
# 集成式请求,没想到什么好办法处理,就先这样写着
|
return error_return(108)
|
||||||
@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查分器用的
|
# # 集成式请求,没想到什么好办法处理,就先这样写着
|
||||||
|
# @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']) # 用户信息
|
||||||
@server.auth.auth_required(request)
|
@server.auth.auth_required(request)
|
||||||
def user_me(user_id):
|
def user_me(user_id):
|
||||||
r = server.info.arc_aggregate_small(user_id) # TODO:独立函数
|
r = server.info.get_user_me_c(user_id)
|
||||||
if r['success']:
|
if r:
|
||||||
r['value'] = r['value'][0]['value']
|
return success_return(r)
|
||||||
return jsonify(r)
|
else:
|
||||||
|
return error_return(108)
|
||||||
|
|
||||||
|
|
||||||
@app.route(add_url_prefix('/user/me/character'), methods=['POST']) # 角色切换
|
@app.route(add_url_prefix('/user/me/character'), methods=['POST']) # 角色切换
|
||||||
@@ -662,7 +690,7 @@ def world_one(user_id, map_id):
|
|||||||
@server.auth.auth_required(request)
|
@server.auth.auth_required(request)
|
||||||
def download_song(user_id):
|
def download_song(user_id):
|
||||||
song_ids = request.args.getlist('sid')
|
song_ids = request.args.getlist('sid')
|
||||||
url_flag=json.loads(request.args.get('url','true'))
|
url_flag = json.loads(request.args.get('url', 'true'))
|
||||||
if server.arcdownload.is_able_download(user_id) or not url_flag:
|
if server.arcdownload.is_able_download(user_id) or not url_flag:
|
||||||
re = {}
|
re = {}
|
||||||
if not song_ids:
|
if not song_ids:
|
||||||
@@ -772,9 +800,11 @@ def sys_set(user_id, path):
|
|||||||
set_arg = path[5:]
|
set_arg = path[5:]
|
||||||
value = request.form['value']
|
value = request.form['value']
|
||||||
server.setme.arc_sys_set(user_id, value, set_arg)
|
server.setme.arc_sys_set(user_id, value, set_arg)
|
||||||
r = server.info.arc_aggregate_small(user_id)
|
r = server.info.get_user_me_c(user_id)
|
||||||
r['value'] = r['value'][0]['value']
|
if r:
|
||||||
return jsonify(r)
|
return success_return(r)
|
||||||
|
else:
|
||||||
|
return error_return(108)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
from server.sql import Connect
|
from server.sql import Connect
|
||||||
|
from .config import Constant
|
||||||
from setting import Config
|
from setting import Config
|
||||||
import server.item
|
import server.item
|
||||||
import server.character
|
import server.character
|
||||||
@@ -26,14 +27,14 @@ def int2b(x):
|
|||||||
def calc_stamina(max_stamina_ts, curr_stamina):
|
def calc_stamina(max_stamina_ts, curr_stamina):
|
||||||
# 计算体力,返回剩余体力数值
|
# 计算体力,返回剩余体力数值
|
||||||
|
|
||||||
stamina = int(Config.MAX_STAMINA - (max_stamina_ts -
|
stamina = int(Constant.MAX_STAMINA - (max_stamina_ts -
|
||||||
int(time.time()*1000)) / Config.STAMINA_RECOVER_TICK)
|
int(time.time()*1000)) / Constant.STAMINA_RECOVER_TICK)
|
||||||
|
|
||||||
if stamina >= Config.MAX_STAMINA:
|
if stamina >= Constant.MAX_STAMINA:
|
||||||
if curr_stamina >= Config.MAX_STAMINA:
|
if curr_stamina >= Constant.MAX_STAMINA:
|
||||||
stamina = curr_stamina
|
stamina = curr_stamina
|
||||||
else:
|
else:
|
||||||
stamina = Config.MAX_STAMINA
|
stamina = Constant.MAX_STAMINA
|
||||||
if stamina < 0:
|
if stamina < 0:
|
||||||
stamina = 0
|
stamina = 0
|
||||||
|
|
||||||
@@ -245,8 +246,8 @@ def play_world_song(user_id, args):
|
|||||||
return {}
|
return {}
|
||||||
stamina = calc_stamina(max_stamina_ts, stamina) - \
|
stamina = calc_stamina(max_stamina_ts, stamina) - \
|
||||||
info['stamina_cost'] * stamina_multiply
|
info['stamina_cost'] * stamina_multiply
|
||||||
max_stamina_ts = now + Config.STAMINA_RECOVER_TICK * \
|
max_stamina_ts = now + Constant.STAMINA_RECOVER_TICK * \
|
||||||
(Config.MAX_STAMINA - stamina)
|
(Constant.MAX_STAMINA - stamina)
|
||||||
c.execute('''update user set max_stamina_ts=?, stamina=? where user_id=?''',
|
c.execute('''update user set max_stamina_ts=?, stamina=? where user_id=?''',
|
||||||
(max_stamina_ts, stamina, user_id))
|
(max_stamina_ts, stamina, user_id))
|
||||||
r = {
|
r = {
|
||||||
@@ -600,11 +601,11 @@ def add_stamina(c, user_id, add_stamina):
|
|||||||
if x and x[0] is not None and x[1] is not None:
|
if x and x[0] is not None and x[1] is not None:
|
||||||
stamina = calc_stamina(x[0], x[1]) + add_stamina
|
stamina = calc_stamina(x[0], x[1]) + add_stamina
|
||||||
max_stamina_ts = now - \
|
max_stamina_ts = now - \
|
||||||
(stamina-Config.MAX_STAMINA) * \
|
(stamina-Constant.MAX_STAMINA) * \
|
||||||
Config.STAMINA_RECOVER_TICK
|
Constant.STAMINA_RECOVER_TICK
|
||||||
else:
|
else:
|
||||||
max_stamina_ts = now
|
max_stamina_ts = now
|
||||||
stamina = Config.MAX_STAMINA
|
stamina = Constant.MAX_STAMINA
|
||||||
|
|
||||||
c.execute('''update user set max_stamina_ts=?, stamina=? where user_id=?''',
|
c.execute('''update user set max_stamina_ts=?, stamina=? where user_id=?''',
|
||||||
(max_stamina_ts, stamina, user_id))
|
(max_stamina_ts, stamina, user_id))
|
||||||
|
|||||||
7
latest version/server/config.py
Normal file
7
latest version/server/config.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
class Constant:
|
||||||
|
|
||||||
|
MAX_STAMINA = 12
|
||||||
|
|
||||||
|
STAMINA_RECOVER_TICK = 1800000
|
||||||
|
|
||||||
|
CORE_EXP = 250
|
||||||
@@ -6,7 +6,7 @@ import server.character
|
|||||||
import server.item
|
import server.item
|
||||||
import time
|
import time
|
||||||
from setting import Config
|
from setting import Config
|
||||||
|
from .config import Constant
|
||||||
|
|
||||||
|
|
||||||
def int2b(x):
|
def int2b(x):
|
||||||
@@ -199,6 +199,38 @@ def get_user_me(c, user_id):
|
|||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_me_c(user_id):
|
||||||
|
# user/me调用,上边没开数据库这里开一下
|
||||||
|
with Connect() as c:
|
||||||
|
return get_user_me(c, user_id)
|
||||||
|
|
||||||
|
|
||||||
|
def get_purchase_pack(user_id):
|
||||||
|
# 返回曲包数据
|
||||||
|
with Connect() as c:
|
||||||
|
return server.arcpurchase.get_purchase(c, 'pack')
|
||||||
|
|
||||||
|
|
||||||
|
def get_game_info():
|
||||||
|
# 返回游戏基本信息
|
||||||
|
r = {
|
||||||
|
"max_stamina": Constant.MAX_STAMINA,
|
||||||
|
"stamina_recover_tick": Constant.STAMINA_RECOVER_TICK,
|
||||||
|
"core_exp": Constant.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_small(user_id):
|
def arc_aggregate_small(user_id):
|
||||||
# 返回用户数据
|
# 返回用户数据
|
||||||
r = {"success": False}
|
r = {"success": False}
|
||||||
@@ -211,28 +243,6 @@ def arc_aggregate_small(user_id):
|
|||||||
|
|
||||||
return r
|
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):
|
def arc_aggregate_big(user_id):
|
||||||
# 返回比较全的用户数据
|
# 返回比较全的用户数据
|
||||||
@@ -258,9 +268,9 @@ def arc_aggregate_big(user_id):
|
|||||||
}, {
|
}, {
|
||||||
"id": 3,
|
"id": 3,
|
||||||
"value": {
|
"value": {
|
||||||
"max_stamina": MAX_STAMINA,
|
"max_stamina": Constant.MAX_STAMINA,
|
||||||
"stamina_recover_tick": STAMINA_RECOVER_TICK,
|
"stamina_recover_tick": Constant.STAMINA_RECOVER_TICK,
|
||||||
"core_exp": CORE_EXP,
|
"core_exp": Constant.CORE_EXP,
|
||||||
"curr_ts": int(time.time()*1000),
|
"curr_ts": int(time.time()*1000),
|
||||||
"level_steps": server.character.get_level_steps(),
|
"level_steps": server.character.get_level_steps(),
|
||||||
"world_ranking_enabled": True,
|
"world_ranking_enabled": True,
|
||||||
|
|||||||
@@ -251,44 +251,3 @@ class Config():
|
|||||||
'''
|
'''
|
||||||
--------------------
|
--------------------
|
||||||
'''
|
'''
|
||||||
|
|
||||||
'''
|
|
||||||
--------------------
|
|
||||||
体力上限
|
|
||||||
TODO
|
|
||||||
'''
|
|
||||||
MAX_STAMINA = 12
|
|
||||||
'''
|
|
||||||
--------------------
|
|
||||||
'''
|
|
||||||
|
|
||||||
'''
|
|
||||||
--------------------
|
|
||||||
恢复1格体力所需时间
|
|
||||||
TODO
|
|
||||||
'''
|
|
||||||
STAMINA_RECOVER_TICK = 1800000
|
|
||||||
'''
|
|
||||||
--------------------
|
|
||||||
'''
|
|
||||||
|
|
||||||
'''
|
|
||||||
--------------------
|
|
||||||
1个以太之滴增加的经验值
|
|
||||||
TODO
|
|
||||||
'''
|
|
||||||
CORE_EXP = 250
|
|
||||||
'''
|
|
||||||
--------------------
|
|
||||||
'''
|
|
||||||
|
|
||||||
'''
|
|
||||||
--------------------
|
|
||||||
使用实验性的 aggregate 接口
|
|
||||||
TODO
|
|
||||||
'''
|
|
||||||
USE_EXPERIMENTAL_AGGREGATE = True
|
|
||||||
'''
|
|
||||||
--------------------
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user