Don't allow automatic room creation

This commit is contained in:
0880
2026-01-20 19:57:01 +03:30
parent 5c268911b4
commit 4240312603

35
app.py
View File

@@ -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("/<id>")
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/<room_id>")
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: