More tweaks to networking code, starting network game seems to stop all gui events.
This commit is contained in:
parent
c7947d9790
commit
7abef7b37d
3 changed files with 55 additions and 32 deletions
|
@ -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()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue