Spaces:
Sleeping
Sleeping
Download server.py from Ensarioglu/NOXBALL-SERVER: direct link, hf CLI and curl.
- Browser
- Download file 4.15 kB
-
https://huggingface.co/spaces/Ensarioglu/NOXBALL-SERVER/resolve/main/server.py
- Command line
-
hf download hf://spaces/Ensarioglu/NOXBALL-SERVER/server.py
-
curl -L -o server.py https://huggingface.co/spaces/Ensarioglu/NOXBALL-SERVER/resolve/main/server.py
4.15 kB
| import asyncio | |
| import json | |
| import random | |
| import websockets | |
| PORT = 7860 | |
| BAGLI_OYUNCULAR = {} | |
| ARAMA_HAVUZU = [] | |
| # --- SUNUCU HAFIZASINDAKİ OYUNCU VERİTABANI --- | |
| # Format: {"cihaz_seri_no": "BENZERSİZ_NOX_ID"} | |
| OYUNCU_VERITABANI = {} | |
| def nox_id_uret(): | |
| karakterler = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" | |
| return ''.join(random.choice(karakterler) for _ in range(8)) | |
| async def handler(websocket): | |
| client_id = str(id(websocket)) | |
| BAGLI_OYUNCULAR[client_id] = websocket | |
| mevcut_nox_id = None | |
| try: | |
| async for message in websocket: | |
| paket = json.loads(message) | |
| gonderen = paket.get("gonderen", client_id) | |
| veri = paket.get("icerik", paket) # Godot'tan direkt veya relay gelen paket uyumu | |
| tip = veri.get("tip") | |
| # --- YENİ KİMLİK DOĞRULAMA TALEBİ MANTIĞI --- | |
| if tip == "id_talep": | |
| seri_no = veri.get("nox_id") # Godot bu komutta nox_id yerine cihaz seri no yollar | |
| if seri_no: | |
| if seri_no not in OYUNCU_VERITABANI: | |
| yeni_id = nox_id_uret() | |
| while yeni_id in OYUNCU_VERITABANI.values(): | |
| yeni_id = nox_id_uret() | |
| OYUNCU_VERITABANI[seri_no] = yeni_id | |
| print(f"SİSTEM: Yeni Oyuncu Kaydedildi! Seri No: {seri_no} -> Nox ID: {yeni_id}") | |
| mevcut_nox_id = OYUNCU_VERITABANI[seri_no] | |
| # Godot'nun network_manager'da beklediği paket formatına tam uyumlu yanıt: | |
| onay_paketi = { | |
| "gonderen": "server", | |
| "icerik": { | |
| "tip": "id_onay", | |
| "nox_id": mevcut_nox_id | |
| } | |
| } | |
| await websocket.send(json.dumps(onay_paketi)) | |
| elif tip == "arama_baslat": | |
| mevcut_nox_id = veri.get("nox_id") | |
| if mevcut_nox_id and mevcut_nox_id not in [p["nox_id"] for p in ARAMA_HAVUZU]: | |
| ARAMA_HAVUZU.append({"nox_id": mevcut_nox_id, "ws": websocket}) | |
| print(f"HAVUZ: {mevcut_nox_id} sıraya girdi. Havuz Boyutu: {len(ARAMA_HAVUZU)}") | |
| # HAVUZ KONTROLÜ: Eğer sırada en az 2 gerçek oyuncu varsa eşleştir! | |
| if len(ARAMA_HAVUZU) >= 2: | |
| p1 = ARAMA_HAVUZU.pop(0) | |
| p2 = ARAMA_HAVUZU.pop(0) | |
| mac_paketi = {"tip": "mac_bulundu", "durum": "basla"} | |
| await p1["ws"].send(json.dumps({"gonderen": "server", "icerik": mac_paketi})) | |
| await p2["ws"].send(json.dumps({"gonderen": "server", "icerik": mac_paketi})) | |
| print(f"SİSTEM: Maç kuruldu! {p1['nox_id']} VS {p2['nox_id']}") | |
| elif tip == "arama_iptal": | |
| mevcut_nox_id = veri.get("nox_id") | |
| ARAMA_HAVUZU = [p for p in ARAMA_HAVUZU if p["nox_id"] != mevcut_nox_id] | |
| print(f"HAVUZ: {mevcut_nox_id} sıradan çıktı.") | |
| # Diğer tüm verileri (hareket vs.) oyun esnasında odadaki rakibe pasla | |
| else: | |
| if BAGLI_OYUNCULAR: | |
| mesaj_str = json.dumps({"gonderen": client_id, "icerik": veri}) | |
| await asyncio.gather(*[ | |
| oyuncu.send(mesaj_str) | |
| for id_k, oyuncu in BAGLI_OYUNCULAR.items() | |
| if id_k != client_id | |
| ]) | |
| except websockets.ConnectionClosed: | |
| pass | |
| finally: | |
| if client_id in BAGLI_OYUNCULAR: | |
| del BAGLI_OYUNCULAR[client_id] | |
| ARAMA_HAVUZU = [p for p in ARAMA_HAVUZU if p["ws"] != websocket] | |
| async def main(): | |
| print(f"Eşleştirmeli NoxBall Sunucusu {PORT} portunda aktif...") | |
| async with websockets.serve(handler, "0.0.0.0", PORT): | |
| await asyncio.Future() | |
| if __name__ == "__main__": | |
| asyncio.run(main()) |