import asyncio import json import websockets PORT = 7860 BAGLI_OYUNCULAR = {} ARAMA_HAVUZU = [] 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") if 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())