예제 #1
0
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)
예제 #2
0
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"/")
예제 #3
0
def list_rooms():
    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"])
        session.pop("room_id", None)
    return render_template("list-rooms.html", rooms=rooms)
예제 #4
0
def room_info(id):
    rooms = ps.get_rooms(rooms_path)
    current_room = {}
    for room in rooms:
        if room["id"] == id:
            current_room = room
            break
    return current_room
예제 #5
0
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
예제 #6
0
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
예제 #7
0
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")
예제 #8
0
def check_room_name_availability(name):
    rooms = ps.get_rooms(rooms_path)
    for room in rooms:
        if room["name"] == name:
            return False
    return True