From 42403126034a03937c0877d0489b47f70d212da8 Mon Sep 17 00:00:00 2001 From: 0880 <98263509+0880880@users.noreply.github.com> Date: Tue, 20 Jan 2026 19:57:01 +0330 Subject: [PATCH] Don't allow automatic room creation --- app.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 45dbaac..5b98e12 100644 --- a/app.py +++ b/app.py @@ -206,20 +206,47 @@ async def favicon_update_s(request): return HTTPResponse(request, favicon_update, content_type="image/x-icon") +letters = string.ascii_lowercase + string.digits + + +def room_key(): + key: str = "".join(random.choices(letters, k=4)) + while key in rooms.keys(): + key: str = "".join(random.choices(letters, k=4)) + return key + + +@app.POST("/create_room") +async def new_room(request): + if len(rooms) == len(letters) ** 4: + return HTTPResponse(request, "Out of service", status=501) + key = room_key() + rooms[key] = Room() + return JSONResponse( + request, + { + "id": key, + }, + ) + @app.GET("/") async def home(request): - return render(request, "index.html") + return render(request, "home.html") @app.GET("/") -async def home_with_id(request, id): - return render(request, "index.html") +async def game(request, id): + if not re.match(r"[a-z0-9]{4}", id): + return redirect("/") + if id not in rooms: + return redirect("/") + return render(request, "game.html") @app.POST("/join/") async def join(request, room_id): if room_id not in rooms: - rooms[room_id] = Room() + return JSONResponse(request, {"code": "NOGO", "error": "Room not found."}, 404) room: Room = rooms[room_id] player = room.add_player() if player: