Initial work on implementing a network mode for communication with a server program.

This commit is contained in:
Anna Rose 2012-04-14 23:42:56 -04:00
parent 50c42e17ea
commit ac0b65ec62
2 changed files with 78 additions and 22 deletions

49
lib/gtpsocket.py Normal file
View File

@ -0,0 +1,49 @@
# A socket class for communicating in Go Text Protocol
#
# A simple connect method is defined, and everything else is
# suffixed with _gtp, so that this can be subclassed easily,
# e.g. for communicating with a server
import socket
class GTPSocket:
def __init__(self, info):
self.info = info
self.socket = None
def connect(self):
try:
self.socket = socket.create_connection(info)
return True
except:
return False
def get_gtp(self):
msg = None
while msg is None:
try:
msg = self.socket.recv(1024)
if not self._validate_gtp(msg):
print 'Error: Incoming data was not a valid GTP message'
msg = None
except:
pass
def send_gtp(self, msg):
try:
if _validate_gtp(msg):
self.socket.send(msg)
return True
else:
print 'Error: Outgoing data was not a valid GTP message'
return False
except:
return False
def _validate_gtp(self, gtp):
return True

11
pygo.py
View File

@ -86,11 +86,14 @@ def main():
# Build the dict of image objects
img_res = build_img_res()
# Data
gb = goban.Goban()
network_mode = False
our_color = None
board_size = 800
board_inc = board_size / 19
gb = goban.Goban()
screen.fill((250, 250, 250))
board = gb.draw_board(board_size, img_res)
screen.blit(board, (0,0))
@ -120,6 +123,9 @@ def main():
if event.type == QUIT:
return
# This set of events should only be called if we can currently play
if network_mode == False or gb.to_move == our_color:
# Hover a transparent stone over our
# cursor position, assuming play is legal
if event.type == MOUSEMOTION:
@ -145,6 +151,7 @@ def main():
if event.button == 1:
gb.play_move((row, col))
# if event.type == USEREVENT:
# gb.elapsed_time += 1