Exemplo n.º 1
0
    def __init__(self,
                 port_min=3000,
                 port_max=3020,
                 max_thread=100,
                 list_update_function=lambda add_remove, host_port: None):
        """
        Constructor
        """
        super().__init__()

        self.setDaemon(True)

        self.server_detected_list = []
        self.stop_scan = False

        self.list_update_function = list_update_function

        self.port_min = port_min
        self.port_max = port_max

        socket.setdefaulttimeout(TIMEOUT)

        self.max_thread = max_thread
        self.thread_running = 0
        self.threads = Queue()

        self.list_in_queue = []

        self.disable_local_loop = Server.get_ip() != LOCAL_LOOP
Exemplo n.º 2
0
    def manual_connexion_button_command(self):
        """
        When the button manual connexion is clicked
        :return: None
        """
        try:
            host = socket.gethostbyname(
                self.manual_connexion_host_string_var.get())
        except socket.gaierror:
            tkinter.messagebox.showerror(
                self.ui.translation.get_translation(
                    "server_list_dialog_host_error_title"),
                self.ui.translation.get_translation(
                    "server_list_dialog_host_error_message").format(
                        Server.get_ip()))
            return None
        port = self.manual_connexion_port.get()

        try:
            port = int(port)
            if port < 1 or port > 65535:
                raise ValueError
        except ValueError:
            tkinter.messagebox.showerror(
                self.ui.translation.get_translation(
                    "server_configuration_dialog_port_error_title"),
                self.ui.translation.get_translation(
                    "server_configuration_dialog_port_error_message"))
            return None

        if not (GAME_PORT_MIN <= port <= GAME_PORT_MAX):
            if not tkinter.messagebox.askokcancel(
                    self.ui.translation.get_translation(
                        "server_configuration_dialog_port_warning_title"),
                    self.ui.translation.get_translation(
                        "server_configuration_dialog_port_warning_message")):
                return None

        self.ui.client = Client(host, port)
        if self.ui.client.connect():
            self.ui.change_panel(
                ServerGameConfigurationPanel.ServerGameConfigurationPanel,
                is_server=False)
        else:
            self.ui.client = None
            tkinter.messagebox.showerror(
                self.ui.translation.get_translation(
                    "server_list_dialog_unreachable_title"),
                self.ui.translation.get_translation(
                    "server_list_dialog_unreachable_message"))
            return
Exemplo n.º 3
0
    def manual_connexion_button_command(self):
        """
        When the button manual connexion is clicked
        :return: None
        """
        try:
            host = socket.gethostbyname(
                self.manual_connexion_host_string_var.get())
        except socket.gaierror:
            tkinter.messagebox.showerror(
                self.ui.translation.get_translation(
                    "server_list_dialog_host_error_title"),
                self.ui.translation.get_translation(
                    "server_list_dialog_host_error_message").format(
                        Server.get_ip()))
            return None
        port = self.manual_connexion_port.get()

        try:
            port = int(port)
        except ValueError:
            tkinter.messagebox.showerror(
                self.ui.translation.get_translation(
                    "server_configuration_dialog_port_error_title"),
                self.ui.translation.get_translation(
                    "server_configuration_dialog_port_error_message"))
            return None

        if not (3000 <= port <= 3020):
            tkinter.messagebox.showerror(
                self.ui.translation.get_translation(
                    "server_configuration_dialog_port_error_title"),
                self.ui.translation.get_translation(
                    "server_configuration_dialog_port_error_message"))
            return None

        if Server.exist(host, port):
            self.ui.client = Client(host, port)
            if self.ui.client.connect():
                self.ui.change_panel(
                    ServerGameConfigurationPanel.ServerGameConfigurationPanel,
                    create_game=False)
        else:
            tkinter.messagebox.showerror(
                self.ui.translation.get_translation(
                    "server_list_dialog_unreachable_title"),
                self.ui.translation.get_translation(
                    "server_list_dialog_unreachable_message"))
            return
