mirror of
https://github.com/ghndrx/shellmate.git
synced 2026-02-10 06:45:02 +00:00
- Fix import ordering (isort) - Remove unused imports - Fix line too long (>100 chars) - Fix variable naming (PIECES -> piece_chars) - Fix bare except clause - Remove unused variable
68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
"""Player models."""
|
|
|
|
from dataclasses import dataclass, field
|
|
from datetime import datetime
|
|
from enum import Enum
|
|
|
|
|
|
class PlayerType(Enum):
|
|
HUMAN = "human"
|
|
AI = "ai"
|
|
GUEST = "guest"
|
|
|
|
|
|
@dataclass
|
|
class Player:
|
|
"""Represents a player."""
|
|
|
|
id: str
|
|
username: str
|
|
player_type: PlayerType = PlayerType.HUMAN
|
|
elo: int = 1200
|
|
games_played: int = 0
|
|
wins: int = 0
|
|
losses: int = 0
|
|
draws: int = 0
|
|
created_at: datetime = field(default_factory=datetime.utcnow)
|
|
last_seen: datetime | None = None
|
|
|
|
@property
|
|
def winrate(self) -> float:
|
|
"""Calculate win rate percentage."""
|
|
if self.games_played == 0:
|
|
return 0.0
|
|
return (self.wins / self.games_played) * 100
|
|
|
|
def update_elo(self, opponent_elo: int, result: float, k: int = 32) -> int:
|
|
"""
|
|
Update ELO rating based on game result.
|
|
result: 1.0 for win, 0.5 for draw, 0.0 for loss
|
|
Returns the ELO change.
|
|
"""
|
|
expected = 1 / (1 + 10 ** ((opponent_elo - self.elo) / 400))
|
|
change = int(k * (result - expected))
|
|
self.elo += change
|
|
return change
|
|
|
|
def record_game(self, won: bool, draw: bool = False) -> None:
|
|
"""Record a completed game."""
|
|
self.games_played += 1
|
|
if draw:
|
|
self.draws += 1
|
|
elif won:
|
|
self.wins += 1
|
|
else:
|
|
self.losses += 1
|
|
self.last_seen = datetime.utcnow()
|
|
|
|
|
|
@dataclass
|
|
class AIPlayer(Player):
|
|
"""AI player with configurable difficulty."""
|
|
|
|
difficulty: int = 10 # Stockfish skill level 0-20
|
|
think_time_ms: int = 1000 # Time to think per move
|
|
|
|
def __post_init__(self):
|
|
self.player_type = PlayerType.AI
|