Example #1
0
 def go_back():
     if Networking.get_instance().is_host:
         next_scene = ChangeSceneEnum.SETMAXPLAYERSCENE
     else:
         next_scene = ChangeSceneEnum.JOINSCENE
     Networking.get_instance().disconnect()
     EventQueue.post(CustomEvent(next_scene))
 def execute(self):
     logger.info(
         f"Player {self._source.nickname} is asking you for command permission"
     )
     EventQueue.post(
         CustomEvent(CustomEventEnum.PERMISSION_PROMPT,
                     source=self._source,
                     target=self._target))
Example #3
0
 def callback_disconnect():
     """
     Define callback here when client's connection to host is interrupted.
     :return:
     """
     # if Networking.get_instance().is_host:
     logger.warning("It seems that client is not connected...")
     Networking.get_instance().disconnect()
     EventQueue.post(CustomEvent(ChangeSceneEnum.DISCONNECT))
    def endgame(self):
        if self._state == GameStateEnum.LOST:
            for player in self.players:

                with open(PROFILES, mode='r+', encoding='utf-8') as file:
                    temp = json.load(file)
                    file.seek(0)
                    file.truncate()
                    for user in temp:
                        if user['_nickname'] == player.nickname:
                            losses = user['_losses']
                            user['_losses'] = losses + 1
                    json.dump(temp, file)
                player.status = PlayerStatusEnum.NOT_READY

            EventQueue.block()

            EventQueue.post(CustomEvent(ChangeSceneEnum.LOSESCENE))

        else:
            for player in self.players:

                with open(PROFILES, mode='r+', encoding='utf-8') as file:
                    temp = json.load(file)
                    file.seek(0)
                    file.truncate()
                    for user in temp:
                        if user['_nickname'] == player.nickname:
                            wins = user['_wins']
                            user['_wins'] = wins + 1
                    json.dump(temp, file)
                player.status = PlayerStatusEnum.NOT_READY

            EventQueue.block()
            EventQueue.post(CustomEvent(ChangeSceneEnum.WINSCENE))
Example #5
0
    def load_game(self, save):
        """Instantiate a new family game and move to the lobby scene."""
        data = save

        # Restore game metadata
        game: GameStateModel = JSONSerializer.deserialize(data)
        game.host = self._current_player
        game.players = [self._current_player]
        game._players_turn_index = 0

        # Restore GameBoard
        GameStateModel.set_game(game)
        game.game_board.is_loaded = True
        Networking.get_instance().create_host()
        EventQueue.post(CustomEvent(ChangeSceneEnum.LOBBYSCENE))
Example #6
0
 def __init__(self):
     pygame.init()
     pygame.display.set_caption(MainConst.WINDOW_TITLE)
     self.screen = pygame.display.set_mode(MainConst.SCREEN_RESOLUTION, pygame.DOUBLEBUF)
     self.clock = pygame.time.Clock()
     SceneManager()
     EventQueue()
Example #7
0
    def is_clicked(self):
        if not self.hover():
            return False

        for event in EventQueue.get_instance():
            if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
                return True
        return False
Example #8
0
    def confirm(self):
        if self.character_enum:

            accept = True  # This is a boolean flag

            if any([player.role == self.character_enum for player in self._game.players]):
                accept = False

            if accept:  # means no one took this character
                EventQueue.post(CustomEvent(ChangeSceneEnum.LOBBYSCENE))

                event = ChooseCharacterEvent(self.character_enum, self._game.players.index(self._current_player))

                if Networking.get_instance().is_host:
                    Networking.get_instance().send_to_all_client(event)
                else:
                    Networking.get_instance().send_to_server(event)
Example #9
0
    def join(self):
        """
        Start the join host process in Networking
        :param ip_addr: ip address to connect
        :param next_scene: next scene to be called after the process completes
        :param args: extra arguments for the next scene
        :return:
        """

        ip_addr = self.text_bar_msg

        try:
            self._current_player.status = PlayerStatusEnum.NOT_READY
            Networking.get_instance().join_host(ip_addr, player=self._current_player)
            reply = Networking.wait_for_reply()
            # Connection error will be raised if no reply
            if reply:
                reply = JSONSerializer.deserialize(reply)
                if isinstance(reply, TooManyPlayersEvent):
                    raise TooManyPlayersException(self._current_player)
                # GameStateModel.set_game(JSONSerializer.deserialize(reply))
                EventQueue.post(CustomEvent(ChangeSceneEnum.LOBBYSCENE))
        except TimeoutError:
            msg = "Host not found."
            print(msg)
            self.init_error_message(msg)
        except TooManyPlayersException:
            msg = "Lobby is full. Cannot join the game."
            print(msg)
            self.init_error_message(msg)
            # Disconnect client that's trying to connect
            if not Networking.get_instance().is_host:
                Networking.get_instance().client.disconnect()
        except Networking.Client.SocketError:
            msg = "Failed to establish connection."
            print(msg)
            self.init_error_message(msg)
        except OSError:
            msg = "Invalid IP address"
            print(msg)
            self.init_error_message(msg)
