Made towels part of normal point system

This commit is contained in:
Jacob Henry 2018-12-03 14:48:03 -05:00
parent 941ae19dad
commit c6f2dd9844
3 changed files with 72 additions and 68 deletions

View File

@ -20,6 +20,7 @@ JOB_VAL = 1
LATE_VAL = 0.5 LATE_VAL = 0.5
MISS_VAL = -1 MISS_VAL = -1
SIGNOFF_VAL = 0.1 SIGNOFF_VAL = 0.1
TOWEL_VAL = 0.1
# What to put for a non-signed-off job # What to put for a non-signed-off job
SIGNOFF_PLACEHOLDER = "E-SIGNOFF" SIGNOFF_PLACEHOLDER = "E-SIGNOFF"
@ -86,6 +87,14 @@ class PointStatus(object):
fmt(self.bonus_points), 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]: def strip_all(l: List[str]) -> List[str]:
return [x.strip() for x in l] return [x.strip() for x in l]

View File

@ -35,7 +35,7 @@ def main() -> None:
# Add towel rolling # Add towel rolling
wrap.add_hook(slavestothemachine.count_work_hook) wrap.add_hook(slavestothemachine.count_work_hook)
wrap.add_hook(slavestothemachine.dump_work_hook) # wrap.add_hook(slavestothemachine.dump_work_hook)
# Add job management # Add job management
wrap.add_hook(job_commands.signoff_hook) wrap.add_hook(job_commands.signoff_hook)

View File

@ -1,20 +1,18 @@
import re
import textwrap
from typing import Match from typing import Match
from slackclient import SlackClient from slackclient import SlackClient
import channel_util import channel_util
import slack_util import house_management
import identifier import identifier
import re import slack_util
import shelve from scroll_util import Brother
from scroll_util import BrotherNotFound
counted_data = ["flaked", "rolled", "replaced", "washed", "dried"] counted_data = ["flaked", "rolled", "replaced", "washed", "dried"]
lookup_format = "{}\s+(\d+)" lookup_format = "{}\s+(\d+)"
DB_NAME = "towels_rolled"
def fmt_work_dict(work_dict: dict) -> str: def fmt_work_dict(work_dict: dict) -> str:
return ",\n".join(["{} × {}".format(job, count) for job, count in sorted(work_dict.items())]) return ",\n".join(["{} × {}".format(job, count) for job, count in sorted(work_dict.items())])
@ -22,12 +20,15 @@ def fmt_work_dict(work_dict: dict) -> str:
# noinspection PyUnusedLocal # noinspection PyUnusedLocal
async def count_work_callback(slack: SlackClient, msg: dict, match: Match) -> None: async def count_work_callback(slack: SlackClient, msg: dict, match: Match) -> None:
with shelve.open(DB_NAME) as db: # Make an error wrapper
verb = slack_util.VerboseWrapper(slack, msg)
# Tidy the text
text = msg["text"].lower().strip() text = msg["text"].lower().strip()
# Couple things to work through. # Couple things to work through.
# One: Who sent the message? # One: Who sent the message?
who_wrote = await identifier.lookup_msg_brother(msg) who_wrote = await verb(identifier.lookup_msg_brother(msg))
who_wrote_label = "{} [{}]".format(who_wrote.name, who_wrote.scroll) who_wrote_label = "{} [{}]".format(who_wrote.name, who_wrote.scroll)
# Two: What work did they do? # Two: What work did they do?
@ -42,59 +43,53 @@ async def count_work_callback(slack: SlackClient, msg: dict, match: Match) -> No
if len(new_work) == 0: if len(new_work) == 0:
if re.search(r'\s\d\s', text) is not None: if re.search(r'\s\d\s', text) is not None:
slack_util.reply(slack, msg, slack_util.reply(slack, msg,
"No work recognized. Use words {} or work will not be recorded".format(counted_data)) "If you were trying to record work, it was not recognized.\n"
"Use words {} or work will not be recorded".format(counted_data))
return return
# Three: Add it to their total work. We key by user_id, to avoid annoying identity shit # Four: Knowing they did something, record to total work
db_key = msg["user"] contribution_count = sum(new_work.values())
old_work = db.get(db_key) or {} new_total = await verb(record_towel_contribution(who_wrote, contribution_count))
total_work = dict(old_work)
for job, count in new_work.items(): # Five, congratulate them on their work!
if job not in total_work: congrats = textwrap.dedent("""{} recorded work:
total_work[job] = 0 {}
total_work[job] += count Net increase in points: {}
Total points since last reset: {}""".format(who_wrote_label,
# 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(new_work),
fmt_work_dict(total_work)) contribution_count,
new_total))
slack_util.reply(slack, msg, congrats) slack_util.reply(slack, msg, congrats)
# noinspection PyUnusedLocal async def record_towel_contribution(for_brother: Brother, contribution_count: int) -> int:
async def dump_work_callback(slack: SlackClient, msg: dict, match: Match) -> None: """
with shelve.open(DB_NAME) as db: Grants <count> contribution point to the specified user.
# Dump out each user Returns the new total.
keys = db.keys() """
result = ["All work:"] # Import house points
for user_id in keys: headers, points = await house_management.import_points()
# Get the work
work = db[user_id]
del db[user_id]
# Get the name # Find the brother
try: for p in points:
brother = await identifier.lookup_slackid_brother(user_id) if p is None or p.brother != for_brother:
except BrotherNotFound: continue
brother = user_id
else:
brother = brother.name
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") # Export
# Send it back house_management.export_points(headers, points)
slack_util.reply(slack, msg, "\n".join(result))
# 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 # Make dem HOOKs
count_work_hook = slack_util.Hook(count_work_callback, count_work_hook = slack_util.Hook(count_work_callback,
patterns=".*", patterns=".*",
channel_whitelist=[channel_util.SLAVES_TO_THE_MACHINE_ID]) channel_whitelist=[channel_util.SLAVES_TO_THE_MACHINE_ID],
dump_work_hook = slack_util.Hook(dump_work_callback, consumer=False)
patterns="dump towel data",
channel_whitelist=[channel_util.COMMAND_CENTER_ID])