More tweaks to networking code, starting network game seems to stop all gui events.
This commit is contained in:
parent
c7947d9790
commit
7abef7b37d
|
@ -80,7 +80,7 @@ class Goban:
|
||||||
|
|
||||||
rpos = self._real_pos(pos)
|
rpos = self._real_pos(pos)
|
||||||
|
|
||||||
if not self._valid_move(rpos):
|
if not self._valid_move(rpos, color):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.board[rpos] = color
|
self.board[rpos] = color
|
||||||
|
|
|
@ -7,33 +7,34 @@
|
||||||
# if it exists.
|
# if it exists.
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
|
import gtpsocket
|
||||||
|
|
||||||
class NetworkThread(threading.Thread):
|
class NetworkThread(threading.Thread):
|
||||||
dispatcher = {
|
def __init__(self, goban, socket):
|
||||||
'quit': do_quit,
|
threading.Thread.__init__(self)
|
||||||
'boardsize': do_boardsize,
|
|
||||||
'clear_board': do_clear_board,
|
self.dispatcher = {
|
||||||
'komi': do_komi,
|
'quit': self.do_quit,
|
||||||
'play': do_play,
|
'boardsize': self.do_boardsize,
|
||||||
'genmove': do_genmove
|
'clear_board': self.do_clear_board,
|
||||||
|
'komi': self.do_komi,
|
||||||
|
'play': self.do_play,
|
||||||
|
'genmove': self.do_genmove
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, goban, socket):
|
|
||||||
self.goban = goban
|
self.goban = goban
|
||||||
self.goban_lock = threading.Lock()
|
self.goban_lock = threading.Lock()
|
||||||
self.socket = GTPSocket(socket)
|
self.socket = gtpsocket.GTPSocket(socket)
|
||||||
self.send_lock = threading.Lock()
|
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):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
gtp = self.socket.get()
|
gtp = self.socket.get()
|
||||||
if gtp is not None:
|
if gtp is not None:
|
||||||
NetworkThread.dispatcher[gtp.command](gtp)
|
self.dispatcher[gtp.command](gtp)
|
||||||
|
|
||||||
|
|
||||||
def do_quit(self, gtp):
|
def do_quit(self, gtp):
|
||||||
|
|
|
@ -8,6 +8,10 @@ import sgc
|
||||||
from sgc.locals import *
|
from sgc.locals import *
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
|
import goban
|
||||||
|
import gtpsocket
|
||||||
|
import networkthread
|
||||||
|
|
||||||
|
|
||||||
class GUI:
|
class GUI:
|
||||||
def __init__(self, goban, settings):
|
def __init__(self, goban, settings):
|
||||||
|
@ -63,7 +67,8 @@ class GUI:
|
||||||
self.quit_btn.activate = sys.exit
|
self.quit_btn.activate = sys.exit
|
||||||
self.quit_btn.add()
|
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
|
self.wait_dialog.rect.center = self.screen.rect.center
|
||||||
|
|
||||||
# Generate a spurious event once a second, just to
|
# Generate a spurious event once a second, just to
|
||||||
|
@ -78,10 +83,11 @@ class GUI:
|
||||||
if event.type == QUIT:
|
if event.type == QUIT:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
if self.network_mode:
|
if self.network_mode:
|
||||||
# This set of events should only be called if we can currently play
|
# This set of events should only be called if we can currently play
|
||||||
if self.goban.to_move == self.our_color:
|
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
|
# Hover a transparent stone over our
|
||||||
# cursor position, assuming play is legal
|
# cursor position, assuming play is legal
|
||||||
if event.type == MOUSEMOTION:
|
if event.type == MOUSEMOTION:
|
||||||
|
@ -113,7 +119,7 @@ class GUI:
|
||||||
row = y / self.board_inc
|
row = y / self.board_inc
|
||||||
col = x / 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:
|
if event.button == 1:
|
||||||
self.goban.play_move((row, col))
|
self.goban.play_move((row, col))
|
||||||
|
|
||||||
|
@ -139,25 +145,40 @@ class GUI:
|
||||||
|
|
||||||
# fixme: this uses localhost as a stub
|
# fixme: this uses localhost as a stub
|
||||||
def join_game(self):
|
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
|
# fixme: this uses localhost as a stub
|
||||||
def wait_for_game(self):
|
def wait_for_game(self):
|
||||||
self.wait_dialog.add()
|
self.wait_dialog.add()
|
||||||
|
self.update()
|
||||||
|
|
||||||
|
try:
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
sock.bind(("127.0.0.1", 6859))
|
sock.bind(("127.0.0.1", 6859))
|
||||||
sock.listen(1)
|
sock.listen(1)
|
||||||
conn, addr = sock.accept()
|
conn, addr = sock.accept()
|
||||||
sock.close()
|
sock.close()
|
||||||
|
except socket.error as exception:
|
||||||
|
print 'Error: Socket creation failed: {}'.format(exception.args)
|
||||||
|
else:
|
||||||
self.socket = gtpsocket.GTPSocket(conn)
|
self.socket = gtpsocket.GTPSocket(conn)
|
||||||
self.net_thread = networkthread.NetworkThread(self.goban, conn)
|
self.net_thread = networkthread.NetworkThread(self.goban, conn)
|
||||||
self.net_thread.start()
|
self.net_thread.start()
|
||||||
self.network_mode = True
|
self.network_mode = True
|
||||||
|
self.our_color = goban.Goban.WHITE
|
||||||
|
finally:
|
||||||
self.wait_dialog.remove()
|
self.wait_dialog.remove()
|
||||||
|
self.update()
|
||||||
|
|
||||||
|
|
||||||
def do_hover(self, event):
|
def do_hover(self, event):
|
||||||
|
@ -166,10 +187,11 @@ class GUI:
|
||||||
col = x / self.board_inc
|
col = x / self.board_inc
|
||||||
|
|
||||||
if _magnitude(event.rel) < 3:
|
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))
|
self.goban.set_hover((row,col))
|
||||||
else:
|
else:
|
||||||
self.goban.clear_hover()
|
self.goban.clear_hover()
|
||||||
|
|
||||||
elif self.goban.hover != self.goban._real_pos((row,col)):
|
elif self.goban.hover != self.goban._real_pos((row,col)):
|
||||||
self.goban.clear_hover()
|
self.goban.clear_hover()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user