wordbot: Add special cases for 1, 2, 3, and 4+ word scores

This groups multiple words into the same message and also adds a little
bit of fun for multi-word scores.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2025-06-30 10:26:56 -07:00
parent f795fe37c8
commit c247dbae8c

View File

@@ -1,18 +1,17 @@
import asyncio import asyncio
import itertools import itertools
import logging import logging
from pathlib import Path
import random import random
import re import re
import sqlite3 import sqlite3
import time import time
from pathlib import Path
from typing import Set from typing import Set
from asyncirc.protocol import IrcProtocol from asyncirc.protocol import IrcProtocol
from irclib.parser import Prefix from irclib.parser import Prefix
from omnibot.plugin import Plugin from omnibot.plugin import Plugin
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -236,13 +235,36 @@ class Wordbot(Plugin):
# Don't try to score words for inactive games # Don't try to score words for inactive games
return return
words_in_line = set(re.findall(r"[a-z0-9-]+", line.lower())) words_in_line = set(re.findall(r"[a-z0-9-]+", line.lower()))
matches = words_in_line & self.db.unmatched_words(channel) matches = list(words_in_line & self.db.unmatched_words(channel))
if not matches:
return
# "'foo', 'bar', and 'baz'"
words_list = (
", ".join([f"'{word}'" for word in matches[:-1]])
+ f" and '{matches[-1]}' are"
)
match len(matches):
case 1:
bark = "Congrats!"
words_list = f"'{matches[-1]}' is"
case 2:
bark = "Doubles!"
case 3:
bark = "Hat trick!"
case _:
bark = "Cheater!"
points = f"{len(matches)} point"
if len(matches) > 1:
points += "s"
self.send_to(
conn,
channel,
f"{who.nick}: {bark} {words_list} good for {points}.",
)
for word in matches: for word in matches:
self.send_to(
conn,
channel,
f"{who.nick}: Congrats! '{word}' is good for 1 point.",
)
self.db.add_score(channel, who.nick, word, line) self.db.add_score(channel, who.nick, word, line)
async def handle_command( async def handle_command(