def createSingletonFromDB(entityName, globalname, dbid, props): def onCreateCallBack(baseRef, databaseID, wasActive): if baseRef: DEBUG_MSG( "createSingletonFromDB: %s create from DB success, databaseID:[%i]" % (entityName, databaseID)) baseRef.writeToDB() # 向全局共享数据中注册这个管理器的mailbox以便在所有逻辑进程中可以方便的访问 KBEngine.globalData[globalname] = baseRef else: WARNING_MSG("createSingletonFromDB: %s create from DB failed" % entityName) singleton = KBEngine.createBaseLocally(entityName, props) def onWriteToDB(success, entity): if success: DEBUG_MSG( "createSingletonFromDB: %s writeToDB success, dbid:[%i]" % (entityName, entity.databaseID)) entity.writeToDB( ) # TODO, to delete if auto-writeToDB each 15 min KBEngine.globalData[globalname] = entity else: ERROR_MSG("createSingletonFromDB: %s writeToDB failed" % entityName) singleton.writeToDB(onWriteToDB) KBEngine.createBaseFromDBID(entityName, dbid, onCreateCallBack)
def StartGameReq(self, characterDBID): ''' exposed. 客户端选择某个角色进行游戏 ''' INFO_MSG('Account::StarteGameReq:(%i) avatar state: %s, %i' % (self.id, avatar.cellData["name"], characterDBID)) #正在创建游戏角色中 if self.avatarState == public_config.CHARACTER_CREATING: return if characterDbid < 1: self.client.OnLoginResp(error_code.ERR_LOGIN_AVATAR_BAD) return if self.activeAvatar is None: if characterDBID in self.chatacters: KBEngine.createBaseFromDBID("Avatar", characterDBID, self.__onAvatarActivated) else: ERROR_MSG("Account[%i]::StartGameReq: not found dbid(%i)" % (self.id, characterDBID)) else: self.giveClientTo(self.activeAvatar) KBEngine.globalData["MapMgr"].SelectMapReq(self.activeAvatar, 10004, 0, characterDBID, self.name, {})
def loadClubs(self): club_list = list(self.clubList) if len(club_list) == 0: self.initFinish() return self.executor.set('c', 0) self.executor.set('c_ok', 0) self.executor.add_condition(lambda: self.executor.get('c_ok') >= 1, [self.initFinish, []]) self.executor.add_condition( lambda: self.executor.get('c') >= len(club_list), [self.executor.inc1, ['c_ok']]) for id in club_list: def create_cb(baseRef, databaseID, wasActive): self.executor.inc1('c') if baseRef: self.clubs[baseRef.clubId] = baseRef else: if databaseID in self.clubList: self.clubList.remove(databaseID) INFO_MSG("ClubStub load club failed. Club_%d not exist!" % databaseID) KBEngine.createBaseFromDBID("Club", id, create_cb)
def _dbCmdSelectCB(resultCollect, num, errorInfo): DEBUG_MSG(resultCollect) DEBUG_MSG(num) # DEBUG_MSG(errorInfo) if errorInfo is not None: DEBUG_MSG("error") KBEngine.executeRawDatabaseCommand( "CREATE TABLE mini_Spaces (name VARCHAR(255) NOT NULL DEFAULT '' primary key, dbid BIGINT)", _dbCmdCreateTblCB) else: DEBUG_MSG("not error") if not resultCollect: DEBUG_MSG("resultCollect == []") bigWorld = KBEngine.createBaseLocally("BigWorld", {}) if bigWorld: DEBUG_MSG("create bigWorld success") bigWorld.writeToDB(_bigWorldSavedCB) # KBEngine.executeRawDatabaseCommand("INSERT INTO mini_Spaces VALUES ('BigWorld', 0)", # _dbCmdInsertBigWorldCB) else: ERROR_MSG("create bigWorld failed") else: for value in resultCollect: if value[0] == b'BigWorld': DEBUG_MSG("==b") if value[1] != 0: KBEngine.createBaseFromDBID( "BigWorld", int(value[1].decode('ascii')), _onBigWorldCreateCB) DEBUG_MSG(int(value[1].decode('ascii'))) return
def loadWeeklyStubs(self): weekly_list = list(self.sportWeeklyList) if len(weekly_list) == 0: self.loadFinish() return self.executor.set('s_load', 0) self.executor.set('s_load_over', 0) self.executor.add_condition( lambda: self.executor.get('s_load_over') >= 1, [self.loadFinish, []]) self.executor.add_condition( lambda: self.executor.get('s_load') >= len(weekly_list), [self.executor.inc1, ["s_load_over"]]) for id in weekly_list: def createSportWeekly(baseRef, databaseID, wasActive): self.executor.inc1("s_load") if baseRef: self.sportWeeklyDict[baseRef.sportId] = baseRef else: if databaseID in self.sportWeeklyList: self.sportWeeklyList.remove(databaseID) KBEngine.createBaseFromDBID("SportWeekly", id, createSportWeekly)
def getOnlineFriends(self, mydbid, dbList, isOut, canRepeat, usePlayerDbidList, id): playerInfo = [] length = 0 for dbid in dbList: findItem = None if canRepeat == 0: for item in usePlayerDbidList: if item["itemID"] == id and item["number"] == dbid: findItem = item break if findItem != None: continue length += 1 if dbid in self.dbidToMailBox: player = self.dbidToMailBox[dbid] playerInfos = {} playerInfos["photoIndex"] = player.photoIndex playerInfos["dbid"] = dbid playerInfos["name"] = player.name playerInfos["guildName"] = player.guildName playerInfos["camp"] = player.camp playerInfos["level"] = player.level playerInfos["officialPosition"] = player.officialPosition playerInfo.append(playerInfos) if length == len(dbList): mb = self.dbidToMailBox[mydbid] mb.onPlayerMgrCmd("onGetPlayers", playerInfo) break else: if isOut == 1: def agreeCB(avatar, dbid, wasActive): if avatar != None: playerInfos = {} playerInfos["photoIndex"] = avatar.photoIndex playerInfos["dbid"] = dbid playerInfos["name"] = avatar.name playerInfos["guildName"] = avatar.guildName playerInfos["camp"] = avatar.camp playerInfos["level"] = avatar.level playerInfos[ "officialPosition"] = avatar.officialPosition playerInfo.append(playerInfos) if wasActive == 0: avatar.destroySelf() if length == len(dbList): mb = self.dbidToMailBox[mydbid] mb.onPlayerMgrCmd("onGetPlayers", playerInfo) KBEngine.createBaseFromDBID("Avatar", dbid, agreeCB)
def reqEnterGame(self, dbid): if self.activeRole is None: if dbid in self.characters: self.lastSelCharacter = dbid # 由于需要从数据库加载角色,因此是一个异步过程,加载成功或者失败会调用__onRoleCreated接口 # 当角色创建好之后,account会调用giveClientTo将客户端控制权(可理解为网络连接与某个实体的绑定)切换到Role身上, # 之后客户端各种输入输出都通过服务器上这个Role来代理,任何proxy实体获得控制权都会调用onEntitiesEnabled # Role继承了Teleport,Teleport.onEntitiesEnabled会将玩家创建在具体的场景中 KBEngine.createBaseFromDBID("Role", dbid, self.__onRoleCreated) else: ERROR_MSG("Account[%i]::selectRoleGame: not found dbid(%i)" % (self.id, dbid)) else: self.giveClientTo(self.activeRole)
def onGetCell(self): DEBUG_MSG("BigWorld:onGetCell") self.npcList = {} if self.xinShouYinDaoDBID == 0 or self.xinShouYinDaoDBID is None: self.npcList["新手引导"] = KBEngine.createBaseLocally("Npc", {}) if self.npcList["新手引导"]: self.npcList["新手引导"].cellData["entityName"] = "新手引导" self.npcList["新手引导"].cellData["modelName"] = "XinShouYinDaoNpc" self.npcList["新手引导"].cellData["position"] = (202.0, 0.0, 253.0) self.npcList["新手引导"].cellData["direction"] = (0.0, 0.0, 0.0) self.npcList["新手引导"].setAttr("spaceCell", self.cell) self.npcList["新手引导"].setAttr("entityName", "新手引导") self.npcList["新手引导"].writeToDB(self._onNpcSaved) else: KBEngine.createBaseFromDBID("Npc", self.xinShouYinDaoDBID, self.__onNpcCreateCB)
def reqItemList(self,data_version): """ exposed. 客户端请求查询道具列表 """ self.DATAVERSION = data_version gdata = KBEngine.createBaseFromDBID("Gdata",1,self.onGetData);
def onEntitiesEnabled(self): INFO_MSG("account[%i] entities enable. mailbox:%s" % (self.id, self.client)) if self.avatarDBID == 0: self.avatar = KBEngine.createBaseLocally("Avatar", {}) if self.avatar: self.avatar.accountEntity = self self.avatar.cellData["position"] = tuple( space_config_Table.datas[1]["spawnPos"]) self.avatar.cellData["direction"] = (0.0, 0.0, 0.0) self.avatar.cellData["entityName"] = self.__ACCOUNT_NAME__ self.avatar.writeToDB(self._onAvatarSaved) self.giveClientTo(self.avatar) else: KBEngine.createBaseFromDBID("Avatar", self.avatarDBID, self.__onAvatarCreateCB)
def onAllSpacesGetCell(self): DEBUG_MSG("SectsManager:onAllSpacesGetCell") for (sectID, sectData) in sect_config_Table.datas.items(): if sectID in self.sectDBIDList.keys(): KBEngine.createBaseFromDBID("Sect", self.sectDBIDList[sectID]["dbid"], self.__sectCreateCallback) else: sect = KBEngine.createBaseLocally( "Sect", { "entityName": sectData["sectName"], "sectID": sectID, "position": sectData["pos"] }) sect.writeToDB(self.__onSectSaved) self.sectList[sect.sectID] = sect
def selectAvatarGame(self, dbid): DEBUG_MSG("Account[%i].selectAvatarGame:%i. self.activeCharacter=%s self.client=%s" % (self.id, dbid, self.activeCharacter, self.client)) # 注意:使用giveClientTo的entity必须是当前baseapp上的entity if self.activeCharacter is None: player = KBEngine.createBaseFromDBID("Avatar", dbid, self.__onAvatarCreated) else: self.activeCharacter.accountEntity = self self.giveClientTo(self.activeCharacter)
def selectAvatarGame(self, dbid): """ exposed. 客户端选择某个角色进行游戏 """ DEBUG_MSG("Account[%i].selectAvatarGame:%i. self.activeAvatar=%s" % (self.id, dbid, self.activeAvatar)) # 注意:使用giveClientTo的entity必须是当前baseapp上的entity if self.activeAvatar is None: if dbid in self.characters: self.lastSelCharacter = dbid # 由于需要从数据库加载角色,因此是一个异步过程,加载成功或者失败会调用__onAvatarCreated接口 # 当角色创建好之后,account会调用giveClientTo将客户端控制权(可理解为网络连接与某个实体的绑定)切换到Avatar身上, # 之后客户端各种输入输出都通过服务器上这个Avatar来代理,任何proxy实体获得控制权都会调用onEntitiesEnabled # Avatar继承了Teleport,Teleport.onEntitiesEnabled会将玩家创建在具体的场景中 KBEngine.createBaseFromDBID("Avatar", dbid, self.__onAvatarCreated) else: ERROR_MSG("Account[%i]::selectAvatarGame: not found dbid(%i)" % (self.id, dbid)) else: self.giveClientTo(self.activeAvatar)
def selectAvatarGame(self, dbid): """ exposed. 客户端选择某个角色进行游戏 """ DEBUG_MSG("Account[%i].selectAvatarGame:%i. self.activeAvatar=%s" % (self.id, dbid, self.activeAvatar)) # 注意:使用giveClientTo的entity必须是当前baseapp上的entity if self.activeAvatar is None: if dbid in self.characters: #self.lastSelCharacter = dbid # 由于需要从数据库加载角色,因此是一个异步过程,加载成功或者失败会调用__onAvatarCreated接口 # 当角色创建好之后,account会调用giveClientTo将客户端控制权(可理解为网络连接与某个实体的绑定)切换到Avatar身上, # 之后客户端各种输入输出都通过服务器上这个Avatar来代理,任何proxy实体获得控制权都会调用onEntitiesEnabled # Avatar继承了Teleport,Teleport.onEntitiesEnabled会将玩家创建在具体的场景中 KBEngine.createBaseFromDBID("Avatar", dbid, self.__onAvatarCreated) else: ERROR_MSG("Account[%i]::selectAvatarGame: not found dbid(%i)" % (self.id, dbid)) else: self.giveClientTo(self.activeAvatar)
def onEntitiesEnabled(self): """ KBEngine method. 该entity被正式激活为可使用, 此时entity已经建立了client对应实体, 可以在此创建它的 cell部分。 """ DEBUG_MSG(self.avatarDBIDList) INFO_MSG("account[%i] entities enable. mailbox:%s" % (self.id, self.client)) if self.avatarDBID == 0: self.avatar = KBEngine.createBaseLocally("Avatar", {}) if self.avatar: self.avatar.accountEntity = self self.avatar.cellData["position"] = (120.4, 0.44, 90.6) self.avatar.cellData["direction"] = (0.0, 0.0, 0.0) self.avatar.cellData["entityName"] = self.__ACCOUNT_NAME__ self.avatar.writeToDB(self._onAvatarSaved) self.giveClientTo(self.avatar) else: KBEngine.createBaseFromDBID("Avatar", self.avatarDBID, self.__onAvatarCreateCB)
def __init__(self): # DEBUG_MSG("SpacesManager:__init__") KBEngine.globalData["SpacesManager"] = self KBEngine.globalData["allAvatarBases"] = {} # if not hasattr(self, "spaceDBIDList"): # self.spaceDBIDList = TIdDbidMapList() self.spaceList = {} self.spaceCreateCounter = 0 for spaceUID, spaceData in space_config_Table.datas.items(): if spaceUID in self.spaceDBIDList.keys(): KBEngine.createBaseFromDBID( "Space", self.spaceDBIDList[spaceUID]["dbid"], self.__onSpaceCreateCallback) else: space = KBEngine.createBaseLocally( "Space", { "spaceUID": spaceUID, "cityName": spaceData["cityName"], "spaceName": spaceData["spaceName"] }) space.writeToDB(self.__onSpaceSaved) self.spaceList[spaceUID] = space
def selectAvatarGame(self, dbid): """ exposed. 客户端选择某个角色进行游戏 """ DEBUG_MSG("Account[%i].selectAvatarGame:%i. self.activeCharacter=%s" % (self.id, dbid, self.activeCharacter)) # 注意:使用giveClientTo的entity必须是当前baseapp上的entity if self.activeCharacter is None: if dbid in self.characters: player = KBEngine.createBaseFromDBID("Avatar", dbid, self.__onAvatarCreated) else: ERROR_MSG("Account::selectAvatarGame: not found dbid(%i)" % dbid) else: self.giveClientTo(self.activeCharacter)
def selectAvatarEnterGame(self, avatarDBID): """ Define method. """ DEBUG_MSG("Account(id=%i dbid=%i)::selectAvatarEnterGame avatarDBID=%i" % (self.id, self.databaseID, avatarDBID)) KBEngine.createBaseFromDBID("Avatar", avatarDBID, self._onCreateAvatarFromDBID)
def onAutoLoadEntityCreate(entityType, dbid): DEBUG_MSG("kbemain:onAutoLoadEntityCreate entityType = " + str(entityType) + " dbid = " + str(dbid)) KBEngine.globalData[entityType] = True KBEngine.createBaseFromDBID(entityType, dbid)