Exemplo n.º 1
0
class Client(object):
    def _get_default_config(self):
        c = ConfigParser.SafeConfigParser({'server_host':'127.0.0.1', 'server_port':'4080'})
        return c

    def build_server(self):
        server = (self.config.get('DEFAULT', 'server_host'), self.config.getint('DEFAULT', 'server_port'))
        self.server = GameServer(connection=Connection(server))

    def __init__(self, conf=None):
        self.config = self._get_default_config()

        if conf:
            self.config.read(conf)

        self.build_server()

    def play_game(self):
        player_id, board = self.server.join_game()
        self.bot = MyBot(player_id=player_id, board=board)
        while (self.server.game_loop(self.bot)):
            pass

    def go(self):
        while True:
            self.play_game()
Exemplo n.º 2
0
    def on_connect(self, widget, host_entry, port_entry):
        server = (host_entry.get_text(), int(port_entry.get_text()))
        try:
            # XXX: Get I/O off the main thread somehow
            self.server = GameServer(connection=Connection(server))
        except Exception as e:
            dialog = Gtk.Dialog(
                title="Game Server Connection Error",
                flags=Gtk.DialogFlags.MODAL
                | Gtk.DialogFlags.DESTROY_WITH_PARENT,
                buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE),
            )
            ca = dialog.get_content_area()
            l = Gtk.Label(str(e))
            l.set_line_wrap_mode(True)
            ca.add(l)
            ca.show_all()
            dialog.run()
            dialog.destroy()

        Gtk.main_quit()
Exemplo n.º 3
0
    def on_connect(self, widget, host_entry, port_entry):
        server = (host_entry.get_text(), int(port_entry.get_text()))
        try:
            # XXX: Get I/O off the main thread somehow
            self.server = GameServer(connection=Connection(server))
        except Exception as e:
            dialog = Gtk.Dialog(
                    title="Game Server Connection Error",
                    flags=Gtk.DialogFlags.MODAL|Gtk.DialogFlags.DESTROY_WITH_PARENT,
                    buttons=(Gtk.STOCK_CLOSE,Gtk.ResponseType.CLOSE),
                    )
            ca = dialog.get_content_area()
            l = Gtk.Label(str(e))
            l.set_line_wrap_mode(True)
            ca.add(l)
            ca.show_all()
            dialog.run()
            dialog.destroy()

        Gtk.main_quit()
Exemplo n.º 4
0
 def build_server(self):
     server = (self.config.get('DEFAULT', 'server_host'), self.config.getint('DEFAULT', 'server_port'))
     self.server = GameServer(connection=Connection(server))