Exemplo n.º 4
0
    def __init__(self, master, ui):
        """
        Constructor
        """
        super().__init__(master, ui)

        for r in range(1, 2):
            self.grid_rowconfigure(r, weight=1)

        for c in range(0, 2):
            self.grid_columnconfigure(c, weight=1)

        tkinter.tix.Label(self,
                          text=self.ui.translation.get_translation(
                              "server_list_server_detected")).grid(
                                  row=0, column=0, sticky=tkinter.tix.W)

        self.scrolled_window_server_detected = tkinter.tix.ScrolledWindow(self)
        self.scrolled_window_server_detected.grid(row=1,
                                                  column=0,
                                                  columnspan=2,
                                                  sticky=tkinter.tix.NSEW)

        self.window_server_detected = self.scrolled_window_server_detected.window

        self.window_server_detected.grid_columnconfigure(0, weight=1)

        self.dict_server_detected = {}

        self.manual_connexion_frame = tkinter.tix.Frame(self)
        self.manual_connexion_frame.grid(row=2,
                                         column=0,
                                         columnspan=2,
                                         sticky=tkinter.tix.NSEW)

        for c in range(0, 3):
            self.manual_connexion_frame.columnconfigure(c, weight=1)

        tkinter.tix.Label(self.manual_connexion_frame,
                          text=self.ui.translation.get_translation(
                              "server_list_manual_connexion_frame_text")).grid(
                                  row=0,
                                  column=0,
                                  rowspan=2,
                                  sticky=tkinter.tix.NSEW)

        tkinter.tix.Label(self.manual_connexion_frame,
                          text=self.ui.translation.get_translation(
                              "server_list_manual_connexion_frame_host")).grid(
                                  row=0, column=1, sticky=tkinter.tix.E)

        self.manual_connexion_host_string_var = tkinter.tix.StringVar()
        self.manual_connexion_host_string_var.set(Server.get_ip())
        self.manual_connexion_host_entry = tkinter.tix.Entry(
            self.manual_connexion_frame,
            textvariable=self.manual_connexion_host_string_var)
        self.manual_connexion_host_entry.grid(row=0,
                                              column=2,
                                              sticky=tkinter.tix.NSEW)

        tkinter.tix.Label(self.manual_connexion_frame,
                          text=self.ui.translation.get_translation(
                              "server_list_manual_connexion_frame_port")).grid(
                                  row=1, column=1, sticky=tkinter.tix.E)

        self.manual_connexion_port = tkinter.tix.Spinbox(
            self.manual_connexion_frame, from_=1, to=65535, increment=1)
        self.manual_connexion_port.delete(0, 1)
        self.manual_connexion_port.insert(0, GAME_PORT_MIN)
        self.manual_connexion_port.grid(row=1,
                                        column=2,
                                        sticky=tkinter.tix.NSEW)

        self.play_button = tkinter.tix.Button(
            self,
            text=self.ui.translation.get_translation("play"),
            state=tkinter.tix.DISABLED)
        self.play_button.grid(row=3, column=0, sticky=tkinter.tix.NSEW)

        self.manual_connexion_button = tkinter.tix.Button(
            self,
            text=self.ui.translation.get_translation(
                "server_list_manual_connexion"),
            command=self.manual_connexion_button_command)
        self.manual_connexion_button.grid(row=3,
                                          column=1,
                                          sticky=tkinter.tix.NSEW)

        self.back_button = tkinter.tix.Button(
            self,
            text=self.ui.translation.get_translation("back"),
            command=self.button_back_command)

        self.back_button.grid(row=4, column=0, sticky=tkinter.tix.NSEW)

        self.create_game_button = tkinter.tix.Button(
            self,
            text=self.ui.translation.get_translation(
                "server_list_create_game"),
            command=self.button_create_game_command)

        self.create_game_button.grid(row=4, column=1, sticky=tkinter.tix.NSEW)

        self.server_scanner = ServerScanner.ServerScanner(
            list_update_function=self.server_scanner_server_detection)
