def _send_reply_event(self, reply: bool): if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client( PermissionReplyEvent(reply, self._source, self._target)) else: Networking.get_instance().send_to_server( PermissionReplyEvent(reply, self._source, self._target))
def __init__(self, screen, current_player: PlayerModel): super().__init__() self._current_player = current_player self._game = GameStateModel.instance() self.player_boxes = [] if Networking.get_instance().is_host: self._current_player.color = Color.BLUE self._game.host.color = Color.BLUE self._current_player.status = PlayerStatusEnum.READY self._player_count = len(self._game.players) self.isReady = False self.resolution = (1280, 700) self.sprite_grp = pygame.sprite.Group() self.players_not_ready_prompt = None self._init_all() if self._game.rules == GameKindEnum.EXPERIENCED: self.buttonSelChar.on_click( EventQueue.post, CustomEvent(ChangeSceneEnum.CHARACTERSCENE)) if Networking.get_instance().is_host: self._current_player.status = PlayerStatusEnum.READY self.isReady = True self.start_button.on_click(self.start_game) self.start_button.disable() if self._game.rules == GameKindEnum.EXPERIENCED and self._current_player.role == PlayerRoleEnum.FAMILY: self._current_player.status = PlayerStatusEnum.NOT_READY else: self._current_player.status = PlayerStatusEnum.NOT_READY self.buttonReady.on_click(self.set_ready) self.buttonBack.on_click(self.go_back)
def send_event_and_close_menu(self, tile_model: TileModel, menu_to_close: Interactable): event = None if self.run_checks(tile_model) and self.remove: event = RemoveHazmatEvent(tile_model.row, tile_model.column) elif self.run_checks_pickup(tile_model) and self.pickup: event = PickupHazmatEvent(tile_model.row, tile_model.column) elif self.run_checks_drop(tile_model) and self.drop: event = DropHazmatEvent(tile_model.row, tile_model.column) self.drop = False self.pickup = False self.remove = False if event: if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().client.send(event) menu_to_close.disable() menu_to_close.on_click(None)
def countdown(self, count): while count and self.running: if switch.ABORT: return mins, secs = divmod(count, 60) temp = '{:02d}:{:02d}'.format(mins, secs) self.time_str = f"TIME LEFT: {temp}" self._current_sprite.text_time_left = self.font_time.render( self.time_str, True, Color.GREEN2) time.sleep(1) count -= 1 self.time_str = "" self.enabled = False self.running = False turn_event = EndTurnAdvanceFireEvent() # send end turn, see ChatBox for example try: if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(turn_event) else: Networking.get_instance().send_to_server(turn_event) except AttributeError as e: pass if not self._should_advance_fire: self._should_advance_fire = True
def end_command(self): event = StopCommandEvent(self._source) if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().send_to_server(event)
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 send_event_and_close_menu(self, tile_model: TileModel, menu_to_close: Interactable): parking_type = tile_model.space_kind vehicle_type = "AMBULANCE" if parking_type == SpaceKindEnum.AMBULANCE_PARKING else "ENGINE" event = None if self._run_checks_dismount_vehicle(tile_model, vehicle_type): event = DismountVehicleEvent(vehicle_type, player=self._current_player) elif self._run_checks_ride_vehicle(tile_model, vehicle_type): event = RideVehicleEvent(vehicle_type, player=self._current_player) elif self._run_checks_drive_ambulance(tile_model): second_tile = GameStateModel.instance().game_board.get_other_parking_tile(tile_model) event = DriveVehicleEvent("AMBULANCE", (tile_model, second_tile)) elif self._run_checks_drive_engine(tile_model): second_tile = GameStateModel.instance().game_board.get_other_parking_tile(tile_model) event = DriveVehicleEvent("ENGINE", (tile_model, second_tile)) else: menu_to_close.disable() return if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().client.send(event) menu_to_close.disable()
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)
def send_pickup_event(self, tile_model: TileModel, menu_to_close: Interactable): victims = [ model for model in tile_model.associated_models if isinstance(model, VictimModel) ] logger.info( f"Player has a victim: {isinstance(self._current_player.carrying_victim, VictimModel)}" ) victim = NullModel() if victims: victim = victims[0] if not victim: return if not self.check_pickup(tile_model): return event = PickupVictimEvent(victim.row, victim.column) if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().client.send(event) menu_to_close.disable()
def notify_game_state(self, game_state: GameStateEnum): print(game_state) if game_state in [GameStateEnum.LOST, GameStateEnum.WON]: event = EndGameEvent(game_state) try: Networking.get_instance().send_to_all_client(event) except AttributeError as e: pass
def send_event(self): self.run_checks(self.board.get_tile_at(0, 0)) event = VeteranGiveExperienceEvent(self._current_player, self.player_booleans) if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().send_to_server(event)
def send_event_and_close_menu(self, tile_model: TileModel, menu_to_close: Interactable): if not self.run_checks(tile_model): return event = IdentifyPOIEvent(tile_model.row, tile_model.column) if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().client.send(event)
def instantiate_event(self, wall_sprite: WallSprite): wall = wall_sprite.wall if not self.check(wall): return event = ChopEvent(wall) if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().send_to_server(event)
def send_event_and_close_menu(self, tile_model: TileModel, menu_to_close: Interactable): if not self.run_checks(tile_model): return target = [player for player in self.game.players if (player.row, player.column) == (tile_model.row, tile_model.column)][0] event = CommandPermissionEvent(self._current_player, target) if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().send_to_server(event) menu_to_close.disable()
def send_event_and_close_menu(self, tile_model: TileModel, menu_to_close: Interactable): if not self.run_checks(tile_model): menu_to_close.disable() return event = ExtinguishEvent(tile_model) if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().send_to_server(event) menu_to_close.disable()
def send_stop_lead_event(self, tile_model: TileModel, menu_to_close: Interactable): if not self.check_stop_lead(tile_model): return event = StopLeadingVictimEvent(self._current_player.row, self._current_player.column) if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().client.send(event) menu_to_close.disable()
def send_event_and_close_menu(self, tile_model: TileModel, menu_to_close: Interactable): if not self.run_checks(tile_model): menu_to_close.disable() return event = ResuscitateEvent(tile_model.row, tile_model.column) ## event = HazmatEvent(tile_model.row, tile_model.column) if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().client.send(event) menu_to_close.disable() menu_to_close.on_click(None)
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))
def instantiate_event(self, door_model: DoorModel): event: TurnEvent = None self.door.menu_shown = False self.door.button_input.disable() if door_model.door_status == DoorStatusEnum.OPEN: event = CloseDoorEvent(door_model) elif door_model.door_status == DoorStatusEnum.CLOSED: event = OpenDoorEvent(door_model) if event: if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().send_to_server(event)
def create_profile(self, text_bar: InputBox): temp = {} with open(self.profiles, mode='r+', encoding='utf-8') as myFile: temp = json.load(myFile) size = len(temp) if size >= 3: return if not text_bar.text.strip(): return size = len(text_bar.text.strip()) if size <= 12: player_model = PlayerModel( ip=Networking.get_instance().get_ip(), nickname=text_bar.text.strip() ) player = JSONSerializer.serialize(player_model) temp.append(player) else: msg = "Nickname must have less than 12 letters" self.init_error_message(msg) with open(self.profiles, mode='w', encoding='utf-8') as myFile: json.dump(temp, myFile) self.update_profiles()
def process_input(self, tile_sprite: TileSprite): if not GameStateModel.instance().players_turn.has_pos: tile_model = GameStateModel.instance().game_board.get_tile_at( tile_sprite.row, tile_sprite.column) if not self.run_checks(tile_model): return event = ChooseStartingPositionEvent(tile_model.row, tile_model.column) self.choose_prompt.kill() if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().send_to_server(event)
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)
def update(self, event_queue: EventQueue): self.chat_textbox.update(event_queue) message = self.chat_textbox.message if message: chat_event = ChatEvent(self.chat_textbox.message, self.current_player.nickname) if self.current_player.ip == GameStateModel.instance().host.ip: Networking.get_instance().send_to_all_client(chat_event) else: Networking.get_instance().send_to_server(chat_event) self.chat_textbox.message = '' if GameStateModel.instance(): self.chat_history = GameStateModel.instance().chat_history self._init_message_box() self.chat_textbox.rect.w = self.chat_history_bg.rect.w
def send_event_and_close_menu(self, tile_model: TileModel, menu_to_close: Interactable, row: int = -1, column: int = -1): event = FireDeckGunEvent(row=row, column=column) if not self.run_checks(tile_model): menu_to_close.disable() return if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().client.send(event) if menu_to_close: menu_to_close.disable()
def send_event_and_close_menu(self, tile_model: TileModel, menu_to_close: Interactable): game_board: GameBoardModel = GameStateModel.instance().game_board spot_list = game_board.engine_spots if self.ambulance_placed else game_board.ambulance_spots parking_spot = [spot for spot in spot_list if tile_model in spot] if not parking_spot: return parking_spot = parking_spot[0] vehicle = game_board.engine if self.ambulance_placed else game_board.ambulance event = VehiclePlacedEvent(vehicle, parking_spot) if Networking.get_instance().is_host: Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().client.send(event) if not self.ambulance_placed: self.ambulance_placed = True elif not self.engine_placed: self.engine_placed = True
def run_checks(self, tile_model: TileModel) -> bool: game: GameStateModel = GameStateModel.instance() if game.state != GameStateEnum.PLACING_VEHICLES: return False if not Networking.get_instance().is_host: return False if tile_model.space_kind == SpaceKindEnum.INDOOR: return False return True
def start_game(self): """Callback for when the host tries to start the game.""" game = GameStateModel.instance() players_ready = len([ player for player in game.players if player.status == PlayerStatusEnum.READY ]) if not self._game.rules == GameKindEnum.FAMILY: if any(player.role == PlayerRoleEnum.FAMILY for player in game.players): return if not players_ready == game.max_players: self.not_enough_players_ready_prompt() return # Perform the start game hook in Networking (ie. stop accepting new connections and kill broadcast) if Networking.get_instance().is_host: # Kill the broadcast for box in self.player_boxes: box.delete_class() Networking.get_instance().send_to_all_client(StartGameEvent())
def set_ready(self): """Set the status of the current player to ready.""" if not self.isReady: if not (self._game.rules == GameKindEnum.FAMILY and self._current_player.role == PlayerRoleEnum.FAMILY) \ or self._game.rules == GameKindEnum.FAMILY: self.isReady = True self.sprite_grp.remove(self.buttonReady) box_size = (130, 48) self.buttonReady = RectButton( 1050, 575, box_size[0], box_size[1], Color.BLACK, 0, Text(pygame.font.SysFont('Agency FB', 25), "Ready", Color.GREEN)) self.buttonReady.change_bg_image(MEDIA_CONSTS.WOOD) self.buttonReady.add_frame(MEDIA_CONSTS.FRAME) self.buttonReady.on_click(self.set_ready) self.sprite_grp.add(self.buttonReady) self._current_player.status = PlayerStatusEnum.READY event = ReadyEvent(self._current_player, True) if self._current_player.ip == GameStateModel.instance( ).host.ip: event.execute() Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().send_to_server(event) else: self.isReady = False self.sprite_grp.remove(self.buttonReady) box_size = (130, 48) self.buttonReady = RectButton( 1050, 575, box_size[0], box_size[1], Color.BLACK, 0, Text(pygame.font.SysFont('Agency FB', 25), "Not Ready", Color.GREY)) self.buttonReady.change_bg_image(MEDIA_CONSTS.WOOD) self.buttonReady.add_frame(MEDIA_CONSTS.FRAME) self.buttonReady.on_click(self.set_ready) self.sprite_grp.add(self.buttonReady) self._current_player.status = PlayerStatusEnum.NOT_READY event = ReadyEvent(self._current_player, False) if self._current_player.ip == GameStateModel.instance().host.ip: event.execute() Networking.get_instance().send_to_all_client(event) else: Networking.get_instance().send_to_server(event)
def _init_ip_addr(self): if Networking.get_instance().is_host: ip_addr = f"Your IP address: {Networking.get_instance().get_ip()}" label_width = 400 label_left = (pygame.display.get_surface().get_size()[0] / 2) - (label_width / 2) ip_addr_label = RectLabel(label_left, 20, label_width, 50, (255, 255, 255), txt_obj=(Text( pygame.font.SysFont('Agency FB', 26), ip_addr, Color.GREEN2))) ip_addr_label.change_bg_image(MEDIA_CONSTS.WOOD) ip_addr_label.add_frame(MEDIA_CONSTS.FRAME) #ip_addr_label.set_transparent_background(True) self.sprite_grp.add(ip_addr_label)
def update_profiles(self): if not os.path.exists(self.profiles): with open(self.profiles, mode="w+", encoding='utf-8') as myFile: myFile.write("[]") with open(self.profiles, mode='r', encoding='utf-8') as myFile: temp = json.load(myFile) for i, user in enumerate(temp): player: PlayerModel = JSONSerializer.deserialize(user) player.ip = Networking.get_instance().get_ip() player.set_pos(-1, -1) player.ap = 0 player.special_ap = 0 player.carrying_victim = NullModel() self.profile.set_profile( i, player.nickname, player.wins, player.losses, EventQueue.post, CustomEvent(ChangeSceneEnum.HOSTJOINSCENE, player=player) ) self.profile.remove_profile_callback(i, self.remove_profile, player.nickname)