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 = {} 不然会将数据清空了,直接不写这个,默认也是会初始化好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值