KBEngine引擎使用笔记

一、 查询数据库

def initEmailToAvatar(self, avatarId):

self.queryEmailsFromDataBase(avatarId, self.initEmailToAvatarCB)

def initEmailToAvatarCB(self, avatar, currentTime, emailDatas):
for email in emailDatas:
avatar.client.recvEmailData({"emailUID": int(email[0].decode()), "title": email[1].decode(), "content": email[2].decode(), 
"attachmentType": int(email[3].decode()), "attachmentId": int(email[4].decode()), "attachmentCount": int(email[5].decode()), 
"remainTime": int(email[6].decode()) - currentTime})

def queryEmailsFromDataBase(self, avatarId, callback):
avatar = KBEngine.entities[avatarId]
currentTime = int(time.time())
sql = "select emailUID, title, content, attachmentType, attachmentId, attachmentCount,"\
"expirationTime from tbl_Avatar_emails_emails where sm_receiveDBID = %i and receivedReward = 0 and sm_expirationTime > currentTime" % avatar.databaseID
def callback_inner(result, row, errstr):
if errstr:
ERROR_MSG("queryEmails::errror: %s" % errstr)
elif len(result) == 0:
ERROR_MSG("queryEmails::dbid %i is not has valid datas in tbl_EmailMgr table!" %  avatar.databaseID)
else:
callback(avatar, currentTime, result)

KBEngine.executeRawDatabaseCommand(sql, callback_inner)


二 特殊字典

# -*- coding: utf-8 -*-
#
from KBEDebug import *




class HeroDictConverter:
"""
"""
def getDictFromObj( self, obj ):
"""
The method converts a wrapper instance to a FIXED_DICT instance.


@param obj: The obj parameter is a wrapper instance.
@return: This method should return a dictionary(or dictionary-like object) that contains the same set of keys as a FIXED_DICT instance.
"""
DEBUG_MSG("HeroDictConverter::getDictFromObj:%s" % obj)


if obj is None:
return { "heros" : [] }


return {"heros": list(obj.values())}


def createObjFromDict( self, dictData ):
"""
This method converts a FIXED_DICT instance to a wrapper instance.


@param dictData: The dictData parameter is a FIXED_DICT instance.
@return: The method should return the wrapper instance constructed from the information in dict.
"""
DEBUG_MSG( "HeroDictConverter::createObjFromDict:%s" % dictData.items() )


obj = {}
if len(dictData) > 0:
for data in dictData["heros"]:
obj[data["dbid"]] = data


return obj


def isSameType( self, obj ):
"""
This method check whether an object is of the wrapper type.


@param obj: The obj parameter in an arbitrary Python object.
@return: This method should return true if obj is a wrapper instance.
"""
if obj is None: return True
return isinstance( obj, dict )




inst = HeroDictConverter()


三、 在配置文件中定于存储了,不能在init中使用 dicts = {} 不然会将数据清空了,直接不写这个,默认也是会初始化好

你可能感兴趣的:(KBEngine引擎使用笔记)