From 336911e2bf336a709a5edb1c7d0863747b427c3b Mon Sep 17 00:00:00 2001 From: Diving-Fish Date: Wed, 24 Jan 2024 00:37:46 +0800 Subject: [PATCH] security update --- database/models/maimai.py | 36 +++++++------------------ database/routes/maimai.py | 56 +++++++++++---------------------------- database/routes/public.py | 5 ++++ 3 files changed, 31 insertions(+), 66 deletions(-) diff --git a/database/models/maimai.py b/database/models/maimai.py index 729c455..0874f67 100644 --- a/database/models/maimai.py +++ b/database/models/maimai.py @@ -140,15 +140,14 @@ def verify_plate(player, version, plate_type) -> Tuple[bool, str]: return False, "" -def get_masked_achievement(record: NewRecord): - sc = ScoreCoefficient(record.achievements) +def get_masked_achievement(record: NewRecord, sc: ScoreCoefficient, ra: int): if record.achievements >= 100.5: if record.fc == "ap" or record.fc == "app": return 101 return math.floor(record.achievements * 10) / 10 if sc.c == 0: return 0 - acc = sc.ra(record.ds) * 100 / sc.c / record.ds + acc = ra * 100 / sc.c / record.ds if acc < sc.min: acc = sc.min else: @@ -157,6 +156,8 @@ def get_masked_achievement(record: NewRecord): def record_json(record: NewRecord, masked: bool): + sc = ScoreCoefficient(record.achievements) + ra = sc.ra(record.ds) data = { "title": record.title, "level": record.diff, @@ -164,44 +165,27 @@ def record_json(record: NewRecord, masked: bool): "level_label": ["Basic", "Advanced", "Expert", "Master", "Re:MASTER"][record.level], "type": record.type, "dxScore": 0 if masked else record.dxScore, - "achievements": get_masked_achievement(record) if masked else record.achievements, - "rate": ScoreCoefficient(record.achievements).r, + "achievements": get_masked_achievement(record, sc, ra) if masked else record.achievements, + "rate": sc.r, "fc": record.fc, "fs": record.fs, - "ra": ScoreCoefficient(record.achievements).ra(record.ds), + "ra": ra, "ds": record.ds, "song_id": record.id } return data -def record_json_output(record: NewRecord, masked: bool): - t1 = time.time() - chart = record.chart - print(time.time() - t1) - music = chart.music - print(time.time() - t1) - return { - "title": music.title, - "level": chart.level, - "level_index": chart.level, - "type": music.type, - "dxScore": 0 if masked else record.dxScore, - "achievements": get_masked_achievement(record) if masked else record.achievements, - "rate": ScoreCoefficient(record.achievements).r, - "fc": record.fc, - "fs": record.fs, - } - - def platerecord_json(platerecord: NewRecord, masked: bool): + sc = ScoreCoefficient(platerecord.achievements) + ra = sc.ra(platerecord.ds) data = { "id": platerecord.id, "title": platerecord.title, "level": platerecord.diff, "level_index": platerecord.level, "type": platerecord.type, - "achievements": get_masked_achievement(platerecord) if masked else platerecord.achievements, + "achievements": get_masked_achievement(platerecord, sc, ra) if masked else platerecord.achievements, "fc": platerecord.fc, "fs": platerecord.fs } diff --git a/database/routes/maimai.py b/database/routes/maimai.py index 4c62ba5..7794ba8 100644 --- a/database/routes/maimai.py +++ b/database/routes/maimai.py @@ -60,7 +60,7 @@ async def agreement(): obj = await request.json if "accept_agreement" in obj: g.user.accept_agreement = obj["accept_agreement"] - g.save() + g.user.save() return {"message": "success"} @@ -365,45 +365,21 @@ async def update_records(): cs_need_update = True j = await request.get_json() dicts = {} - if "userId" in j: - pass - try: - for ml in j["userMusicList"]: - for m in ml["userMusicDetailList"]: - if str(m["musicId"]) not in md_map: - continue - music = md_map[str(m["musicId"])] - level = m["level"] - achievement = min(1010000, m["achievement"]) - fc = ["", "fc", "fcp", "ap", "app"][m["comboStatus"]] - fs = ["", "fs", "fsp", "fsd", "fsdp"][m["syncStatus"]] - dxScore = m["deluxscoreMax"] - cid = music["cids"][level] - dicts[cid] = (achievement / 10000.0, fc, fs, dxScore) - g.user.user_id = j["userId"] - g.user.user_data = json.dumps(j["userData"]) if "userData" in j else "" - g.user.save() - except Exception as e: - raise e - return { - "message": str(e) - }, 400 - else: - if type(j) != type([]): - return {"message": "导入数据格式有误"}, 404 - elif len(j) == 0: - return {"message": "更新成功"} - for record in j: - # print(time.time()) - title = record['title'] - _type = record['type'] - level = record['level_index'] - m = get_music_by_title(md_cache, title, _type) - if m is None or level >= len(m["cids"]): - continue - cid = m["cids"][level] - dicts[cid] = (record["achievements"], record["fc"], - record["fs"], record["dxScore"]) + if type(j) != type([]): + return {"message": "导入数据格式有误"}, 404 + elif len(j) == 0: + return {"message": "更新成功"} + for record in j: + # print(time.time()) + title = record['title'] + _type = record['type'] + level = record['level_index'] + m = get_music_by_title(md_cache, title, _type) + if m is None or level >= len(m["cids"]): + continue + cid = m["cids"][level] + dicts[cid] = (record["achievements"], record["fc"], + record["fs"], record["dxScore"]) rs = NewRecord.raw( 'select * from newrecord where player_id = %s', g.user.id) updates = [] diff --git a/database/routes/public.py b/database/routes/public.py index b403f28..e6e6821 100644 --- a/database/routes/public.py +++ b/database/routes/public.py @@ -29,6 +29,11 @@ async def message_resp(): return resp +@app.route("/alive_check", methods=['GET']) +async def alive_check(): + return {"message": "ok"} + + @app.route("/message", methods=['GET']) async def message_g(): return await message_resp()