From c6f2dd98446186f26412ac8035e99ae07d87b89f Mon Sep 17 00:00:00 2001 From: Jacob Henry Date: Mon, 3 Dec 2018 14:48:03 -0500 Subject: [PATCH] Made towels part of normal point system --- house_management.py | 9 +++ main.py | 2 +- slavestothemachine.py | 129 ++++++++++++++++++++---------------------- 3 files changed, 72 insertions(+), 68 deletions(-) diff --git a/house_management.py b/house_management.py index 8e338b4..96ee591 100644 --- a/house_management.py +++ b/house_management.py @@ -20,6 +20,7 @@ JOB_VAL = 1 LATE_VAL = 0.5 MISS_VAL = -1 SIGNOFF_VAL = 0.1 +TOWEL_VAL = 0.1 # What to put for a non-signed-off job SIGNOFF_PLACEHOLDER = "E-SIGNOFF" @@ -86,6 +87,14 @@ class PointStatus(object): fmt(self.bonus_points), ) + @property + def towel_contribution_count(self) -> int: + return round(self.towel_points / TOWEL_VAL) + + @towel_contribution_count.setter + def towel_contribution_count(self, val: int) -> None: + self.towel_points = val * TOWEL_VAL + def strip_all(l: List[str]) -> List[str]: return [x.strip() for x in l] diff --git a/main.py b/main.py index 40aef39..b2ed9c2 100644 --- a/main.py +++ b/main.py @@ -35,7 +35,7 @@ def main() -> None: # Add towel rolling wrap.add_hook(slavestothemachine.count_work_hook) - wrap.add_hook(slavestothemachine.dump_work_hook) + # wrap.add_hook(slavestothemachine.dump_work_hook) # Add job management wrap.add_hook(job_commands.signoff_hook) diff --git a/slavestothemachine.py b/slavestothemachine.py index 11af477..691ec4e 100644 --- a/slavestothemachine.py +++ b/slavestothemachine.py @@ -1,20 +1,18 @@ +import re +import textwrap from typing import Match from slackclient import SlackClient import channel_util -import slack_util +import house_management import identifier -import re -import shelve - -from scroll_util import BrotherNotFound +import slack_util +from scroll_util import Brother counted_data = ["flaked", "rolled", "replaced", "washed", "dried"] lookup_format = "{}\s+(\d+)" -DB_NAME = "towels_rolled" - def fmt_work_dict(work_dict: dict) -> str: return ",\n".join(["{} × {}".format(job, count) for job, count in sorted(work_dict.items())]) @@ -22,79 +20,76 @@ def fmt_work_dict(work_dict: dict) -> str: # noinspection PyUnusedLocal async def count_work_callback(slack: SlackClient, msg: dict, match: Match) -> None: - with shelve.open(DB_NAME) as db: - text = msg["text"].lower().strip() + # Make an error wrapper + verb = slack_util.VerboseWrapper(slack, msg) - # Couple things to work through. - # One: Who sent the message? - who_wrote = await identifier.lookup_msg_brother(msg) - who_wrote_label = "{} [{}]".format(who_wrote.name, who_wrote.scroll) + # Tidy the text + text = msg["text"].lower().strip() - # Two: What work did they do? - new_work = {} - for job in counted_data: - pattern = lookup_format.format(job) - match = re.search(pattern, text) - if match: - new_work[job] = int(match.group(1)) + # Couple things to work through. + # One: Who sent the message? + who_wrote = await verb(identifier.lookup_msg_brother(msg)) + who_wrote_label = "{} [{}]".format(who_wrote.name, who_wrote.scroll) - # Three: check if we found anything - if len(new_work) == 0: - if re.search(r'\s\d\s', text) is not None: - slack_util.reply(slack, msg, - "No work recognized. Use words {} or work will not be recorded".format(counted_data)) - return + # Two: What work did they do? + new_work = {} + for job in counted_data: + pattern = lookup_format.format(job) + match = re.search(pattern, text) + if match: + new_work[job] = int(match.group(1)) - # Three: Add it to their total work. We key by user_id, to avoid annoying identity shit - db_key = msg["user"] - old_work = db.get(db_key) or {} - total_work = dict(old_work) + # Three: check if we found anything + if len(new_work) == 0: + if re.search(r'\s\d\s', text) is not None: + slack_util.reply(slack, msg, + "If you were trying to record work, it was not recognized.\n" + "Use words {} or work will not be recorded".format(counted_data)) + return - for job, count in new_work.items(): - if job not in total_work: - total_work[job] = 0 - total_work[job] += count + # Four: Knowing they did something, record to total work + contribution_count = sum(new_work.values()) + new_total = await verb(record_towel_contribution(who_wrote, contribution_count)) - # Save - db[db_key] = total_work - - # Four, congratulate them on their work - congrats = "{} recorded work:\n{}\nTotal work since last dump now\n{}".format(who_wrote_label, - fmt_work_dict(new_work), - fmt_work_dict(total_work)) - slack_util.reply(slack, msg, congrats) + # Five, congratulate them on their work! + congrats = textwrap.dedent("""{} recorded work: + {} + Net increase in points: {} + Total points since last reset: {}""".format(who_wrote_label, + fmt_work_dict(new_work), + contribution_count, + new_total)) + slack_util.reply(slack, msg, congrats) -# noinspection PyUnusedLocal -async def dump_work_callback(slack: SlackClient, msg: dict, match: Match) -> None: - with shelve.open(DB_NAME) as db: - # Dump out each user - keys = db.keys() - result = ["All work:"] - for user_id in keys: - # Get the work - work = db[user_id] - del db[user_id] +async def record_towel_contribution(for_brother: Brother, contribution_count: int) -> int: + """ + Grants contribution point to the specified user. + Returns the new total. + """ + # Import house points + headers, points = await house_management.import_points() - # Get the name - try: - brother = await identifier.lookup_slackid_brother(user_id) - except BrotherNotFound: - brother = user_id - else: - brother = brother.name + # Find the brother + for p in points: + if p is None or p.brother != for_brother: + continue - result.append("{} has done:\n{}".format(brother, fmt_work_dict(work))) + # If found, mog with more points + p.towel_contribution_count += contribution_count - result.append("Database wiped. Next dump will show new work since the time of this message") - # Send it back - slack_util.reply(slack, msg, "\n".join(result)) + # Export + house_management.export_points(headers, points) + + # Return the new total + return p.towel_contribution_count + + # If not found, get mad! + raise KeyError("No score entry found for brother {}".format(for_brother)) # Make dem HOOKs count_work_hook = slack_util.Hook(count_work_callback, patterns=".*", - channel_whitelist=[channel_util.SLAVES_TO_THE_MACHINE_ID]) -dump_work_hook = slack_util.Hook(dump_work_callback, - patterns="dump towel data", - channel_whitelist=[channel_util.COMMAND_CENTER_ID]) + channel_whitelist=[channel_util.SLAVES_TO_THE_MACHINE_ID], + consumer=False)