路由规则投票人数排除掉不吃的人

This commit is contained in:
ZaneYork 2024-09-14 08:16:50 +08:00
parent 33589709c3
commit 5c3d0c1d3f
2 changed files with 10 additions and 6 deletions

View File

@ -51,10 +51,13 @@ def check_roll() -> int:
return 0 if within_time() else 1
def vote_reduce(summary: dict[str, float], limit: int = 2) -> tuple[dict[str, float], float, list[RollResult]]:
def vote_reduce(summary: dict[str, float],
dislikes: list[tuple[str, list[str]]], limit: int = 2
) -> tuple[dict[str, float], float, list[RollResult]]:
"""
按规则对投票结果进行修饰
:param summary: 投票汇总结果
:param dislikes: 不吃的项
:param limit: 降低最近N次点餐结果概率
:return: 投票汇总结果
"""
@ -65,7 +68,8 @@ def vote_reduce(summary: dict[str, float], limit: int = 2) -> tuple[dict[str, fl
if menu.expression is None or menu.name not in summary.keys():
continue
# 计算预定路由规则
new_name = eval(menu.expression, {'total_vote': total_vote})
dislike_cnt = len(list(filter(lambda x: menu.name in x[1], dislikes)))
new_name = eval(menu.expression, {'total_vote': total_vote - dislike_cnt})
if new_name != menu.name:
if new_name is not None:
if summary.get(new_name) is None:
@ -131,8 +135,8 @@ def dinner_roll():
def roll_logic(check=False):
menus, _, _ = fetch_user_menu_summary()
summary, _, _ = vote_reduce(menus)
menus, _, dislikes = fetch_user_menu_summary()
summary, _, _ = vote_reduce(menus, dislikes)
sorted_items = sorted(summary.items(), key=lambda item: item[1], reverse=True)
items = [item for item in sorted_items[:2]]
if len(items) > 1 and (items[0][1] * 0.9) <= items[1][1]:
@ -156,7 +160,7 @@ def dinner():
"""
user_menu = get_user_menu()
menus, users, dislikes = fetch_user_menu_summary()
summary, total_vote, last_results = vote_reduce(menus)
summary, total_vote, last_results = vote_reduce(menus, dislikes)
result = fetch_roll_result()
can_roll = (check_roll() == 1)
all_choice = list(fetch_all_menu())

View File

@ -17,7 +17,7 @@
<div class="mb-3">
<div class="input-group">
<label class="input-group-text" for="inputName" id="basic-addon1">姓名</label>
<input type="text" class="form-control"
<input type="text" class="form-control" {{ 'disabled="disabled"' if user_menu.nickname else '' | safe }}
id="inputName" name="nickname" value="{{ user_menu.nickname }}">
</div>
</div>