improve /best formatting, account for unknown responses

This commit is contained in:
Tomáš Mládek 2019-10-23 11:16:13 +02:00
parent f1ebd8d4c6
commit 7762ffd80a

View file

@ -4,7 +4,6 @@ import re
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, timedelta from datetime import datetime, timedelta
from enum import Enum from enum import Enum
from operator import itemgetter
from typing import List, Dict from typing import List, Dict
import telegram import telegram
@ -153,33 +152,42 @@ class DudleBot:
@staticmethod @staticmethod
def _best_of_plan(plan): def _best_of_plan(plan):
days = [plan.start + timedelta(days=i) for i in range(plan.duration.days)] days = [plan.start + timedelta(days=i) for i in range(plan.duration.days)]
results = [0] * plan.duration.days results = [(0, 0)] * plan.duration.days
for entry in plan.entries.values(): for entry in plan.entries.values():
for idx, response in enumerate(entry.responses): for idx, response in enumerate(entry.responses):
if response == PlanResponse.YES: if response == PlanResponse.YES:
results[idx] += 1 results[idx] = (results[idx][0] + 1, results[idx][1])
# elif response == PlanResponse.UNKNOWN: elif response == PlanResponse.UNKNOWN:
# results[idx] += 0.01 results[idx] = (results[idx][0], results[idx][1] + 1)
sorted_days = list(zip(days, results))
sorted_days.sort(key=lambda dwr: dwr[1][1], reverse=True) # First sort by unknowns
sorted_days.sort(key=lambda dwr: dwr[1][0], reverse=True) # Then sort by positive answers
sorted_days = sorted(zip(days, results), key=itemgetter(1), reverse=True)
best_days = [] best_days = []
last_result, different_results = None, 0 last_result, different_results = None, 0
for day, result in sorted_days: for idx, day_with_result in enumerate(sorted_days):
if result == 0 or (len(best_days) >= 3 and different_results > 1): day, result = day_with_result
yes, unknown = result
if yes == 0 or (idx > 3 and different_results > 1):
break break
best_days.append((day, result)) best_days.append(day_with_result)
if result != last_result: if result != last_result:
different_results += 1 different_results += 1
last_result = result last_result = result
response = f"Best {len(best_days)} result{'s' if len(best_days) > 1 else ''}:\n\n" if len(best_days) > 0:
for idx, dayresult in enumerate(best_days): response = f"Best {len(best_days)} result{'s' if len(best_days) > 1 else ''}:\n\n"
day, result = dayresult for idx, day_with_result in enumerate(best_days):
response += f"{idx + 1}. {day.strftime('%A')} ({result})\n" day, result = day_with_result
return response yes, unknown = result
result_fmt = str(yes) + (f" + {unknown}?" if unknown > 0 else "")
response += f"{idx + 1}. {day.strftime('%A %-d/%-m')} ({result_fmt})\n"
return response
else:
return "There has been no resolution."
def start(self): def start(self):
self.logger.info("Starting DudleBot...") self.logger.info("Starting DudleBot...")