Made towels part of normal point system
This commit is contained in:
parent
941ae19dad
commit
c6f2dd9844
|
|
@ -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]
|
||||
|
|
|
|||
2
main.py
2
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)
|
||||
|
|
|
|||
|
|
@ -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 <count> 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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue