diff --git a/lib/goban.py b/lib/goban.py index dc273cc..37303b9 100644 --- a/lib/goban.py +++ b/lib/goban.py @@ -80,7 +80,7 @@ class Goban: rpos = self._real_pos(pos) - if not self._valid_move(rpos): + if not self._valid_move(rpos, color): return self.board[rpos] = color diff --git a/lib/networkthread.py b/lib/networkthread.py index a868dd3..cb28512 100644 --- a/lib/networkthread.py +++ b/lib/networkthread.py @@ -7,33 +7,34 @@ # if it exists. import threading - +import gtpsocket class NetworkThread(threading.Thread): - dispatcher = { - 'quit': do_quit, - 'boardsize': do_boardsize, - 'clear_board': do_clear_board, - 'komi': do_komi, - 'play': do_play, - 'genmove': do_genmove - } - - def __init__(self, goban, socket): + threading.Thread.__init__(self) + + self.dispatcher = { + 'quit': self.do_quit, + 'boardsize': self.do_boardsize, + 'clear_board': self.do_clear_board, + 'komi': self.do_komi, + 'play': self.do_play, + 'genmove': self.do_genmove + } + self.goban = goban self.goban_lock = threading.Lock() - self.socket = GTPSocket(socket) + self.socket = gtpsocket.GTPSocket(socket) self.send_lock = threading.Lock() - GTPSocket.known_cmds = GTPSocket.known_cmds & set(NetworkThread.dispatcher.keys()) + gtpsocket.GTPSocket.known_cmds = gtpsocket.GTPSocket.known_cmds & set(self.dispatcher.keys()) def run(self): while True: gtp = self.socket.get() if gtp is not None: - NetworkThread.dispatcher[gtp.command](gtp) + self.dispatcher[gtp.command](gtp) def do_quit(self, gtp): diff --git a/lib/pygogui.py b/lib/pygogui.py index a0900ff..c2dd68e 100644 --- a/lib/pygogui.py +++ b/lib/pygogui.py @@ -8,6 +8,10 @@ import sgc from sgc.locals import * import socket +import goban +import gtpsocket +import networkthread + class GUI: def __init__(self, goban, settings): @@ -63,7 +67,8 @@ class GUI: self.quit_btn.activate = sys.exit self.quit_btn.add() - self.wait_dialog = sgc.widgets.Dialog(title="Please wait...", widget=sgc.widgets.Label(text='Waiting for a connection')) + wait_label = sgc.widgets.Label(text='Waiting for a connection') + self.wait_dialog = sgc.widgets.Dialog(title="Please wait...", widget=wait_label) self.wait_dialog.rect.center = self.screen.rect.center # Generate a spurious event once a second, just to @@ -78,10 +83,11 @@ class GUI: if event.type == QUIT: return - if self.network_mode: # This set of events should only be called if we can currently play if self.goban.to_move == self.our_color: + print 'Processing an event while it is our turn: {}'.format(event) + # Hover a transparent stone over our # cursor position, assuming play is legal if event.type == MOUSEMOTION: @@ -113,7 +119,7 @@ class GUI: row = y / self.board_inc col = x / self.board_inc - if x <= self.board_size: + if x < self.board_size and y < self.board_size: if event.button == 1: self.goban.play_move((row, col)) @@ -139,25 +145,40 @@ class GUI: # fixme: this uses localhost as a stub def join_game(self): - pass + try: + sock = socket.create_connection(("127.0.0.1", 6859)) + except socket.error as exception: + print 'Error: Socket creation failed: {}'.format(exception.args) + else: + self.socket = gtpsocket.GTPSocket(sock) + self.net_thread = networkthread.NetworkThread(self.goban, sock) + self.net_thread.start() + self.network_mode = True + self.our_color = goban.Goban.BLACK # fixme: this uses localhost as a stub def wait_for_game(self): self.wait_dialog.add() + self.update() - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.bind(("127.0.0.1", 6859)) - sock.listen(1) - conn, addr = sock.accept() - sock.close() - - self.socket = gtpsocket.GTPSocket(conn) - self.net_thread = networkthread.NetworkThread(self.goban, conn) - self.net_thread.start() - self.network_mode = True - - self.wait_dialog.remove() + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.bind(("127.0.0.1", 6859)) + sock.listen(1) + conn, addr = sock.accept() + sock.close() + except socket.error as exception: + print 'Error: Socket creation failed: {}'.format(exception.args) + else: + self.socket = gtpsocket.GTPSocket(conn) + self.net_thread = networkthread.NetworkThread(self.goban, conn) + self.net_thread.start() + self.network_mode = True + self.our_color = goban.Goban.WHITE + finally: + self.wait_dialog.remove() + self.update() def do_hover(self, event): @@ -166,10 +187,11 @@ class GUI: col = x / self.board_inc if _magnitude(event.rel) < 3: - if x <= self.board_size: + if x < self.board_size and y < self.board_size: self.goban.set_hover((row,col)) else: self.goban.clear_hover() + elif self.goban.hover != self.goban._real_pos((row,col)): self.goban.clear_hover()