Ejemplo n.º 1
0
class FGPClient(object):
  def __init__(self, gameclient):
    logging.debug("FGPClient")
    self.local_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    self.local_socket.bind( (host, port) )
    self.gameclient = gameclient
  
  def setServHost(self,serv_host):
    self.serv_host = serv_host
  
  def setServPort(self,serv_port):
    self.serv_port = serv_port

  def baseDecode(self,buf):
    logging.debug(buf_debug(buf))
    offset = 0
    self.msgtype = struct.unpack("H", buf[offset:offset + 2])[0]
    if self.msgtype == 3:
      logging.debug("FGPClient received UpdatePDU")
      self.updatepdu = UpdatePDU()
      self.updatepdu.decode(buf)
      self.gameclient.update(self.updatepdu)
    elif self.msgtype == 4:
      logging.debug("FGPClient received GameOverPDU")
      self.gameoverpdu = GameOverPDU()
      self.gameoverpdu.decode(buf)
      self.gameclient.gameover(self.gameoverpdu)
    elif self.msgtype == 5:
      logging.debug("FGPClient received DropPDU")
      self.droppdu = DropPDU()
      self.droppdu.decode(buf)
      self.gameclient.drop(self.droppdu)
    else:
      logging.debug("Dude, I did not expect that!")

  def send(self,msg):
    buf = msg.encode()
    logging.debug("FGPClient transmitting " + msg.name())
    self.local_socket.sendto(buf, (self.serv_host, self.serv_port))

  def listen(self):
    logging.debug("FGPClient listening...")
    buf, sender = self.local_socket.recvfrom(4096)
    self.setServHost(sender[0])
    self.setServPort(sender[1])
    self.baseDecode(buf)
Ejemplo n.º 2
0
 def baseDecode(self,buf):
   logging.debug(buf_debug(buf))
   offset = 0
   self.msgtype = struct.unpack("H", buf[offset:offset + 2])[0]
   if self.msgtype == 3:
     logging.debug("FGPClient received UpdatePDU")
     self.updatepdu = UpdatePDU()
     self.updatepdu.decode(buf)
     self.gameclient.update(self.updatepdu)
   elif self.msgtype == 4:
     logging.debug("FGPClient received GameOverPDU")
     self.gameoverpdu = GameOverPDU()
     self.gameoverpdu.decode(buf)
     self.gameclient.gameover(self.gameoverpdu)
   elif self.msgtype == 5:
     logging.debug("FGPClient received DropPDU")
     self.droppdu = DropPDU()
     self.droppdu.decode(buf)
     self.gameclient.drop(self.droppdu)
   else:
     logging.debug("Dude, I did not expect that!")
Ejemplo n.º 3
0
 def move(self, ctx):
   if ctx.isMoveLegal(ctx.clientMsg.getX(), ctx.clientMsg.getY()):
     logging.debug("P1 legal move at " + str(ctx.clientMsg.getX()) + ", " + str(ctx.clientMsg.getY()))
     ctx.getBoard().addPiece(Piece("white"), ctx.clientMsg.getX(), ctx.clientMsg.getY())
     # Count score
     score = ctx.countScore()
     minus = score.find("-")
     ws = score[0:minus]
     bs = score[minus+1:]
     if ctx.getBoard().isBoardFull():
       gameoverpdu = GameOverPDU()
       gameoverpdu.setX(ctx.clientMsg.getX())
       gameoverpdu.setY(ctx.clientMsg.getY())
       gameoverpdu.setWhiteScore(int(ws))
       gameoverpdu.setBlackScore(int(bs))
       gameoverpdu.setSendersays("Game Over!")
       #gameoverpdu.setSendersays("Score: " + score + ", Game Over!")
       ctx.getFGPSession().send(gameoverpdu, 0) # Broadcast
       ctx.getFGPSession().send(gameoverpdu, 1)
       # Kill Session
       ctx.stopListening()
     else:
       updatepdu = UpdatePDU()
       updatepdu.setX(ctx.clientMsg.getX())
       updatepdu.setY(ctx.clientMsg.getY())
       updatepdu.setWhiteScore(int(ws))
       updatepdu.setBlackScore(int(bs))
       updatepdu.setSendersays("It is black's turn!")
       #updatepdu.setSendersays("Score: " + score + ", Opponent played at (" + str(ctx.clientMsg.getX()) + ", " + str(ctx.clientMsg.getY()) + ")." + " Make a move!")
       ctx.getFGPSession().send(updatepdu, 0)
       ctx.getFGPSession().send(updatepdu, 1)
       ctx.setState(WF_P2_MOVE())
   else:
     logging.debug("P1 illegal move")
     # Count score
     score = ctx.countScore()
     minus = score.find("-")
     ws = score[0:minus]
     bs = score[minus+1:]
     updatepdu = UpdatePDU()
     updatepdu.setX(-1)
     updatepdu.setY(-1)
     updatepdu.setWhiteScore(int(ws))
     updatepdu.setBlackScore(int(bs))
     updatepdu.setSendersays("Dude, that was illegal! Make a new move!")
     #updatepdu.setSendersays("Score: " + score + ", Dude, that was illegal! Make a new move!")
     ctx.getFGPSession().send(updatepdu, 0) # Send it to player who made the illegal move.
     ctx.setState(WF_P1_MOVE())