home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import struct
-
- class GGZChessFeedback:
-
- def onSeat(self, seatNum, version):
- pass
-
-
- def onPlayers(self, whiteType, whiteName, blackType, blackName):
- pass
-
-
- def onClockRequest(self):
- pass
-
-
- def onClock(self, mode, seconds):
- pass
-
-
- def onStart(self):
- pass
-
-
- def onMove(self, move):
- pass
-
-
-
- class Chess:
- CLOCK_NONE = 0
- CLOCK_CLIENT = 1
- CLOCK_SERVERLAG = 2
- CLOCK_SERVER = 3
- GGZ_SEAT_NONE = '\x00'
- GGZ_SEAT_OPEN = '\x01'
- GGZ_SEAT_BOT = '\x02'
- GGZ_SEAT_PLAYER = '\x03'
- GGZ_SEAT_RESERVED = '\x04'
- GGZ_SEAT_ABANDONED = '\x05'
-
- def sendClock(self, mode, seconds):
- return '\x04' + struct.pack('!I', mode << 24 | seconds & 16777215)
-
-
- def sendMove(self, move, time = None):
- cmd = '\x06' + struct.pack('!I', len(move)) + move
- if time is not None:
- cmd += struct.pack('!I', time)
-
- return cmd
-
-
- def sendStart(self):
- return '\x05'
-
-
- def __init__(self, feedback):
- self.feedback = feedback
- self.decodeMethod = None
- self.decodeMethods = {
- '\x01': self.decodeSeat,
- '\x02': self.decodePlayers,
- '\x03': self.decodeClockRequest,
- '\x04': self.decodeClock,
- '\x05': self.decodeStart,
- '\x07': self.decodeMove,
- '\x08': self.decodeGameEnd,
- '\n': self.decodeUpdate,
- '\r': self.decodeDraw }
-
-
- def decode(self, char):
- if self.decodeMethod is None:
-
- try:
- self.decodeMethod = self.decodeMethods[char]
- except KeyError:
- self.decodeMethod = None
- print 'Unknown data received: %s' % repr(char)
- return None
-
- self.command = ''
-
- self.command += char
- self.decodeMethod()
-
-
- def getGGZString(self, buffer):
- if len(buffer) < 4:
- return (None, 0)
- (length,) = struct.unpack('!I', buffer[:4])
- if len(buffer) < length + 4:
- return (None, 0)
- string = buffer[4:length + 4]
- while string.endswith('\x00'):
- string = string[:-1]
- continue
- len(buffer) < length + 4
- return (string, length + 4)
-
-
- def decodeSeat(self):
- if len(self.command) == 3:
- self.decodeMethod = None
- (num, version) = struct.unpack('!xBB', self.command)
- self.feedback.onSeat(num, version)
-
-
-
- def decodePlayers(self):
- requiredLength = 2
- if len(self.command) < requiredLength:
- return None
- whiteCode = self.command[1]
- if len(self.command) < requiredLength:
- return None
- blackCode = self.command[requiredLength - 1]
- if len(self.command) >= requiredLength:
- self.decodeMethod = None
- self.feedback.onPlayers(whiteCode, whiteName, blackCode, blackName)
-
-
-
- def decodeClockRequest(self):
- self.decodeMethod = None
- self.feedback.onClockRequest()
-
-
- def decodeClock(self):
- if len(self.command) == 5:
- (value,) = struct.unpack('!xI', self.command)
- mode = value >> 24
- seconds = value & 16777215
- self.feedback.onClock(mode, seconds)
- self.decodeMethod = None
-
-
-
- def decodeStart(self):
- self.decodeMethod = None
- self.feedback.onStart()
-
-
- def decodeMove(self):
- (move, _) = self.getGGZString(self.command[1:])
- if move is None:
- return None
- self.decodeMethod = None
- self.feedback.onMove(move)
-
-
- def decodeGameEnd(self):
- if len(self.command) == 2:
- self.decodeMethod = None
-
-
-
- def decodeUpdate(self):
- if len(self.command) == 9:
- self.decodeMethod = None
-
-
-
- def decodeDraw(self):
- self.decodeMethod = None
-
-
-