73 lines
2.6 KiB
Python
Raw Normal View History

2025-10-04 20:40:44 +02:00
from server.game_state import GameState
from server.models import BUILDING_CONFIGS, BuildingType
import time
class EconomyEngine:
"""Handles all economy calculations and ticks"""
def __init__(self, game_state: GameState):
self.game_state = game_state
def tick(self):
"""Process one economy tick for all players"""
current_time = time.time()
for player in self.game_state.players.values():
# Calculate power factor (10% if offline, 100% if online)
time_diff = current_time - player.last_online
if player.is_online:
power_factor = 1.0
else:
power_factor = 0.1
# Process player economy
self._process_player_economy(player, power_factor)
def _process_player_economy(self, player, power_factor: float):
"""Process economy for a single player"""
total_income = 0
total_population = 0
# Get all player buildings
buildings = self.game_state.get_player_buildings(player.player_id)
for building in buildings:
config = BUILDING_CONFIGS[building.building_type]
# Calculate base income
base_income = config.income
# Apply connectivity bonus for income-generating buildings
if base_income > 0:
zone_size = self.game_state.get_building_zone_size(building.x, building.y)
connectivity_bonus = 1.0 + (zone_size * 0.05) # 5% per road in zone
base_income = int(base_income * connectivity_bonus)
# Add to totals
total_income += base_income
total_population += config.population
# Apply power factor
total_income = int(total_income * power_factor)
# Update player stats
player.money += total_income
player.population = max(0, total_population)
# Prevent negative money (but allow debt for realism)
if player.money < -100000:
player.money = -100000
def calculate_building_stats(self, player_id: str, building_type: BuildingType) -> dict:
"""Calculate what a building would produce for a player"""
config = BUILDING_CONFIGS[building_type]
return {
"cost": config.cost,
"income": config.income,
"population": config.population,
"power_required": config.power_required,
"requires_population": config.requires_population,
"description": config.description
}