Don't allow automatic room creation
This commit is contained in:
35
app.py
35
app.py
@@ -206,20 +206,47 @@ async def favicon_update_s(request):
|
|||||||
return HTTPResponse(request, favicon_update, content_type="image/x-icon")
|
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("/")
|
@app.GET("/")
|
||||||
async def home(request):
|
async def home(request):
|
||||||
return render(request, "index.html")
|
return render(request, "home.html")
|
||||||
|
|
||||||
|
|
||||||
@app.GET("/<id>")
|
@app.GET("/<id>")
|
||||||
async def home_with_id(request, id):
|
async def game(request, id):
|
||||||
return render(request, "index.html")
|
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>")
|
@app.POST("/join/<room_id>")
|
||||||
async def join(request, room_id):
|
async def join(request, room_id):
|
||||||
if room_id not in rooms:
|
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]
|
room: Room = rooms[room_id]
|
||||||
player = room.add_player()
|
player = room.add_player()
|
||||||
if player:
|
if player:
|
||||||
|
|||||||
Reference in New Issue
Block a user