-
-
-
-
-class NetworkSender(Player,Network):
- def __init__(self, base_player, address = None):
- self.base_player = base_player
- Player.__init__(self, base_player.name, base_player.colour)
-
- self.address = address
-
- def connect(self):
- nAttempts=3
- for i in range(nAttempts):
- try:
- Network.__init__(self, self.colour, self.address)
- debug(str(self) +" connected to " + str(self.address))
- return
- except Exception, e:
- debug(str(self) +" attempt " + str(i) + ": " + str(e.message))
-
- raise Exception("NETWORK - Can't connect to " + str(self.address))
-
-
- def select(self):
- [x,y] = self.base_player.select()
- choice = self.board.grid[x][y]
- s = str(x) + " " + str(y)
- #debug(str(self) + " sends: " + str(s))
- self.send_message(s)
- return [x,y]
-
- def get_move(self):
- [x,y] = self.base_player.get_move()
- s = str(x) + " " + str(y)
- #debug(str(self) + " sends: " + str(s))
- self.send_message(s)
- return [x,y]
-
- def update(self, s):
-
- self.base_player.update(s)
- if self.server == True:
- #debug(str(self) + " sends: " + str(s))
- self.send_message(s)
- return s
-
- s = s.split(" ")
- [x,y] = map(int, s[0:2])
- selected = self.board.grid[x][y]
- if selected != None and selected.colour == self.colour and len(s) > 2 and not "->" in s:
- s = " ".join(s[0:3])
- for i in range(2):
- if selected.types[i][0] != '?':
- s += " " + str(selected.types[i])
- else:
- s += " unknown"
- #debug(str(self) +" sending: " + str(s))
- self.send_message(s)
-
-
- def quit(self, final_result):
- self.base_player.quit(final_result)
- #self.src.send("QUIT " + str(final_result) + "\n")
- self.src.close()
-
- def __str__(self):
- s = "NetworkSender:"
- if self.server:
- s += "server"
- else:
- s += "client"
- s += ":"+str(self.address)
- return s
-
-
-class NetworkReceiver(Player,Network):
- def __init__(self, colour, address=None):
-
- s = "@network"
- if address != None:
- s += ":"+str(address)
- Player.__init__(self, s, colour)
-
- self.address = address
-
- self.board = None
-
-
- def connect(self):
- nAttempts=3
- for i in range(nAttempts):
- try:
- Network.__init__(self, self.colour, self.address)
- debug(str(self) +" connected to " + str(self.address))
- return
- except Exception, e:
- debug(str(self) +" attempt " + str(i) + ": " + str(e.message))
-
- raise Exception("NETWORK - Can't connect to " + str(self.address))