Chris4K's picture
Upload 195 files
4c75d73 verified
Raw
History Blame Contribute Delete
4.03 kB
"""
Player management service implementation.
"""
import time
from typing import Dict, Optional, List
import uuid
from ..interfaces.service_interfaces import IPlayerService
from ..interfaces.game_interfaces import IGameWorld
from ..core.player import Player
class PlayerService(IPlayerService):
"""Service for managing players in the game world."""
def __init__(self, game_world: IGameWorld):
self.game_world = game_world
def create_player(self, name: str, player_type: str) -> Player:
"""Create a new player with given name and type."""
player = Player(
id=str(uuid.uuid4()), # Generate a unique ID
name=name,
type=player_type,
x=100,
y=100,
level=1,
hp=100,
max_hp=100,
gold=50,
experience=0,
last_active=time.time(),
session_hash="",
ai_agent_id=""
)
return player
def get_player(self, player_id: str) -> Optional[Player]:
"""Get player by ID from the game world."""
return self.game_world.get_player(player_id)
def update_player(self, player: Player) -> bool:
"""Update player data in the game world."""
# Update the player's activity
player.update_activity()
# In a more complex implementation, we might save to database
# For now, the player is already updated in the game world
return True
def move_player(self, player_id: str, direction: str) -> bool:
"""Move player in specified direction."""
return self.game_world.move_player(player_id, direction)
def level_up_player(self, player_id: str) -> bool:
"""Level up a player if they have enough experience."""
player = self.get_player(player_id)
if not player:
return False
if player.can_level_up():
player.level_up()
self.update_player(player)
return True
return False
def get_all_players(self) -> Dict[str, Player]:
"""Get all players from the game world."""
return self.game_world.get_all_players()
def add_player_to_world(self, player: Player) -> bool:
"""Add player to the game world."""
return self.game_world.add_player(player)
def remove_player_from_world(self, player_id: str) -> bool:
"""Remove player from the game world."""
return self.game_world.remove_player(player_id)
def heal_player(self, player_id: str, amount: int) -> bool:
"""Heal a player by specified amount."""
player = self.get_player(player_id)
if not player:
return False
healed = player.heal(amount)
self.update_player(player)
return healed > 0
def damage_player(self, player_id: str, amount: int) -> bool:
"""Damage a player by specified amount."""
player = self.get_player(player_id)
if not player:
return False
player.take_damage(amount)
self.update_player(player)
return True
def get_player_stats(self, player_id: str) -> Dict:
"""Get formatted player statistics."""
player = self.get_player(player_id)
if not player:
return {}
return player.get_stats()
def cleanup_inactive_players(self, timeout_seconds: int = 300) -> int:
"""Remove inactive players and return count removed."""
all_players = self.get_all_players()
removed_count = 0
for player_id, player in list(all_players.items()):
if not player.is_active(timeout_seconds):
if self.remove_player_from_world(player_id):
removed_count += 1
return removed_count