Exemplo n.º 5
0
class HumanClient(Client):
    def build_server(self):
        default_host = self.config.get('DEFAULT', 'server_host')
        default_port = self.config.get('DEFAULT', 'server_port')

        config_grid = Gtk.Grid()

        host_label = Gtk.Label('Host server')
        host_label.set_halign(Gtk.Align.END)
        config_grid.attach(host_label, 0,0,1,1)
        host_entry = Gtk.Entry()
        host_entry.set_text(default_host)
        config_grid.attach(host_entry, 1,0,1,1)

        port_label = Gtk.Label('Port')
        port_label.set_halign(Gtk.Align.END)
        config_grid.attach(port_label, 0,1,1,1)
        port_entry = Gtk.Entry()
        port_entry.set_text(default_port)
        config_grid.attach(port_entry, 1,1,1,1)

        config_grid.set_column_spacing(5)

        cancel_button = Gtk.Button(stock=Gtk.STOCK_CANCEL, use_stock=True)
        cancel_button.connect('clicked', self.build_server_destroy)

        connect_button = Gtk.Button(stock=Gtk.STOCK_CONNECT, use_stock=True)
        connect_button.connect('clicked', self.on_connect, host_entry, port_entry)

        button_line = Gtk.HBox()
        button_line.pack_start(cancel_button, True, False, 0)
        button_line.pack_end(connect_button, True, False, 0)

        vbox = Gtk.VBox()
        vbox.add(config_grid)
        vbox.add(button_line)

        server_window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        server_window.add(vbox)
        server_window.connect('destroy', self.build_server_destroy)
        server_window.connect('key-press-event', self.build_server_keypress)

        server_window.show_all()

        connect_button.grab_focus()

        self.server = None
        while self.server is None:
            Gtk.main()

        server_window.destroy()
        while Gtk.events_pending():
            Gtk.main_iteration()

    def __init__(self, *args, **kwds):
        self.move_queue = Queue()

        GObject.threads_init()
        Gdk.threads_init()
        Gtk.init(sys.argv)

        Gdk.threads_enter()
        super(HumanClient, self).__init__(*args, **kwds)
        Gdk.threads_leave()

    def on_connect(self, widget, host_entry, port_entry):
        server = (host_entry.get_text(), int(port_entry.get_text()))
        try:
            # XXX: Get I/O off the main thread somehow
            self.server = GameServer(connection=Connection(server))
        except Exception as e:
            dialog = Gtk.Dialog(
                    title="Game Server Connection Error",
                    flags=Gtk.DialogFlags.MODAL|Gtk.DialogFlags.DESTROY_WITH_PARENT,
                    buttons=(Gtk.STOCK_CLOSE,Gtk.ResponseType.CLOSE),
                    )
            ca = dialog.get_content_area()
            l = Gtk.Label(str(e))
            l.set_line_wrap_mode(True)
            ca.add(l)
            ca.show_all()
            dialog.run()
            dialog.destroy()

        Gtk.main_quit()

    def build_server_keypress(self, widget, event):
        if event.keyval == Gdk.KEY_Escape:
            self.build_server_destroy(widget)

    def build_server_destroy(self, widget, data=None):
        if self.server is None:
            sys.exit()

    def board_builder(self, **kwds):
        return HumanBoardGui(player_id=self.server.player_id, move_queue=self.move_queue, **kwds)

    class GameLoop(threading.Thread):
        def __init__(self, game_server, bot, end_game, **kwds):
            self.game_server = game_server
            self.bot = bot
            self.end_game = end_game
            super(HumanClient.GameLoop, self).__init__(**kwds)

        def run(self):
            while self.game_server.game_loop(self.bot):
                pass
            GObject.idle_add(self.end_game)

    def play_game(self):
        Gdk.threads_enter()
        self.player_id, self.board = self.server.join_game(board_constructor=self.board_builder)
        self.bot = HumanBot(player_id=self.player_id, board=self.board, move_queue=self.move_queue)
        Gdk.threads_leave()

        self.loop_thread = self.GameLoop(self.server, self.bot, self.end_game)
        self.loop_thread.daemon = True
        self.loop_thread.start()

        Gdk.threads_enter()
        Gtk.main()
        Gdk.threads_leave()

    def end_game(self):
        dialog = Gtk.Dialog(
                title="Game Over",
                flags=Gtk.DialogFlags.MODAL|Gtk.DialogFlags.DESTROY_WITH_PARENT,
                buttons=(Gtk.STOCK_CLOSE,Gtk.ResponseType.CLOSE),
                )
        ca = dialog.get_content_area()
        s = "The game is over.\n\nScores:\n"
        for p in xrange(self.board.player_count):
            s += "\tPlayer %d: %d\n" % (p, self.board.get_score(p))
        print s
        l = Gtk.Label(s)
        l.set_line_wrap_mode(True)
        ca.add(l)
        ca.show_all()
        dialog.run()
        dialog.destroy()
        self.board.window.destroy()
        Gtk.main_quit()
        while Gtk.events_pending():
            Gtk.main_iteration()
        sys.exit()
