def room(id): rooms = ps.get_rooms(rooms_path) characters_stat = ps.get_characters(characters_path) if "nickname" not in session: return redirect("/create-nickname") selected_room = {} for room in rooms: if room["id"] == id: selected_room = room break else: return redirect("/list-rooms") if request.method == "POST": password = request.form["password"] if password == selected_room["password"]: if put_player_into_room(selected_room, session["nickname"]): session["room_id"] = id characters_stat.append( game.create_character(session["nickname"], session["user_id"])) ps.export_rooms(rooms_path, rooms) ps.export_characters(characters_path, characters_stat) return redirect(f"/room/{id}") else: return render_template("join_room.html", room=selected_room, message="Password is incorrect") if "room_id" not in session or session["room_id"] != id: return render_template("join_room.html", room=selected_room) return render_template("room.html", room=selected_room)
def room_start(id): rooms = ps.get_rooms(rooms_path) for room in rooms: if room["id"] == str(id): room["game_state"] = "playing" ps.export_rooms(rooms_path, rooms) game.initialize_my_map() return redirect(f"/")
def remove_player_from_room(player, room_id): rooms = ps.get_rooms(rooms_path) for selected_room in rooms: if selected_room["id"] == room_id: room = selected_room for num in range(4): if room[f"{num + 1}"] == player: room[f"{num + 1}"] = "" ps.export_rooms(rooms_path, rooms) break
def put_player_into_room(room, player): rooms = ps.get_rooms(rooms_path) for num in range(4): if room[f"{num + 1}"] == "": room[f"{num + 1}"] = player session["user_id"] = num + 1 for selected_room in rooms: if selected_room["id"] == room["id"]: selected_room = room ps.export_rooms(rooms_path, rooms) return True return False
def create_room(): rooms = ps.get_rooms(rooms_path) if "nickname" not in session: return redirect("/create-nickname") if "room_id" in session: remove_player_from_room(session["nickname"], session["room_id"]) if request.method == "POST": data = request.form password = data["password"] name = data["name"] if name != "" and name[0] != " " and \ password != "" and password[0] != " " and \ check_room_name_availability(name): next_id = str(int(rooms[-1]["id"]) + 1) new_room = {'id': next_id, 'name': name, 'password': password, '1': '', '2': '', '3': '', '4': '', "game_state": "lobby"} rooms.append(new_room) ps.export_rooms(rooms_path, rooms) return redirect(f"/room/{next_id}") else: return render_template("create-room.html", message="Invalid name or password") return render_template("create-room.html")
def reset_rooms(): rooms = [{'id': '1', 'name': 'test', 'password': '******', '1': '', '2': '', '3': '', '4': '', "game_state": "lobby"}, {'id': '2', 'name': 'test2', 'password': '******', '1': '', '2': '', '3': '', '4': '', "game_state": "lobby"}] ps.export_rooms(rooms_path, rooms) return redirect("/list-rooms")