def saveState(self): try: savelist = toGzJson(self.makeState()) self.gameState = savelist except zlib.error: Log.exception('zlib compress fail') return 0 except ValueError: Log.exception('encode fail') return 0 return len(savelist)
def saveState(self): try: savelist = toGzJson(self.makeState()) self.gameState = savelist except zlib.error: log.msg("zlib compress fail") return 0 except ValueError: log.msg("encode fail") return 0 return len(savelist)
def startFactory(self): self.FPSInit(getFrameTime, 60) self.recvcount, self.sendcount, self.initedTime = 0, 0, time.time() self.clientCount = 0 self.dispgroup = {} self.dispgroup["effectObjs"] = GameObjectGroup().initialize( gameObj=self, spriteClass=SpriteObj, teamcolor=(0x7F, 0x7F, 0x7F) ) self.dispgroup["objplayers"] = [] self.statObjN = Statistics() self.statCmpN = Statistics() self.statGState = Statistics() self.gameState = toGzJson(self.makeState()) task.LoopingCall(self.printStat).start(1.0) task.LoopingCall(self.FPSRun).start(1.0 / 60) log.msg("Factory inited")
def run(self): self.profile = ProfileMixin(g_profile) self.profile.begin() self.recvcount, self.sendcount = 0, 0 Log.critical('GameLogicServer initing pid:%s', self.pid) self.FPSInit(getFrameTime, 60) self.dispgroup = {} self.dispgroup['effectObjs'] = GameObjectGroup().initialize( gameObj=self, spriteClass=SpriteObj, teamcolor=(0x7f, 0x7f, 0x7f)) self.dispgroup['objplayers'] = [] self.statObjN = Statistics() self.statCmpN = Statistics() self.statGState = Statistics() self.clients = {} # clientid : team info self.gameState = toGzJson({ 'cmd': 'gameState', 'frameinfo': {k: v for k, v in self.frameinfo.iteritems() if k in ['thisFrameTime']}, 'objplayers': [og.serialize() for og in self.dispgroup['objplayers']], 'effectObjs': self.dispgroup['effectObjs'].serialize() }) self.quit = False self.registerRepeatFn(self.prfps, 1) self.r2c = {} self.w2c = {} for c in self.channels: self.r2c[c.reader] = c self.w2c[c.writer] = c Log.info('GameLogicServer inited') for ch in self.channels: ch.writeTo(((0, 0), None)) # run server # main loop self.recvlist = [self.toMainCh.reader] + [ c.reader for c in self.channels] self.sendlist = [] while not self.quit: self.sendlist = [o.writer for o in self.channels if o.canSend()] inputready, outputready, exceptready = select.select( self.recvlist, self.sendlist, [], 0) if len(inputready) == 0 and len(outputready) == 0: self.FPSRun() self.FPSYield() for i in inputready: if i == self.toMainCh.reader: self.quit = True break else: if i in self.r2c: clientid, packet = self.r2c[i].readFrom() cmdDict = fromGzJson(packet) self.do1ClientCmd(self.r2c[i], clientid, cmdDict) else: Log.critical('invalid reader %s', i) for o in outputready: if o in self.w2c: self.w2c[o].writeFromQueue() else: Log.critical('invalid writer %s', i) for ch in self.channels: ch.writeTo(((0, -1), None)) # quit server Log.info('end doGame') self.prfps(0) self.profile.end()