feat: new endpoints for users and updated models
This commit is contained in:
41
app/services/server/command.py
Normal file
41
app/services/server/command.py
Normal file
@ -0,0 +1,41 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from fastapi import HTTPException
|
||||
from typing import Dict
|
||||
|
||||
# Глобальное хранилище команд (в реальном проекте используйте БД)
|
||||
pending_commands: Dict[str, Dict] = {}
|
||||
|
||||
class CommandService:
|
||||
async def add_command(self, command_data):
|
||||
try:
|
||||
command_id = str(uuid.uuid4())
|
||||
pending_commands[command_id] = {
|
||||
"command": command_data.command,
|
||||
"server_ip": command_data.server_ip,
|
||||
"require_online_player": command_data.require_online_player,
|
||||
"created_at": datetime.now().isoformat()
|
||||
}
|
||||
print(f"[{datetime.now()}] Добавлена команда: {command_data.command} "
|
||||
f"для сервера {command_data.server_ip}")
|
||||
return {"status": "success", "command_id": command_id}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
async def get_commands(self, server_ip: str):
|
||||
try:
|
||||
# Получаем команды для указанного сервера
|
||||
commands = [
|
||||
{"id": cmd_id, "command": cmd["command"], "require_online_player": cmd["require_online_player"]}
|
||||
for cmd_id, cmd in pending_commands.items()
|
||||
if cmd["server_ip"] == server_ip
|
||||
]
|
||||
|
||||
# Удаляем полученные команды (чтобы не выполнять их повторно)
|
||||
for cmd_id in list(pending_commands.keys()):
|
||||
if pending_commands[cmd_id]["server_ip"] == server_ip:
|
||||
del pending_commands[cmd_id]
|
||||
|
||||
return {"status": "success", "commands": commands}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
70
app/services/server/event.py
Normal file
70
app/services/server/event.py
Normal file
@ -0,0 +1,70 @@
|
||||
from fastapi import HTTPException
|
||||
from datetime import datetime
|
||||
import json
|
||||
from app.services.coins import CoinsService
|
||||
from app.models.server.event import PlayerEvent, OnlinePlayersUpdate
|
||||
|
||||
class EventService:
|
||||
def __init__(self):
|
||||
self.coins_service = CoinsService()
|
||||
|
||||
async def process_event(self, event_data: dict):
|
||||
try:
|
||||
event_type = event_data.get("event_type")
|
||||
server_ip = event_data.get("server_ip", "unknown")
|
||||
|
||||
if event_type == "player_join":
|
||||
player_name = event_data["player_name"]
|
||||
player_id = event_data["player_id"]
|
||||
print(f"[{datetime.now()}] Игрок вошел: {player_name} (ID: {player_id}) "
|
||||
f"IP сервера: {server_ip}")
|
||||
|
||||
elif event_type == "player_quit":
|
||||
player_name = event_data["player_name"]
|
||||
player_id = event_data["player_id"]
|
||||
print(f"[{datetime.now()}] Игрок вышел: {player_name} (ID: {player_id}) "
|
||||
f"IP сервера: {server_ip}")
|
||||
|
||||
elif event_type == "player_session":
|
||||
player_name = event_data["player_name"]
|
||||
player_id = event_data["player_id"]
|
||||
duration = event_data["duration"]
|
||||
|
||||
# Обновляем монеты через выделенный сервис
|
||||
await self.coins_service.update_player_coins(player_id, player_name, duration, server_ip)
|
||||
|
||||
print(f"[{datetime.now()}] Игрок {player_name} провел на сервере: {duration} секунд "
|
||||
f"IP сервера: {server_ip}")
|
||||
|
||||
elif event_type == "online_players_update":
|
||||
players = event_data["players"]
|
||||
print(f"\n[{datetime.now()}] Текущие онлайн-игроки ({len(players)}): "
|
||||
f"IP сервера: {server_ip}")
|
||||
|
||||
# Обрабатываем каждого игрока
|
||||
for player in players:
|
||||
player_id = player["player_id"]
|
||||
player_name = player["player_name"]
|
||||
online_time = player["online_time"]
|
||||
|
||||
# Обновляем монеты через выделенный сервис
|
||||
await self.coins_service.update_player_coins(
|
||||
player_id, player_name, online_time, server_ip
|
||||
)
|
||||
|
||||
hours, remainder = divmod(online_time, 3600)
|
||||
minutes, seconds = divmod(remainder, 60)
|
||||
print(f" - {player_name} (ID: {player_id}) "
|
||||
f"Онлайн: {hours}ч {minutes}м {seconds}с")
|
||||
print()
|
||||
|
||||
else:
|
||||
print(f"[{datetime.now()}] Неизвестное событие: {json.dumps(event_data, indent=2)}")
|
||||
raise HTTPException(status_code=400, detail="Invalid event type")
|
||||
|
||||
return {"status": "success", "message": "Event processed"}
|
||||
|
||||
except Exception as e:
|
||||
print(f"[{datetime.now()}] Ошибка обработки события: {str(e)}")
|
||||
print(f"Полученные данные: {json.dumps(event_data, indent=2)}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
Reference in New Issue
Block a user