添加注释

This commit is contained in:
ZaneYork 2024-09-11 09:37:12 +08:00
parent 254a2f82c6
commit 6bb199d1db
2 changed files with 11 additions and 11 deletions

View File

@ -33,7 +33,7 @@ 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(chain.from_iterable(menus)) menu_keys = set(chain(*menus))
result = {} result = {}
for k in menu_keys: for k in menu_keys:
result[k] = 0 result[k] = 0
@ -79,6 +79,7 @@ def vote_reduce(summary: dict[str, float]) -> tuple[dict[str, float], float, str
name, _, expression = menu name, _, expression = menu
if expression is None or name not in summary.keys(): if expression is None or name not in summary.keys():
continue continue
# 计算预定路由规则
new_name = eval(expression, {'total_vote': total_vote}) new_name = eval(expression, {'total_vote': total_vote})
if new_name != name: if new_name != name:
if summary.get(new_name) is None: if summary.get(new_name) is None:
@ -86,9 +87,9 @@ def vote_reduce(summary: dict[str, float]) -> tuple[dict[str, float], float, str
else: else:
summary[new_name] += summary[name] summary[new_name] += summary[name]
summary[name] = 0 summary[name] = 0
for k in summary: # 昨日中签项降低权重
if last_result == k: if last_result in summary:
summary[k] = summary[k] * 7 / 10 summary[last_result] = summary[last_result] * 7 / 10
total_vote = sum(value for value in summary.values()) total_vote = sum(value for value in summary.values())
return summary, total_vote, last_result return summary, total_vote, last_result
@ -109,10 +110,12 @@ def dinner_update():
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")))
user_menu = json.loads(user_menu) user_menu = json.loads(user_menu)
# 计算总投票数值
summary = sum(abs(int(value)) for value in user_menu.values()) summary = sum(abs(int(value)) for value in user_menu.values())
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 user_menu: for key in user_menu:
user_menu[key] = abs(int(user_menu[key])) / summary user_menu[key] = abs(int(user_menu[key])) / summary
set_user_menu(json.dumps(user_menu, ensure_ascii=False)) set_user_menu(json.dumps(user_menu, ensure_ascii=False))
@ -128,12 +131,9 @@ def dinner_roll():
if check_roll() != 1: if check_roll() != 1:
return make_response(json.dumps(dict(code=-1, data="目前不能抽签"))) return make_response(json.dumps(dict(code=-1, data="目前不能抽签")))
summary, _, _ = vote_reduce(fetch_user_menu_summary()) summary, _, _ = vote_reduce(fetch_user_menu_summary())
for k in summary: # 票数乘以100四舍五入取整投入抽奖池
summary[k] = int(round(summary[k] * 100)) pool = list(chain(*[[name] * int(round(summary[name] * 100)) for name in summary]))
pool = [] # Knuth-Durstenfeld Shuffle算法洗牌: 从后往前依次随机将未乱序元素交换到当前位置,直到所有元素均被打乱
for k in summary:
for i in range(int(summary[k])):
pool.append(k)
random.shuffle(pool) random.shuffle(pool)
result = pool[0] result = pool[0]
set_roll_result(result) set_roll_result(result)

View File

@ -35,7 +35,7 @@
<div style="padding-top: 40px"> <div style="padding-top: 40px">
<label>大家的选择 - 总票数:{{ '{:.2f}'.format(total_vote | round(2)) }}</label> <label>大家的选择 - 总票数:{{ '{:.2f}'.format(total_vote | round(2)) }}</label>
<ul style="padding-top: 20px; margin-bottom: 80px" class="list-group"> <ul style="padding-top: 20px; margin-bottom: 80px" class="list-group">
{% for key in summary %} {% for key in summary|dictsort(true) %}
<li class="list-group-item {{ "active" if result == key else "" }}"> <li class="list-group-item {{ "active" if result == key else "" }}">
<div class="row justify-content-between"> <div class="row justify-content-between">
<span class="col-5">{{ key }}</span> <span class="col-5">{{ key }}</span>