Spaces:
Running
Running
File size: 1,798 Bytes
d3e2372 96ce456 d3e2372 96ce456 7ff19b5 96ce456 7ff19b5 96ce456 5fae15a d39e356 fc3eeaf 5fae15a 2921d52 d3e2372 2921d52 96ce456 7ff19b5 424e8dd d3e2372 7ff19b5 424e8dd d3e2372 424e8dd 7ff19b5 424e8dd 43d29d1 96ce456 7ff19b5 d3e2372 7ff19b5 96ce456 7ff19b5 96ce456 7ff19b5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Dict, Any, Optional
import time
import asyncio
app = FastAPI(title="Roblox Server Manager")
servers: Dict[str, dict] = {}
TIMEOUT = 60
lock = asyncio.Lock()
class ServerInfo(BaseModel):
ID: str
PlaceId: int
MaxPlayers: int
CurrentPlayers: int
Heartbeat: float
ISPRIVATE: bool = False
ActiveEvents: list[str] = []
class Config:
extra = "ignore"
def prune_servers():
now = time.time()
expired = [
sid for sid, data in servers.items()
if now - data.get("Heartbeat", 0) > TIMEOUT
]
for sid in expired:
del servers[sid]
@app.post("/heartbeat")
async def heartbeat(info: ServerInfo):
async with lock:
sid = info.ID
if not sid:
raise HTTPException(status_code=400, detail="Missing server ID")
if sid not in servers:
servers[sid] = {}
# ✅ Safe update
servers[sid].update({
"PlaceId": info.PlaceId,
"MaxPlayers": info.MaxPlayers,
"CurrentPlayers": info.CurrentPlayers,
"ISPRIVATE": info.ISPRIVATE,
"ActiveEvents": info.ActiveEvents or {},
"Heartbeat": time.time()
})
prune_servers()
return {"status":"ok","active_servers": servers}
@app.post("/shutdown")
async def shutdown(info: ServerInfo):
async with lock:
if info.ID in servers:
del servers[info.ID]
return {"status": "ok"}
return {"status": "not_found"}
@app.get("/servers")
async def get_servers():
async with lock:
prune_servers()
return servers
@app.get("/")
async def root():
return {"message": "Roblox Server Manager API is running!"} |