def button_play_command(self): """ When the button play (or ready is press) :return: None """ if self.is_server: if self.player_2_is_connected: if self.player_2_is_ready: self.export_last_game_settings() self.ui.server.send_message_to_all(Server.encode_message(MESSAGE_PLAY)) self.ui.change_panel( ServerGamePanel.ServerGamePanel, player_1=Player.Player(TokenState.Player_1, self.players_tokens[0], self.players_entry_string_variable[0].get()), player_2=Player.Player(TokenState.Player_2, self.players_tokens[1], self.players_entry_string_variable[1].get()), is_server=self.is_server ) else: self.player_1_want_to_start() else: self.set_player_2_ready(not self.player_2_is_ready) self.ui.client.send_message(Server.encode_message(PARAMETER_SEPARATOR.join((MESSAGE_SEND_READY, str(self.player_2_is_ready)))))
def syn_client(self): """ Synchronise the client :return: None """ Server.log("Synchronise client", "GamePanel") if self.is_server: grid_state = '' for line in self.game.grid: for e in line: grid_state += str(e.value) grid_state += SUB_SEPARATOR self.ui.server.send_message_to_all(Server.encode_message(PARAMETER_SEPARATOR.join(( MESSAGE_SYN, SYN_GRID, str(self.game.current_turn.value), grid_state)))) if self.game.is_win(): self.ui.server.send_message_to_all(Server.encode_message(PARAMETER_SEPARATOR.join(( MESSAGE_SYN, SYN_WIN, str(self.game.winner.value), SUB_SEPARATOR.join( (str(self.game.score[0]), str(self.game.score[1]))), SUB_SEPARATOR.join(( ''.join(str(self.game.win_tokens_coord[0])), ''.join(str(self.game.win_tokens_coord[1])) )) )))) else: self.ui.client.send_message(Server.encode_message(MESSAGE_SYN))
def send_add_token(self, x, y): """ Send add token to the player :param x: The x coord of the new token :param y: The y coord of the new token :return: None """ if self.is_server: self.ui.server.send_message_to_all( Server.encode_message(PARAMETER_SEPARATOR.join((MESSAGE_NEW_TOKEN, str(x), str(y)))) ) else: self.ui.client.send_message(Server.encode_message( PARAMETER_SEPARATOR.join((MESSAGE_NEW_TOKEN, str(x), str(y)))) )
def client_send_player_name(self): """ Send the name of the client player :return: """ self.ui.client.send_message(Server.encode_message( PARAMETER_SEPARATOR.join((MESSAGE_SEND_NAME_SELECTED, self.players_entry_string_variable[self.player_id].get()))))
def client_on_connection_function(self): """ When the client is connect :return: None """ self.ui.client.send_message(Server.encode_message(MESSAGE_NEED_STATE)) self.client_send_player_name() self.client_send_token()
def server_send_token(self): """ Send the token of the client :return: None """ self.ui.server.send_message_to_all(Server.encode_message( PARAMETER_SEPARATOR.join((MESSAGE_SEND_TOKEN_SELECTED, str(self.players_tokens[self.player_id].value)))) )
def server_send_player_name(self): """ Send the name of the host player :return: None """ self.ui.server.send_message_to_all(Server.encode_message( PARAMETER_SEPARATOR.join((MESSAGE_SEND_NAME_SELECTED, self.players_entry_string_variable[self.player_id].get()))) )
def player_1_want_to_start(self): """ When the player click on the button play and the second player isn't ready :return: None """ self.player_1_label.configure( text=self.ui.translation.get_translation("server_configuration_player_1_want_start"), bg="#ee2e31" ) if self.is_server: self.ui.server.send_message_to_all(Server.encode_message(MESSAGE_PLAYER_1_WANT_START))
def button_restart_command(self): """ The command of the button restart :return: None """ if self.is_server: self.ui.server.send_message_to_all(Server.encode_message(MESSAGE_RESTART_GAME)) self.game.reset() self.ui.change_panel(ServerGamePanel, player_1=self.players[TokenState.TokenState.Player_1], player_2=self.players[TokenState.TokenState.Player_2], game=self.game, is_server=self.is_server)
def button_back_command(self): """ The command of the button back :return: None """ if self.is_server: from UI.ServerGameConfigurationPanel import ServerGameConfigurationPanel self.ui.change_panel(ServerGameConfigurationPanel, is_server=self.is_server) self.ui.server.send_message_to_all(Server.encode_message(MESSAGE_BACK_MENU)) else: if tkinter.messagebox.askquestion( self.ui.translation.get_translation("server_dialog_quit_title"), self.ui.translation.get_translation("server_dialog_quit_message") ) == tkinter.messagebox.YES: from UI.ServerListPanel import ServerListPanel self.ui.client.close_connection() self.ui.change_panel(ServerListPanel)
def __init__(self, master, ui, is_server=False, player_1=Player.Player(TokenState.TokenState.Player_1, TokenStyle.TokenStyle.Blue), player_2=Player.Player(TokenState.TokenState.Player_2, TokenStyle.TokenStyle.Green), game=None): """ Constructor """ super().__init__(master, ui, player_1, player_2, game, disable_end_button=not is_server) self.is_server = is_server self.button_syn = tkinter.tix.Button( self, image=self.ui.image_getter.syn_icon, command=self.button_syn_command) self.button_syn.place(relx=1., rely=0., x=0, y=0, anchor=tkinter.N + tkinter.E) self.button_syn_lock = False if self.is_server: self.after(100, lambda: self.ui.server.send_message_to_all(Server.encode_message( PARAMETER_SEPARATOR.join((MESSAGE_SET_TURN, str(self.game.current_turn.value)))))) self.button_main_menu.configure( text=self.ui.translation.get_translation("quit"), command=self.button_back_dialog_command ) # Set server functions self.ui.server.on_message_function = self.server_on_message self.ui.server.on_client_connect_function = self.server_on_client_connect_function self.ui.server.on_client_disconnect_function = self.server_on_client_disconnect_function else: tkinter.tix.Label( self.win_text_frame, text=self.ui.translation.get_translation("server_game_wait_host") ).grid(row=1, column=0) # Set client functions self.ui.client.on_message_function = self.client_on_message self.ui.client.on_connection_function = self.client_on_connection_function self.ui.client.on_disconnection_function = self.client_on_disconnection_function