Example #10
0
    def main(self):

        # Run main loop
        # FileImporter.play_music("media/music/intro_music/Kontrabandz-Get Down-kissvk.com.mp3", -1)
        #  FileImporter.play_music("media/music/Get_Down.wav", -1)

        while True:
            # Lock frame rate at 60 FPS
            try:
                self.clock.tick(60)
                EventQueue.fill_queue()
                self.screen.fill(Color.BLACK)

                for event in EventQueue.get_instance():
                    if event.type == pygame.QUIT:
                        Networking.get_instance().disconnect()

                        sys.exit()

                # Clear the screen to black
                self.screen.fill(Color.BLACK)

                SceneManager.draw()
                SceneManager.update(EventQueue.get_instance())

                EventQueue.flush_queue()

                pygame.display.flip()
            except Exception as e:
                info = sys.exc_info()
                logger.error("Exception was raised! Continuing, even though we might be screwed.")
                traceback.print_exception(*info)
Example #11
0
        def update(self, event_queue: EventQueue):
            self._active_scene.update(event_queue)
            for event in event_queue:
                if event.type == ChangeSceneEnum.STARTSCENE:
                    StateCleaner.clear()
                    self.next(StartScene)

                elif event.type == ChangeSceneEnum.SETMAXPLAYERSCENE:
                    self.next(SetMaxPlayers, self._current_player)

                elif event.type == ChangeSceneEnum.CHOOSEBOARDSCENE:
                    self.next(ChooseBoardScene, self._current_player)

                elif event.type == ChangeSceneEnum.CHARACTERSCENE:
                    self.next(CharacterScene, self._current_player)

                elif event.type == ChangeSceneEnum.CREATEGAMEMENU:
                    StateCleaner.clear()
                    self.next(CreateGameMenuScene, self._current_player)

                elif event.type == ChangeSceneEnum.HOSTJOINSCENE:
                    self._current_player = event.player
                    self.next(HostJoinScene, self._current_player)

                elif event.type == ChangeSceneEnum.HOSTMENUSCENE:
                    self.next(HostMenuScene, self._current_player)

                elif event.type == ChangeSceneEnum.JOINSCENE:
                    StateCleaner.clear()
                    self.next(JoinScene, self._current_player)

                elif event.type == ChangeSceneEnum.LOADGAME:
                    StateCleaner.clear()
                    self.next(LoadGameScene, self._current_player)

                elif event.type == ChangeSceneEnum.LOBBYSCENE:
                    self.next(LobbyScene, self._current_player)

                elif event.type == ChangeSceneEnum.DISCONNECT:
                    # player won't be redirected if they are not in game or trying to enter a game when disconnected
                    StateCleaner.clear()
                    if isinstance(self._active_scene, LobbyScene):
                        self.next(JoinScene)
                    self.next(StartScene)

                elif event.type == ChangeSceneEnum.GAMEBOARDSCENE:
                    EventQueue.unblock()
                    self.next(GameBoardScene, self._current_player)

                elif event.type == ChangeSceneEnum.WINSCENE:
                    self.next(WinScene)
                    EventQueue.unblock()

                elif event.type == ChangeSceneEnum.LOSESCENE:
                    self.next(LoseScene)
                    EventQueue.unblock()
