修复拉踩BUG

This commit is contained in:
ZaneYork 2024-09-11 08:48:54 +08:00
parent 1c9ce1d41f
commit efd379cea8
2 changed files with 13 additions and 19 deletions

View File

@ -1,5 +1,6 @@
import json import json
from re import Pattern from re import Pattern
from itertools import chain
from session import app from session import app
from flask import render_template, make_response, abort from flask import render_template, make_response, abort
@ -32,16 +33,13 @@ def fetch_user_menu_summary() -> dict[str, float]:
all_menu = list(fetch_all_user_today_menu()) all_menu = list(fetch_all_user_today_menu())
if len(all_menu) > 0: if len(all_menu) > 0:
menus = list(map(lambda x: json.loads(x[1]), all_menu)) menus = list(map(lambda x: json.loads(x[1]), all_menu))
menu_keys = set() menu_keys = set(chain.from_iterable(menus))
for x in menus:
for k in x:
menu_keys.add(k)
result = {} result = {}
for k in menu_keys: for k in menu_keys:
result[k] = 0 result[k] = 0
for x in menus: for user_menu in menus:
for k in x: for k in user_menu:
result[k] += x[k] result[k] += user_menu[k]
return result return result
return {} return {}
@ -106,20 +104,18 @@ def dinner_update():
abort(403) abort(403)
if check_roll() != 0: if check_roll() != 0:
return make_response(json.dumps(dict(code=-1, data="来晚了,提交失败"))) return make_response(json.dumps(dict(code=-1, data="来晚了,提交失败")))
value = request.args.get('value').strip() user_menu = request.args.get('value').strip()
if not value: if not user_menu:
set_user_menu('') set_user_menu('')
return make_response(json.dumps(dict(code=0, data="OK"))) return make_response(json.dumps(dict(code=0, data="OK")))
value = json.loads(value) user_menu = json.loads(user_menu)
summary = 0 summary = sum(abs(int(value)) for value in user_menu.values())
for key in value:
summary += int(value[key])
if summary <= 0: if summary <= 0:
set_user_menu('') set_user_menu('')
return make_response(json.dumps(dict(code=0, data="OK"))) return make_response(json.dumps(dict(code=0, data="OK")))
for key in value: for key in user_menu:
value[key] = int(value[key]) / summary user_menu[key] = abs(int(user_menu[key])) / summary
set_user_menu(json.dumps(value, ensure_ascii=False)) set_user_menu(json.dumps(user_menu, ensure_ascii=False))
return make_response(json.dumps(dict(code=0, data="OK"))) return make_response(json.dumps(dict(code=0, data="OK")))

View File

@ -1,11 +1,9 @@
import os import os
from flask import redirect, send_from_directory from flask import redirect, send_from_directory
import threading # noinspection PyUnresolvedReferences
from dinner import * from dinner import *
from session import app from session import app
lock = threading.Lock()
@app.route('/') @app.route('/')
def index(): def index():