Beispiel #1
0
def test_RoomService_distribute_cards(room_db: list):
    room: Room = make_room()

    with patch.object(Room, "status",
                      new_callable=PropertyMock) as status_prop:
        status_prop.return_value = "PLAYING"

        room.players = [make_player(i) for i in range(3)]
        room.cards_left = {"D": 4}
        result: bool = RoomService.distribute_cards(room)
        assert not result

        room.players.append(make_player(3))
        room.cards_left = {"D": 3}
        result: bool = RoomService.distribute_cards(room)
        assert not result

        room.cards_left = {"D": 4}
        result: bool = RoomService.distribute_cards(room)

        assert result
        for player in room.players:
            assert player.hand == ["D"]

    result: bool = RoomService.distribute_cards(room)
    assert not result
Beispiel #2
0
def test_RoomService_add_player(room_db: list):
    player1: Player = make_player(1)
    player2: Player = make_player(2)

    room1: Room = make_room(1)
    room2: Room = make_room(2)

    assert not room1.players
    assert not room2.players

    with patch.object(Room, "status",
                      new_callable=PropertyMock) as status_prop:
        status_prop.return_value = "PLAYING"
        result: bool = RoomService.add_player(room1, player1)
        assert not result
        assert not room1.players

    result: bool = RoomService.add_player(room1, player1)

    assert result
    assert len(room1.players) == 1 and player1 in room1.players
    assert not room2.players

    result: bool = RoomService.add_player(room2, player2)

    assert result
    assert len(room1.players) == 1 and player1 in room1.players
    assert len(room2.players) == 1 and player2 in room2.players
Beispiel #3
0
def test_RoomService_cut_card(room_db: list):
    player1: Player = make_player(1)
    player2: Player = make_player(2)
    room: Room = make_room()

    player2.hand = ["D"]
    room.players = [player1, player2]
    room.cutter = player1
    room.cards_found = {"D": 0}
    room.cards_left = {"D": 1}

    assert room.status != "PLAYING"
    result: bool = RoomService.cut_card(room, player1, player2)
    assert not result

    with patch.object(Room, "status",
                      new_callable=PropertyMock) as status_prop:
        status_prop.return_value = "PLAYING"
        assert room.status == "PLAYING"

        result: str = RoomService.cut_card(room, player2, player1)
        assert not result

        result: str = RoomService.cut_card(room, player1, player1)
        assert not result

        result: str = RoomService.cut_card(room, player1, player2)
        assert result == "D"
        assert room.cutter is player2
        assert room.cards_found == {"D": 1}
        assert room.cards_left == {"D": 0}
        assert player2.hand == []
Beispiel #4
0
    def on_start(self, sid: str) -> dict:
        player = PlayerService.get_by_id(sid)

        if not player:
            return {
                "status": "ERROR",
                "data": {
                    "message": "Player not logged in."
                }
            }

        room = RoomService.get_by_id(player.room_id)
        if not room:
            return {"status": "ERROR", "data": {"message": "Invalid room."}}

        res = RoomService.start(room)
        if not res:
            return {
                "status": "ERROR",
                "data": {
                    "message": "Can not start game in this room."
                },
            }

        for player in room.players:
            self.emit_player(player)

        json = self.emit_room(room)
        return {"status": "SUCCESS", "data": json}
Beispiel #5
0
    def on_cut(self, sid: str, data: dict) -> dict:
        target_id = data.get("target")
        target_player = PlayerService.get_by_id(target_id)

        if not target_player:
            return {
                "status": "ERROR",
                "data": {
                    "message": "Invalid target id."
                }
            }

        src_player = PlayerService.get_by_id(sid)
        if not src_player:
            return {
                "status": "ERROR",
                "data": {
                    "message": "Player not logged in."
                }
            }

        if src_player.room_id != target_player.room_id:
            return {
                "status": "ERROR",
                "data": {
                    "message": "Can not cut player from other room."
                },
            }

        room = RoomService.get_by_id(target_player.room_id)
        if not room:
            return {"status": "ERROR", "data": {"message": "Invalid room."}}

        card = RoomService.cut_card(room, src_player, target_player)
        if not card:
            return {
                "status": "ERROR",
                "data": {
                    "message": "Error while cutting."
                }
            }

        if room.status == "ENDED":
            json = self.emit_end(room)
            return {"status": "SUCCESS", "data": json}

        res = RoomService.distribute_cards(room)
        if res:
            for player in room.players:
                self.emit_player(player)

        self.emit_notify(target_player,
                         {"message": "Your turn to cut a card!"})
        self.emit_player(target_player)
        json = self.emit_room(room)
        return {"status": "SUCCESS", "data": json}
Beispiel #6
0
def test_RoomService_update(room_db: list):
    room1: Room = make_room(1)

    room_db.append(room1)

    assert room_db[0].name == "room_1"

    updates = {"name": "newname"}
    RoomService.update(room1, updates)

    assert room_db[0].name == "newname"
