Files
isp-tycoon/economy.py

101 lines
4.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import time
from config import *
def calculate_effective_bandwidth(player: dict) -> int:
base = player["channel_capacity"]
server_boost = min(0.5, player["servers"] * 0.1)
cable_boost = player["cables"] * 0.2
return int(base * (1 + server_boost + min(1.0, cable_boost)))
def calculate_rating(player: dict) -> float:
monthly_income = player["coverage_customers"] * 15 * 30
income_score = 10 * (monthly_income / 100_000) ** 0.5
churn_ratio = (player["max_customers"] - player["coverage_customers"]) / max(player["max_customers"], 1)
stability_score = max(0, 20 - (churn_ratio * 100))
coverage_score = 20 * min(1, player["max_customers"] / 100_000)
return round(income_score * 0.4 + stability_score * 0.3 + coverage_score * 0.3, 2)
def calculate_client_retention(player: dict) -> tuple[int, str]:
effective_bw = calculate_effective_bandwidth(player)
required_bw_per_client = 2.0
max_supported = effective_bw / required_bw_per_client
if player["coverage_customers"] > max_supported:
churn_ratio = min(0.9, (player["coverage_customers"] - max_supported) / max_supported)
lost = int(player["coverage_customers"] * churn_ratio)
return lost, f"⚠️ Канала не хватает! Отток: {lost} клиентов/сутки"
if player["network_channel_level"] >= 3 and effective_bw >= 100:
return 0, "✅ Высокий канал: отток снижен на 15%"
return 0, "🌐 Связь стабильна."
def calculate_daily_income(player: dict) -> float:
supported_tiers = ["home"]
if player["network_channel_level"] >= 2:
supported_tiers.append("office")
if player["network_channel_level"] >= 3:
supported_tiers.append("gamer")
if player["network_channel_level"] >= 4:
supported_tiers.append("botfarm")
base_rate = sum(TARIFFS[t] for t in supported_tiers) / len(supported_tiers)
effective_bw = calculate_effective_bandwidth(player)
ratio = effective_bw / max(player["coverage_customers"], 1)
multiplier = 1.0 + 0.2 * min(1, ratio / 5)
return round(player["coverage_customers"] * base_rate * multiplier, 2)
def calculate_channel_cost(level: int) -> int:
base = 2000
return int(base * (1.8 ** (level - 1)))
def calculate_daily_offline_earnings(player: dict) -> float:
now = int(time.time())
if player["last_login_ts"] >= now:
return 0.0
offline_hours = max((now - player["last_login_ts"]) / 3600, 0)
if offline_hours > 48:
offline_hours = 48
effective_bw = calculate_effective_bandwidth(player)
server_capacity = player["servers"] * 500
stability = min(1.0, server_capacity / max(player["coverage_customers"], 1))
tariff = 15 # ₡/час/клиент (усреднённый)
income = player["coverage_customers"] * tariff * stability * offline_hours
return round(income, 2)
def handle_outage(player: dict) -> dict:
if player["outage_end_ts"] > 0:
now = int(time.time())
if now > player["outage_end_ts"]:
# Восстановление
return {**player, "outage_end_ts": 0, "backup_channel_active": False}
else:
if player["backup_channel_active"]:
return {**player, "speed": max(5, player["speed"] * 0.75)}
else:
churn = int(player["coverage_customers"] * 0.35)
return {
**player,
"coverage_customers": max(0, player["coverage_customers"] - churn),
"max_customers": max(0, player["max_customers"] - churn)
}
return player
def check_power_overload(player: dict) -> tuple[bool, str]:
power_usage = player["servers"] * SERVER_POWER
if power_usage > player["power_capacity"] * 1.2:
return True, "⚠️ КРИТИЧЕСКАЯ ПЕРЕНАГРУЗКА! Серверы отключаются..."
elif power_usage > player["power_capacity"]:
return True, "⚠️ Перегрузка! Некоторые серверы работают с лагами."
return False, ""
def sync_power(player: dict) -> dict:
power_usage = player["servers"] * SERVER_POWER
if power_usage > player["power_capacity"] * 1.2:
disabled = max(1, int(player["servers"] * 0.1))
new_servers = max(0, player["servers"] - disabled)
new_max = max(0, player["max_customers"] - disabled * 100)
return {**player, "servers": new_servers, "max_customers": new_max}
return {**player, "power_usage": power_usage}