yahtzee-server/models/server_chat.py

146 lines
6.4 KiB
Python

# -*- coding: utf-8 -*-
import random
from models.chat_engine_data import REACTIONS_DATA # Oder direkt eingebettet (siehe unten)
CAT_NAMES = {
"DE": {
"1": "Einser", "2": "Zweier", "3": "Dreier", "4": "Vierer", "5": "Fünfer", "6": "Sechser",
"3K": "Dreierpasch", "4K": "Viererpasch", "FH": "Full House",
"SS": "Kleine Straße", "LS": "Große Straße", "Y": "Yahtzee", "C": "Chance"
},
"EN": {
"1": "Ones", "2": "Twos", "3": "Threes", "4": "Fours", "5": "Fives", "6": "Sixes",
"3K": "Three of a Kind", "4K": "Four of a Kind", "FH": "Full House",
"SS": "Small Straight", "LS": "Large Straight", "Y": "Yahtzee", "C": "Chance"
}
}
class ServerChatEngine:
"""Erzeugt zweisprachige Wurf-Protokolle und Bot-Reaktionen für alle Clients im Raum."""
DICE_FACES = {1: "⚀", 2: "⚁", 3: "⚂", 4: "⚃", 5: "⚄", 6: "⚅"}
@classmethod
def format_roll_log(cls, player_name: str, dice: list[int], cat_key: str, pts: int) -> dict:
dice_str = "[" + " ".join(cls.DICE_FACES.get(d, str(d)) for d in sorted(dice)) + "]"
cat_de = CAT_NAMES["DE"].get(cat_key, cat_key)
cat_en = CAT_NAMES["EN"].get(cat_key, cat_key)
return {
"event": "CHAT_MSG",
"sender": player_name,
"text_de": f"würfelt {dice_str} -> {pts} Pkt. bei '{cat_de}'",
"text_en": f"rolled {dice_str} -> {pts} pts in '{cat_en}'",
"is_taunt": False,
"is_roll_log": True
}
@classmethod
def generate_reaction_event(cls, actor, all_participants, cat_key: str, pts: int, turn_num: int, has_joker: bool = False):
bots = [p for p in all_participants if p.is_bot]
if not bots:
return None
target_name = actor.name
other_bots = [b for b in bots if b != actor]
if not actor.is_bot:
sender = random.choice(bots).name
bucket = cls._pick_other_bucket(cat_key, pts, turn_num, has_joker)
idx = random.randrange(len(REACTIONS_DATA["DE"][bucket]))
return {
"event": "CHAT_MSG",
"sender": sender,
"text_de": REACTIONS_DATA["DE"][bucket][idx].format(target_name),
"text_en": REACTIONS_DATA["EN"][bucket][idx].format(target_name),
"is_taunt": False,
"is_roll_log": False
}
# Bot hat selbst gezogen
if pts == 0:
if not other_bots or random.random() < 0.75:
idx = random.randrange(len(REACTIONS_DATA["DE"]["self_zero"]))
return {
"event": "CHAT_MSG",
"sender": actor.name,
"text_de": REACTIONS_DATA["DE"]["self_zero"][idx],
"text_en": REACTIONS_DATA["EN"]["self_zero"][idx],
"is_taunt": False,
"is_roll_log": False
}
else:
commenter = random.choice(other_bots).name
idx = random.randrange(len(REACTIONS_DATA["DE"]["other_zero"]))
return {
"event": "CHAT_MSG",
"sender": commenter,
"text_de": REACTIONS_DATA["DE"]["other_zero"][idx].format(target_name),
"text_en": REACTIONS_DATA["EN"]["other_zero"][idx].format(target_name),
"is_taunt": False,
"is_roll_log": False
}
is_big = (
has_joker
or (cat_key in ["FH", "SS", "LS", "Y"] and pts > 0)
or (cat_key in ["1", "2", "3", "4", "5", "6"] and pts >= int(cat_key) * 3)
or pts >= 20
)
if is_big:
if not other_bots or random.random() < 0.70:
idx = random.randrange(len(REACTIONS_DATA["DE"]["self_taunt"]))
return {
"event": "CHAT_MSG",
"sender": actor.name,
"text_de": REACTIONS_DATA["DE"]["self_taunt"][idx],
"text_en": REACTIONS_DATA["EN"]["self_taunt"][idx],
"is_taunt": True,
"is_roll_log": False
}
else:
commenter = random.choice(other_bots).name
bucket = cls._pick_other_bucket(cat_key, pts, turn_num, has_joker)
idx = random.randrange(len(REACTIONS_DATA["DE"][bucket]))
return {
"event": "CHAT_MSG",
"sender": commenter,
"text_de": REACTIONS_DATA["DE"][bucket][idx].format(target_name),
"text_en": REACTIONS_DATA["EN"][bucket][idx].format(target_name),
"is_taunt": False,
"is_roll_log": False
}
else:
if not other_bots or random.random() < 0.65:
idx = random.randrange(len(REACTIONS_DATA["DE"]["self_normal"]))
return {
"event": "CHAT_MSG",
"sender": actor.name,
"text_de": REACTIONS_DATA["DE"]["self_normal"][idx],
"text_en": REACTIONS_DATA["EN"]["self_normal"][idx],
"is_taunt": False,
"is_roll_log": False
}
else:
commenter = random.choice(other_bots).name
idx = random.randrange(len(REACTIONS_DATA["DE"]["other_small"]))
return {
"event": "CHAT_MSG",
"sender": commenter,
"text_de": REACTIONS_DATA["DE"]["other_small"][idx].format(target_name),
"text_en": REACTIONS_DATA["EN"]["other_small"][idx].format(target_name),
"is_taunt": False,
"is_roll_log": False
}
@staticmethod
def _pick_other_bucket(cat_key, pts, turn_num, has_joker):
if pts == 0: return "other_zero"
if (cat_key == "Y" and pts == 50) or has_joker: return "other_yahtzee"
if cat_key == "LS" and pts == 40: return "other_ls"
if cat_key == "SS" and pts == 30: return "other_early_ss" if turn_num <= 4 else "other_ss"
if cat_key == "FH" and pts == 25: return "other_fh"
if cat_key == "4K" and pts >= 15: return "other_4k"
if (cat_key in ["1", "2", "3", "4", "5", "6"] and pts >= int(cat_key) * 3) or pts >= 18:
return "other_good"
return "other_small"