Beispiel #7
0
def test_RoomService_get_open_rooms(room_db: list):
    room1: Room = make_room(1)

    room_db.append(room1)

    result: list = RoomService.get_open_rooms()

    assert len(result) == 1 and room1 in result

    room1.players = [make_player(i) for i in range(8)]

    result: list = RoomService.get_open_rooms()

    assert result == []
Beispiel #8
0
def test_RoomService_create_random(room_db: list):
    result: Room = RoomService.create_random()

    assert result.name and "-" in result.name and result.id

    assert len(room_db) == 1
    assert result in room_db
Beispiel #9
0
def test_RoomService_create(room_db: list):
    result: Room = RoomService.create("newroom")

    assert result.name == "newroom" and result.id

    assert len(room_db) == 1
    assert result in room_db
Beispiel #10
0
    def on_login(self, sid: str, data: dict) -> dict:
        player = PlayerService.get_by_id(sid)

        if player:
            return {
                "status": "ERROR",
                "data": {
                    "message": "Player already logged in."
                }
            }

        username = data.get("username")
        room_name = data.get("roomname")

        if not username:
            return {
                "status": "ERROR",
                "data": {
                    "message": "Invalid username."
                }
            }

        player = PlayerService.create({"name": username[:11], "id": sid})

        if not room_name:
            open_rooms = RoomService.get_open_rooms()
            if not len(open_rooms):
                room = RoomService.create_random()
            else:
                room = open_rooms[0]
        else:
            room = RoomService.get_by_name(room_name)
            if not room:
                room = RoomService.create(room_name)

        player = RoomService.add_player(room, player)
        if not player:
            return {
                "status": "ERROR",
                "data": {
                    "message": "Impossible to join this room."
                },
            }

        self.enter_room(sid, room.id)
        json = self.emit_room(room)
        return {"status": "SUCCESS", "data": json}
Beispiel #11
0
def test_RoomService_get_by_name(room_db: list):
    room1: Room = make_room(1)

    room_db.append(room1)

    result: Room = RoomService.get_by_name("room_1")

    assert result is room1
Beispiel #12
0
def test_RoomService_delete(room_db: list):
    room1: Room = make_room(1)
    room2: Room = make_room(2)

    room_db.append(room1)
    room_db.append(room2)

    result = RoomService.delete(room1)

    assert result == "id_1"
    assert len(room_db) == 1
    assert room1 not in room_db and room2 in room_db
Beispiel #13
0
def test_RoomService_distribute_cards(room_db: list):
    room: Room = make_room()
    assert room.status == "WAITING"

    result: bool = RoomService.start(room)
    assert not result

    room.players = [make_player(i) for i in range(4)]
    assert room.status == "READY"

    result: bool = RoomService.start(room)
    assert result
    assert room.status == "PLAYING"
    assert room.cutter is room.players[0]

    assert len(room.cards_found) == 3 and sum(room.cards_found.values()) == 0
    assert len(room.cards_left) == 3 and sum(room.cards_left.values()) == 20

    for player in room.players:
        assert player.team in ["Sherlock", "Moriarty"]
        assert len(player.hand) == 5
Beispiel #14
0
    def on_disconnect(self, sid: str) -> dict:
        player = PlayerService.get_by_id(sid)

        if not player:
            return {
                "status": "SUCCESS",
                "data": {
                    "message": "Player disconnect."
                }
            }

        room = RoomService.get_by_id(player.room_id)
        if not room:
            return {
                "status": "SUCCESS",
                "data": {
                    "message": "Player disconnect."
                }
            }

        if room.status == "PLAYING":
            self.emit_end(room)
            self.emit_notify(room,
                             {"message": f"{player.name} has left the game."})
            for r_player in room.players:
                self.leave_room(r_player.id, room.id)
                room.players.remove(r_player)
                PlayerService.delete(r_player)
            RoomService.delete(room)

        else:
            self.leave_room(player.id, room.id)
            room.players.remove(player)
            if not len(room.players):
                RoomService.delete(room)

            PlayerService.delete(player)

        return {"status": "SUCCESS", "data": {"message": "Player disconnect."}}
Beispiel #15
0
    def on_chat(self, sid: str, data: dict) -> dict:
        player = PlayerService.get_by_id(sid)

        if not player:
            return {
                "status": "ERROR",
                "data": {
                    "message": "Player not logged in."
                }
            }

        room = RoomService.get_by_id(player.room_id)
        if not room:
            return {"status": "ERROR", "data": {"message": "Invalid room."}}

        message = data.get("message", "").strip()
        if not message:
            return {"status": "ERROR", "data": {"message": "Invalid data."}}

        data = {"player": player.name, "message": message}
        json = self.emit_chat(room, data)
        return {"status": "SUCCESS", "data": json}