Files
popa_minecraft_launcher_api/app/services/server/command.py

42 lines
1.9 KiB
Python

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))