Example #12
0
    def dodge(self, player: PlayerModel) -> bool:
        """
        Determines whether the player can
        dodge (out of turn) to avoid being
        knocked down and performs dodge.
        Returns False otherwise.

        :param player: player that is attempting to dodge
        :return: True if the player is able to dodge, False otherwise
        """
        logger.info("Attempting to dodge...")
        # Doge cannot dodge
        if player.role == PlayerRoleEnum.DOGE:
            self._log_player_dodge(1, player)
            return False

        if not self._valid_to_dodge(player):
            self._log_player_dodge(1, player)
            return False

        player_tile = self.game_state.game_board.get_tile_at(
            player.row, player.column)
        possible_dodge_target = NullModel()
        for dirn, nb_tile in player_tile.adjacent_tiles.items():
            if isinstance(nb_tile, TileModel):
                has_obstacle = player_tile.has_obstacle_in_direction(dirn)
                obstacle = player_tile.get_obstacle_in_direction(dirn)
                is_open_door = isinstance(
                    obstacle,
                    DoorModel) and obstacle.door_status == DoorStatusEnum.OPEN
                if not has_obstacle or is_open_door:
                    if nb_tile.space_status != SpaceStatusEnum.FIRE:
                        possible_dodge_target = nb_tile
                        break

        # If we couldn't find a potential space
        # to dodge, the player cannot avoid being
        # knocked down.
        if isinstance(possible_dodge_target, NullModel):
            self._log_player_dodge(1, player)
            return False

        # Pause the current event and ask player to dodge:
        EventQueue.post(CustomEvent(CustomEventEnum.DODGE_PROMPT, player))
        logger.info(
            f"Thread {threading.current_thread().getName()} going to sleep")
        # Go to sleep
        switch.pause_event.clear()
        switch.pause_event.wait()
        logger.info(f"Thread {threading.current_thread().getName()} woke up")
        if not GameStateModel.instance().dodge_reply:
            logger.info("Reply was no, knocking player down")
            return False

        logger.info("Reply was yes, attempting to dodge...")
        GameStateModel.instance().dodge_reply = False
        # Disassociate the victim/hazmat that the player
        # may be carrying since they cannot dodge with them
        player_victim = player.carrying_victim
        player_hazmat = player.carrying_hazmat
        is_carrying_victim = isinstance(player_victim, VictimModel)
        is_carrying_hazmat = isinstance(player_hazmat, HazmatModel)
        if is_carrying_victim:
            self._log_player_dodge(2, player, player_victim, player_tile)
            player_tile.add_associated_model(player_victim)
            player.carrying_victim = NullModel()

        if is_carrying_hazmat:
            self._log_player_dodge(2, player, player_hazmat, player_tile)
            player_tile.add_associated_model(player_hazmat)
            player.carrying_hazmat = NullModel()

        self._log_player_dodge(3, player)
        player.set_pos(possible_dodge_target.row, possible_dodge_target.column)
        # Costs 1 AP for Veteran to dodge
        # and 2 AP for the rest of the roles
        if player.role == PlayerRoleEnum.VETERAN:
            player.ap = player.ap - 1
        else:
            player.ap = player.ap - 2

        return True
Example #13
0
 def _continue(self):
     Networking.get_instance().disconnect()
     EventQueue.post(CustomEvent(ChangeSceneEnum.STARTSCENE))
Example #14
0
 def set_and_continue(board_type: GameBoardTypeEnum):
     GameStateModel.instance().board_type = board_type
     EventQueue.post(CustomEvent(ChangeSceneEnum.SETMAXPLAYERSCENE))
Example #15
0
 def create_new_game(self, game_kind: GameKindEnum, difficulty_level: DifficultyLevelEnum = None):
     """Instantiate a new family game and move to the lobby scene."""
     GameStateModel(self._current_player, 6, game_kind, GameBoardTypeEnum.ORIGINAL, difficulty_level)
     EventQueue.post(CustomEvent(ChangeSceneEnum.CHOOSEBOARDSCENE))
Example #16
0
 def _disconnect_and_back():
     Networking.get_instance().disconnect()
     EventQueue.post(CustomEvent(ChangeSceneEnum.HOSTMENUSCENE))
Example #17
0
 def countdown(self):
     EventQueue.post(CustomEvent(CustomEventEnum.ENABLE_KNOCKDOWN_PROMPT, self.player.nickname))
     time.sleep(5)
     EventQueue.post(CustomEvent(CustomEventEnum.DISABLE_KNOCKDOWN_PROMPT))
 def set_and_continue(desired_players: int):
     GameStateModel.instance().max_players = desired_players
     Networking.get_instance().create_host()
     EventQueue.post(CustomEvent(ChangeSceneEnum.LOBBYSCENE))
Example #19
0
 def execute(self):
     logging.info("Executing StartGameEvent.")
     GameStateModel.instance().state = GameStateEnum.PLACING_PLAYERS
     EventQueue.block()
     EventQueue.post(CustomEvent(ChangeSceneEnum.GAMEBOARDSCENE))
Example #20
0
 def countdown(self):
     EventQueue.post(CustomEvent(
         CustomEventEnum.ENABLE_VICTIM_SAVED_PROMPT))
     time.sleep(5)
     EventQueue.post(
         CustomEvent(CustomEventEnum.DISABLE_VICTIM_SAVED_PROMPT))
 def execute(self, *args, **kwargs):
     logger.info(f"Host has disconnected")
     EventQueue.post(CustomEvent(ChangeSceneEnum.DISCONNECT))