157 lines
5.6 KiB
Python
157 lines
5.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
import random
|
|
from collections import Counter
|
|
|
|
CATEGORIES = ["1", "2", "3", "4", "5", "6", "3K", "4K", "FH", "SS", "LS", "Y", "CH"]
|
|
UPPER_KEYS = ["1", "2", "3", "4", "5", "6"]
|
|
|
|
class MatchParticipant:
|
|
"""Repräsentiert einen aktiven Spieler oder Bot innerhalb eines laufenden Server-Matches."""
|
|
def __init__(self, name: str, is_bot: bool = False, ai_level: str = None, session=None):
|
|
self.name = name
|
|
self.is_bot = is_bot
|
|
self.ai_level = ai_level
|
|
self.session = session # ClientSession bei echten Spielern, None bei Bots
|
|
self.scores = {k: None for k in CATEGORIES}
|
|
self.bonus = 0 # +100 für zusätzliche Yahtzees (Joker)
|
|
|
|
@property
|
|
def display_name(self) -> str:
|
|
if self.is_bot and self.ai_level:
|
|
return f"{self.name} (Bot - {self.ai_level})"
|
|
return self.name
|
|
|
|
@property
|
|
def upper_score(self) -> int:
|
|
return sum(self.scores[k] for k in UPPER_KEYS if self.scores.get(k) is not None)
|
|
|
|
@property
|
|
def upper_bonus(self) -> int:
|
|
return 35 if self.upper_score >= 63 else 0
|
|
|
|
@property
|
|
def total_score(self) -> int:
|
|
base = sum(v for v in self.scores.values() if v is not None)
|
|
return base + self.upper_bonus + self.bonus
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"name": self.name,
|
|
"display_name": self.display_name,
|
|
"is_bot": self.is_bot,
|
|
"ai_level": self.ai_level,
|
|
"scores": self.scores,
|
|
"upper_score": self.upper_score,
|
|
"upper_bonus": self.upper_bonus,
|
|
"yahtzee_bonus": self.bonus,
|
|
"total_score": self.total_score
|
|
}
|
|
|
|
|
|
class ServerGameLogic:
|
|
"""Autoritative Würfel-, Punkte- und Bot-Logik auf dem Server."""
|
|
|
|
@staticmethod
|
|
def calculate_score(cat_key: str, dice: list[int], has_joker: bool = False) -> int:
|
|
counts = Counter(dice)
|
|
s_dice = set(dice)
|
|
total = sum(dice)
|
|
|
|
if cat_key in UPPER_KEYS:
|
|
target = int(cat_key)
|
|
return dice.count(target) * target
|
|
|
|
if cat_key == "3K":
|
|
return total if any(c >= 3 for c in counts.values()) or has_joker else 0
|
|
if cat_key == "4K":
|
|
return total if any(c >= 4 for c in counts.values()) or has_joker else 0
|
|
if cat_key == "FH":
|
|
if has_joker:
|
|
return 25
|
|
vals = sorted(counts.values())
|
|
return 25 if vals == [2, 3] or vals == [5] else 0
|
|
if cat_key == "SS":
|
|
if has_joker:
|
|
return 30
|
|
for seq in ({1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}):
|
|
if seq.issubset(s_dice):
|
|
return 30
|
|
return 0
|
|
if cat_key == "LS":
|
|
if has_joker:
|
|
return 40
|
|
return 40 if s_dice in ({1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}) else 0
|
|
if cat_key == "Y":
|
|
return 50 if len(s_dice) == 1 else 0
|
|
if cat_key in ("CH", "C"):
|
|
return total
|
|
return 0
|
|
|
|
@classmethod
|
|
def choose_bot_hold(cls, bot: MatchParticipant, dice: list[int]) -> list[bool]:
|
|
level = bot.ai_level or "Medium"
|
|
if level == "Easy":
|
|
return [random.choice([True, False]) for _ in range(5)]
|
|
|
|
counts = Counter(dice)
|
|
most_val, most_cnt = counts.most_common(1)[0]
|
|
|
|
if (bot.scores["LS"] is None or bot.scores["SS"] is None) and most_cnt <= 2:
|
|
for seq in ([1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]):
|
|
if len(set(seq).intersection(set(dice))) >= 3:
|
|
kept = set()
|
|
held = []
|
|
for d in dice:
|
|
if d in seq and d not in kept:
|
|
kept.add(d)
|
|
held.append(True)
|
|
else:
|
|
held.append(False)
|
|
return held
|
|
|
|
if bot.scores["FH"] is None and sorted(counts.values()) == [2, 3]:
|
|
return [True] * 5
|
|
|
|
return [d == most_val for d in dice]
|
|
|
|
@classmethod
|
|
def choose_bot_category(cls, bot: MatchParticipant, dice: list[int]) -> tuple[str, int, bool]:
|
|
open_cats = [k for k, v in bot.scores.items() if v is None]
|
|
is_yz = len(set(dice)) == 1
|
|
has_joker = is_yz and bot.scores["Y"] == 50
|
|
|
|
if is_yz and "Y" in open_cats:
|
|
return "Y", 50, False
|
|
|
|
level = bot.ai_level or "Medium"
|
|
scored = {cat: cls.calculate_score(cat, dice, has_joker) for cat in open_cats}
|
|
|
|
if level == "Easy":
|
|
non_zero = [c for c, p in scored.items() if p > 0]
|
|
choice = random.choice(non_zero) if non_zero else random.choice(open_cats)
|
|
return choice, scored[choice], has_joker
|
|
|
|
best_cat = None
|
|
best_weight = -999.0
|
|
|
|
for cat, pts in scored.items():
|
|
weight = float(pts)
|
|
if cat == "Y" and pts == 0:
|
|
weight -= 45 if level in ("Hard", "Grandmaster") else 25
|
|
elif cat in ("LS", "SS", "FH") and pts > 0:
|
|
weight += 12
|
|
elif cat in UPPER_KEYS:
|
|
target = int(cat)
|
|
count = dice.count(target)
|
|
if count >= 3:
|
|
weight += 10 if level == "Grandmaster" else 6
|
|
elif pts == 0 and target >= 4:
|
|
weight -= 20
|
|
elif cat == "CH" and pts < 18 and len(open_cats) > 4:
|
|
weight -= 10
|
|
|
|
if weight > best_weight:
|
|
best_weight = weight
|
|
best_cat = cat
|
|
|
|
return best_cat, scored[best_cat], has_joker |