Exemplo n.º 5
0
    def __init__(self, master, ui, is_server=False):
        """
        Constructor
        """
        super().__init__(master, ui)
        self.is_server = is_server

        self.player_2_is_ready = False
        self.player_2_is_connected = False

        for r in range(2, 3):
            self.rowconfigure(r, weight=1)

        for c in range(0, 2):
            self.columnconfigure(c, weight=1)

        self.player = (TokenState.Player_2, TokenState.Player_1)[is_server]
        self.player_id = (1, 0)[is_server]
        self.opponent = (TokenState.Player_1, TokenState.Player_2)[is_server]
        self.opponent_id = (0, 1)[is_server]

        self.ui.image_getter.resize_tokens_images(100)

        # Configure server

        if is_server:

            self.server_configure_frame = tkinter.tix.Frame(
                self, relief=tkinter.tix.SUNKEN, borderwidth=2,
                padx=10, pady=10
            )
            self.server_configure_frame.grid(row=0, column=0, columnspan=2, sticky=tkinter.tix.NSEW, padx=10, pady=10)

            for c in range(0, 2):
                self.server_configure_frame.columnconfigure(c, weight=1)

            self.server_state_label = tkinter.tix.Label(
                self.server_configure_frame, text=self.ui.translation.get_translation("server_state_stopped"),
                fg="#ee2e31"
            )
            self.server_state_label.grid(row=0, column=0, columnspan=2, sticky=tkinter.tix.NSEW)

            self.server_host = tkinter.tix.Label(
                self.server_configure_frame,
                text=self.ui.translation.get_translation("server_configuration_host_label").format(
                    Server.get_ip())
            )

            self.server_host.grid(row=1, column=0, sticky=tkinter.tix.NSEW)

            self.server_port_frame = tkinter.tix.Frame(self.server_configure_frame)
            self.server_port_frame.grid(row=1, column=1, sticky=tkinter.tix.NSEW)

            for c in range(0, 2):
                self.server_port_frame.columnconfigure(c, weight=1)

            self.server_port_label = tkinter.tix.Label(
                self.server_port_frame,
                text=self.ui.translation.get_translation("server_configuration_port_entry_title")
            )
            self.server_port_label.grid(row=0, column=0, sticky=tkinter.tix.E)

            self.server_port_spin_box = tkinter.tix.Spinbox(self.server_port_frame, from_=1, to=65535, increment=1)
            self.server_port_spin_box.delete(0, 1)
            self.server_port_spin_box.insert(0, GAME_PORT_MIN)

            self.server_port_spin_box.grid(row=0, column=1, sticky=tkinter.tix.W)

            self.server_action_frame = tkinter.tix.Frame(self.server_configure_frame)
            self.server_action_frame.grid(row=2, column=0, columnspan=2, sticky=tkinter.tix.NSEW)

            for c in range(0, 3):
                self.server_action_frame.columnconfigure(c, weight=1)

            self.server_start_stop = tkinter.tix.Button(
                self.server_action_frame, text=self.ui.translation.get_translation("server_configuration_start"),
                command=self.server_start_stop_command
            )
            self.server_start_stop.grid(row=0, column=0, sticky=tkinter.tix.NSEW)

            if self.ui.server.server_is_on:
                self.server_port_spin_box.configure(state="readonly", increment=0)
                self.server_start_stop.configure(
                    text=self.ui.translation.get_translation("server_configuration_stop")
                )

                self.server_state_label.configure(
                    text=self.ui.translation.get_translation("server_state_started"), fg="#78bc61"
                )

        # Configure game

        self.players_name_frame = [tkinter.tix.Frame(self),
                                   tkinter.tix.Frame(self)]
        self.players_name_frame[0].grid(row=1, column=0)
        self.players_name_frame[1].grid(row=1, column=1)

        tkinter.tix.Label(self.players_name_frame[0], text=self.ui.translation.get_translation(
            "configure_game_panel_name")).grid(row=0, column=0)
        tkinter.tix.Label(self.players_name_frame[1], text=self.ui.translation.get_translation(
            "configure_game_panel_name")).grid(row=0, column=0)

        self.players_entry_string_variable = [
            tkinter.tix.StringVar(),
            tkinter.tix.StringVar()
        ]

        for i in range(0, 2):
            if i == self.opponent_id:
                self.players_entry_string_variable[i].set(
                    self.ui.translation.get_translation("server_configuration_waiting"))
            else:
                self.players_entry_string_variable[i].set(
                    self.ui.translation.get_translation("default_player_name").format(str(i + 1))
                )

        self.players_entry = [tkinter.tix.Entry(self.players_name_frame[0],
                                                textvariable=self.players_entry_string_variable[0],
                                                state=("readonly", "normal")[is_server]),
                              tkinter.tix.Entry(self.players_name_frame[1],
                                                textvariable=self.players_entry_string_variable[1],
                                                state=("normal", "readonly")[is_server])]

        if is_server:
            self.players_entry_string_variable[0].trace_add("write", lambda x, y, z: self.name_string_var_trace_write())
        else:
            self.players_entry_string_variable[1].trace_add("write", lambda x, y, z: self.name_string_var_trace_write())

        self.players_entry[0].grid(row=0, column=1)
        self.players_entry[1].grid(row=0, column=1)

        self.players_settings_scrolled_window = [tkinter.tix.ScrolledWindow(self, relief=tkinter.tix.SUNKEN,
                                                                            borderwidth=3),
                                                 tkinter.tix.ScrolledWindow(self, relief=tkinter.tix.SUNKEN,
                                                                            borderwidth=3)]
        self.players_settings_scrolled_window[0].grid(row=2, column=0, sticky=tkinter.tix.NSEW, pady=10, padx=5)
        self.players_settings_scrolled_window[1].grid(row=2, column=1, sticky=tkinter.tix.NSEW, pady=10, padx=5)

        self.players_settings_window = [self.players_settings_scrolled_window[0].window,
                                        self.players_settings_scrolled_window[1].window]

        for i in range(0, 2):
            self.players_settings_window[i].grid_columnconfigure(0, weight=1)

        self.players_tokens = [
            TokenStyle.TokenStyle(random.randint(0, TokenStyle.NUMBER_STYLE - 1)),
            TokenStyle.TokenStyle(random.randint(0, TokenStyle.NUMBER_STYLE - 1))
        ]
        self.players_tokens[self.opponent_id] = TokenStyle.TokenStyle.Not_Connected

        self.players_tokens_images = [
            self.ui.image_getter.save_token_photos[TokenState.Player_1][self.players_tokens[0]],
            self.ui.image_getter.save_token_photos[TokenState.Player_2][self.players_tokens[1]]
        ]

        self.players_tokens_labels = [
            tkinter.tix.Label(self.players_settings_window[0], image=self.players_tokens_images[0]),
            tkinter.tix.Label(self.players_settings_window[1], image=self.players_tokens_images[1])
        ]

        self.players_tokens_labels[0].grid(row=1, column=0, sticky=tkinter.tix.NSEW)
        self.players_tokens_labels[1].grid(row=1, column=0, sticky=tkinter.tix.NSEW)

        self.player_1_label = tkinter.tix.Label(
            self.players_settings_window[0], fg="white", text="", wraplength=250
        )
        self.player_1_label.grid(row=2, column=0)

        self.player_2_ready_label = tkinter.tix.Label(
            self.players_settings_window[1], fg="#ee2e31",
            text=self.ui.translation.get_translation(
                ("server_configuration_player_2_not_ready",
                 "server_configuration_player_2_not_connected")
                [self.is_server])
        )
        self.player_2_ready_label.grid(row=2, column=0)

        self.button_token_choose_per_line_last = 0
        self.button_token_choose_per_line = 0

        self.token_choose_frame = tkinter.tix.Frame(self.players_settings_window[self.player_id])

        self.token_choose_frame.grid(row=3, column=0, sticky=tkinter.tix.NSEW)

        self.token_choose_buttons = []

        for index in range(0, TokenStyle.NUMBER_STYLE):
            self.token_choose_buttons.append(tkinter.tix.Button(self.token_choose_frame,
                                                                image=self.ui.image_getter.save_token_icons[self.player]
                                                                [TokenStyle.TokenStyle(index)],
                                                                command=lambda _index=index:
                                                                self.button_change_token_command(
                                                                    _index)))

        self.button_main_menu = tkinter.tix.Button(
            self,
            text=self.ui.translation.get_translation("back"),
            command=self.button_main_menu_command)
        self.button_main_menu.grid(row=3, column=0, sticky=tkinter.tix.NSEW)

        self.button_play = tkinter.tix.Button(
            self,
            text=self.ui.translation.get_translation(("ready", "play")[is_server]),
            command=self.button_play_command)
        if not self.is_server:
            self.button_play.configure(fg="#ee2e31", activeforeground="#ee2e31")
        self.button_play.grid(row=3, column=1, sticky=tkinter.tix.NSEW)

        self.import_last_game_setting()

        if is_server:
            # 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:
            # 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

            self.client_on_connection_function()  # because it is already connected