Exemplo n.º 6
0
class HumanClient(Client):
    def build_server(self):
        default_host = self.config.get('DEFAULT', 'server_host')
        default_port = self.config.get('DEFAULT', 'server_port')

        config_grid = Gtk.Grid()

        host_label = Gtk.Label('Host server')
        host_label.set_halign(Gtk.Align.END)
        config_grid.attach(host_label, 0, 0, 1, 1)
        host_entry = Gtk.Entry()
        host_entry.set_text(default_host)
        config_grid.attach(host_entry, 1, 0, 1, 1)

        port_label = Gtk.Label('Port')
        port_label.set_halign(Gtk.Align.END)
        config_grid.attach(port_label, 0, 1, 1, 1)
        port_entry = Gtk.Entry()
        port_entry.set_text(default_port)
        config_grid.attach(port_entry, 1, 1, 1, 1)

        config_grid.set_column_spacing(5)

        cancel_button = Gtk.Button(stock=Gtk.STOCK_CANCEL, use_stock=True)
        cancel_button.connect('clicked', self.build_server_destroy)

        connect_button = Gtk.Button(stock=Gtk.STOCK_CONNECT, use_stock=True)
        connect_button.connect('clicked', self.on_connect, host_entry,
                               port_entry)

        button_line = Gtk.HBox()
        button_line.pack_start(cancel_button, True, False, 0)
        button_line.pack_end(connect_button, True, False, 0)

        vbox = Gtk.VBox()
        vbox.add(config_grid)
        vbox.add(button_line)

        server_window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        server_window.add(vbox)
        server_window.connect('destroy', self.build_server_destroy)
        server_window.connect('key-press-event', self.build_server_keypress)

        server_window.show_all()

        connect_button.grab_focus()

        self.server = None
        while self.server is None:
            Gtk.main()

        server_window.destroy()
        while Gtk.events_pending():
            Gtk.main_iteration()

    def __init__(self, *args, **kwds):
        self.move_queue = Queue()

        GObject.threads_init()
        Gdk.threads_init()
        Gtk.init(sys.argv)

        Gdk.threads_enter()
        super(HumanClient, self).__init__(*args, **kwds)
        Gdk.threads_leave()

    def on_connect(self, widget, host_entry, port_entry):
        server = (host_entry.get_text(), int(port_entry.get_text()))
        try:
            # XXX: Get I/O off the main thread somehow
            self.server = GameServer(connection=Connection(server))
        except Exception as e:
            dialog = Gtk.Dialog(
                title="Game Server Connection Error",
                flags=Gtk.DialogFlags.MODAL
                | Gtk.DialogFlags.DESTROY_WITH_PARENT,
                buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE),
            )
            ca = dialog.get_content_area()
            l = Gtk.Label(str(e))
            l.set_line_wrap_mode(True)
            ca.add(l)
            ca.show_all()
            dialog.run()
            dialog.destroy()

        Gtk.main_quit()

    def build_server_keypress(self, widget, event):
        if event.keyval == Gdk.KEY_Escape:
            self.build_server_destroy(widget)

    def build_server_destroy(self, widget, data=None):
        if self.server is None:
            sys.exit()

    def board_builder(self, **kwds):
        return HumanBoardGui(player_id=self.server.player_id,
                             move_queue=self.move_queue,
                             **kwds)

    class GameLoop(threading.Thread):
        def __init__(self, game_server, bot, end_game, **kwds):
            self.game_server = game_server
            self.bot = bot
            self.end_game = end_game
            super(HumanClient.GameLoop, self).__init__(**kwds)

        def run(self):
            while self.game_server.game_loop(self.bot):
                pass
            GObject.idle_add(self.end_game)

    def play_game(self):
        Gdk.threads_enter()
        self.player_id, self.board = self.server.join_game(
            board_constructor=self.board_builder)
        self.bot = HumanBot(player_id=self.player_id,
                            board=self.board,
                            move_queue=self.move_queue)
        Gdk.threads_leave()

        self.loop_thread = self.GameLoop(self.server, self.bot, self.end_game)
        self.loop_thread.daemon = True
        self.loop_thread.start()

        Gdk.threads_enter()
        Gtk.main()
        Gdk.threads_leave()

    def end_game(self):
        dialog = Gtk.Dialog(
            title="Game Over",
            flags=Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
            buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE),
        )
        ca = dialog.get_content_area()
        s = "The game is over.\n\nScores:\n"
        for p in xrange(self.board.player_count):
            s += "\tPlayer %d: %d\n" % (p, self.board.get_score(p))
        print s
        l = Gtk.Label(s)
        l.set_line_wrap_mode(True)
        ca.add(l)
        ca.show_all()
        dialog.run()
        dialog.destroy()
        self.board.window.destroy()
        Gtk.main_quit()
        while Gtk.events_pending():
            Gtk.main_iteration()
        sys.exit()