def _setUserDatasForce(userId, datas): dataKey = UserDataSchema.mkey(userId) params = UserDataSchema.paramsDict2List(datas, 0) ret = daobase.executeUserLua(userId, user_scripts.MAIN_SET_HASH_DATA_FORCE, 2, dataKey, json.dumps(params)) if _CACHE_USER_ENABLE: _cacheUser.update_group_dict_data(userId, dataKey, datas) return ret
def checkUserData(userId, clientId=None, appId=0): ''' 检查当前用户的数据是否是热数据(即存储在REDIS), 如果不是热数据, 那么重MYSQL中读取冷数据导入至热数据中 同时更新当前用户的数据生命时间 导入导出的用户数据包括user和个个游戏的所有数据 返回: 如果用户数据的最终状态为热数据,返回1 如果用户数据不存在,返回0 ''' ftlog.debug('MySqlSwap checkUserData->userId=', userId, 'clientId=', clientId, 'appId=', appId) userId = int(userId) ctfull = timestamp.formatTimeMs() isok = daobase.executeUserLua(userId, dataswap_scripts.CHECK_USER_DATA_LUA_SCRIPT, 2, userId, ctfull) ftlog.debug('MySqlSwap checkUserData->userId=', userId, 'clientId=', clientId, 'appId=', appId, 'isok=', isok) if isok == 1: return 1 if clientId: intClientId = pokerconf.clientIdToNumber(clientId) else: intClientId = configure.DEFAULT_CLIENT_ID appId = int(appId) return _tryReadDataFromMySql(userId, intClientId, appId, ctfull)
def _tryReadDataFromMySql(userId, intClientId, appId, ctfull): # 得到MySQL中的数据 csize = getMysqlSize() dbname = 'user' + str(userId % csize) tablename = 't' + str(userId / csize % 200) sqlstr = 'select data from %s where userid=%d limit 1' % (tablename, userId) ftlog.info('_tryReadDataFromMySql', userId, intClientId, appId, dbname, sqlstr) jsonstr = dbmysql._queryMysql00(userId, dbname, sqlstr) ftlog.info('_tryReadDataFromMySql before', userId, jsonstr) if not jsonstr: ftlog.info('_tryReadDataFromMySql', userId, 'the user mysql data not found !') return 0 loaddatas = strutil.loads(jsonstr) # 拆解执行数据装载,避免redis的slowlog, 避免挤压redis isok, chip, diamond, coin, coupon = 1, 0, 0, 0, 0 rkeys = loaddatas.keys() while (len(rkeys)) > 0: subrkeys = rkeys[0:4] rkeys = rkeys[4:] subdata = {} for subkey in subrkeys: subdata[subkey] = loaddatas[subkey] jsonstr1 = strutil.dumps(subdata) isok1, chip1, diamond1, coin1, coupon1 = daobase.executeUserLua( userId, dataswap_scripts.DATA_SWAP_LUA_SCRIPT, 3, userId, ctfull, jsonstr1) ftlog.debug('_tryReadDataFromMySql save to redis->', userId, isok, jsonstr1) isok = min(isok, isok1) chip = max(chip, chip1) diamond = max(diamond, diamond1) coin = max(coin, coin1) coupon = max(coupon, coupon1) ftlog.info('_tryReadDataFromMySql save to redis->', userId, 'isok=', isok, 'chip=', chip, 'diamond=', diamond, 'coin=', coin, 'coupon=', coupon) chip, diamond, coin, coupon = strutil.parseInts(chip, diamond, coin, coupon) if isok == 1: from poker.entity.biz import bireport bireport.reportBiChip(userId, chip, chip, chip, 'DATA_FROM_MYSQL_2_REDIS_CHIP', intClientId, appId, appId, 0, daoconst.CHIP_TYPE_CHIP) bireport.reportBiChip(userId, coin, coin, coin, 'DATA_FROM_MYSQL_2_REDIS_COIN', intClientId, appId, appId, 0, daoconst.CHIP_TYPE_COIN) bireport.reportBiChip(userId, diamond, diamond, diamond, 'DATA_FROM_MYSQL_2_REDIS_DIAMOND', intClientId, appId, appId, 0, daoconst.CHIP_TYPE_DIAMOND) bireport.reportBiChip(userId, coupon, coupon, coupon, 'DATA_FROM_MYSQL_2_REDIS_COUPON', intClientId, appId, appId, 0, daoconst.CHIP_TYPE_COUPON) return 1 return 0
def _tryReadDataFromMySql(userId, intClientId, appId, ctfull): # 得到MySQL中的数据 csize = getMysqlSize() dbname = 'user' + str(userId % csize) tablename = 't' + str(userId / csize % 200) sqlstr = 'select data from %s where userid=%d limit 1' % (tablename, userId) ftlog.info('_tryReadDataFromMySql', userId, intClientId, appId, dbname, sqlstr) jsonstr = dbmysql._queryMysql00(userId, dbname, sqlstr) ftlog.info('_tryReadDataFromMySql before', userId, jsonstr) if not jsonstr: ftlog.info('_tryReadDataFromMySql', userId, 'the user mysql data not found !') return 0 loaddatas = strutil.loads(jsonstr) # 拆解执行数据装载,避免redis的slowlog, 避免挤压redis isok, chip, diamond, coin, coupon = 1, 0, 0, 0, 0 rkeys = loaddatas.keys() while (len(rkeys)) > 0: subrkeys = rkeys[0:4] rkeys = rkeys[4:] subdata = {} for subkey in subrkeys: subdata[subkey] = loaddatas[subkey] jsonstr1 = strutil.dumps(subdata) isok1, chip1, diamond1, coin1, coupon1 = daobase.executeUserLua(userId, dataswap_scripts.DATA_SWAP_LUA_SCRIPT, 3, userId, ctfull, jsonstr1) ftlog.debug('_tryReadDataFromMySql save to redis->', userId, isok, jsonstr1) isok = min(isok, isok1) chip = max(chip, chip1) diamond = max(diamond, diamond1) coin = max(coin, coin1) coupon = max(coupon, coupon1) ftlog.info('_tryReadDataFromMySql save to redis->', userId, 'isok=', isok, 'chip=', chip, 'diamond=', diamond, 'coin=', coin, 'coupon=', coupon) chip, diamond, coin, coupon = strutil.parseInts(chip, diamond, coin, coupon) if isok == 1: from poker.entity.biz import bireport bireport.reportBiChip(userId, chip, chip, chip, 'DATA_FROM_MYSQL_2_REDIS_CHIP', intClientId, appId, appId, 0, daoconst.CHIP_TYPE_CHIP) bireport.reportBiChip(userId, coin, coin, coin, 'DATA_FROM_MYSQL_2_REDIS_COIN', intClientId, appId, appId, 0, daoconst.CHIP_TYPE_COIN) bireport.reportBiChip(userId, diamond, diamond, diamond, 'DATA_FROM_MYSQL_2_REDIS_DIAMOND', intClientId, appId, appId, 0, daoconst.CHIP_TYPE_DIAMOND) bireport.reportBiChip(userId, coupon, coupon, coupon, 'DATA_FROM_MYSQL_2_REDIS_COUPON', intClientId, appId, appId, 0, daoconst.CHIP_TYPE_COUPON) return 1 return 0
def addPresentNum(self, gameId, userId, clientId, presentNum): activityId = self._clientConf.get("id") if not activityId: if ftlog.is_debug(): ftlog.debug("TYActivityPlayGamePresentGift.addPresentNum not find adtivityId") return if not self.checkOperative(): if ftlog.is_debug(): ftlog.info("TYActivityPlayGamePresentGift.addPersentNum activity expired:", activityId) return # 有效时间 if "dayTimeLimit" in self._serverConf: if not self._checkDayTime(): ftlog.debug("TYActivityPlayGamePresentGift.addPersentNum, dayTimeLimit not match") return # 是否可连续,1表示1天内连续、0表示可连续 accumulateType = self._serverConf["accumulateType"] # 需要领取奖励的局数 rewardNeedNum = self._serverConf["rewardNeedNum"] accumulateKey = "accumulate" accumulateValue = 1 hasRewardedKey = "hasRewarded" if accumulateType == 1: accumulateKey = "%s:%s" % (accumulateKey, time.strftime('%Y-%m-%d', time.localtime(time.time()))) hasRewardedKey = "%s:%s" % (hasRewardedKey, time.strftime('%Y-%m-%d', time.localtime(time.time()))) mustContinuous = 0 maxInterval = 0 accumulateIdentiferValue = "noidentifer" nowTimeStamp = int(time.time()) if 'accumulateMustContinuous' in self._serverConf: if self._serverConf["accumulateMustContinuous"] == 1: mustContinuous = 1 maxInterval = self._serverConf.get("accumulateMaxInterval", 300) userActivityKey = "TYActivity:%d:%d:%s" % (gameId, userId, activityId) if ftlog.is_debug(): ftlog.debug("TYActivityPlayGamePresentGift.addPresentNum:", presentNum, accumulateKey, accumulateValue, accumulateType, accumulateIdentiferValue, mustContinuous, maxInterval, userActivityKey, rewardNeedNum, nowTimeStamp, hasRewardedKey) ret = daobase.executeUserLua(userId, self.HANDLE_EVENT_SCRIPT, 11, presentNum, accumulateKey, accumulateValue, accumulateType, accumulateIdentiferValue, mustContinuous, maxInterval, userActivityKey, rewardNeedNum, nowTimeStamp, hasRewardedKey) if ftlog.is_debug(): ftlog.debug("TYActivityPlayGamePresentGift.addPresentNum executeUserLua ret:", ret)
def addPresentNum(self, gameId, userId, clientId, presentNum): activityId = self._clientConf.get("id") if not activityId: if ftlog.is_debug(): ftlog.debug("TYActivityPlayGamePresentGift.addPresentNum not find adtivityId") return if not self.checkOperative(): if ftlog.is_debug(): ftlog.info("TYActivityPlayGamePresentGift.addPersentNum activity expired:", activityId) return # 有效时间 if "dayTimeLimit" in self._serverConf: if not self._checkDayTime(): ftlog.debug("TYActivityPlayGamePresentGift.addPersentNum, dayTimeLimit not match") return # 是否可连续,1表示1天内连续、0表示可连续 accumulateType = self._serverConf["accumulateType"] # 需要领取奖励的局数 rewardNeedNum = self._serverConf["rewardNeedNum"] accumulateKey = "accumulate" accumulateValue = 1 hasRewardedKey = "hasRewarded" if accumulateType == 1: accumulateKey = "%s:%s" % (accumulateKey, time.strftime('%Y-%m-%d', time.localtime(time.time()))) hasRewardedKey = "%s:%s" % (hasRewardedKey, time.strftime('%Y-%m-%d', time.localtime(time.time()))) mustContinuous = 0 maxInterval = 0 accumulateIdentiferValue = "noidentifer" nowTimeStamp = int(time.time()) if 'accumulateMustContinuous' in self._serverConf: if self._serverConf["accumulateMustContinuous"] == 1: mustContinuous = 1 maxInterval = self._serverConf.get("accumulateMaxInterval", 300) userActivityKey = "TYActivity:%d:%d:%s" % (gameId, userId, activityId) if ftlog.is_debug(): ftlog.debug("TYActivityPlayGamePresentGift.addPresentNum:", presentNum, accumulateKey, accumulateValue, accumulateType, accumulateIdentiferValue, mustContinuous, maxInterval, userActivityKey, rewardNeedNum, nowTimeStamp, hasRewardedKey) ret = daobase.executeUserLua(userId, self.HANDLE_EVENT_SCRIPT, 11, presentNum, accumulateKey, accumulateValue, accumulateType, accumulateIdentiferValue, mustContinuous, maxInterval, userActivityKey, rewardNeedNum, nowTimeStamp, hasRewardedKey) if ftlog.is_debug(): ftlog.debug("TYActivityPlayGamePresentGift.addPresentNum executeUserLua ret:", ret)
def incrGameAttrLimit(uid, gameid, attrname, deltaCount, lowLimit, highLimit, chipNotEnoughOpMode): ''' INCR用户游戏属性 执行lua命令 参考: incr_chip_limit ''' assert(isinstance(gameid, int) and gameid > 0), 'gameid must be int' assert(attrname not in daoconst.FILTER_MUST_FUNC_FIELDS) trueDetal, finalCount, fixCount = daobase.executeUserLua(uid, user_scripts.MAIN_INCR_CHIP_LUA_SCRIPT, 6, deltaCount, lowLimit, highLimit, chipNotEnoughOpMode, daoconst.HKEY_GAMEDATA + str(gameid) + ':' + str(uid), attrname) return trueDetal, finalCount, fixCount
def incrGameAttrLimit(uid, gameid, attrname, deltaCount, lowLimit, highLimit, chipNotEnoughOpMode): ''' INCR用户游戏属性 参考: incr_chip_limit ''' assert (isinstance(gameid, int) and gameid > 0), 'gameid must be int' assert (attrname not in daoconst.FILTER_MUST_FUNC_FIELDS) trueDetal, finalCount, fixCount = daobase.executeUserLua(uid, user_scripts.MAIN_INCR_CHIP_LUA_SCRIPT, 6, deltaCount, lowLimit, highLimit, chipNotEnoughOpMode, daoconst.HKEY_GAMEDATA + str(gameid) + ':' + str(uid), attrname) return trueDetal, finalCount, fixCount
def _setTableChipToRange(userId, gameid, _min, _max, eventId, intEventParam, clientId, tableId, rhashkey, rfield): from poker.entity.dao import sessiondata _, numberClientId = sessiondata.getClientIdNum(userId, None) appId = sessiondata.getGameId(userId) tdelta, tfinal, tfixed, delta, final, fixed = daobase.executeUserLua( userId, user_scripts.MAIN_MOVE_CHIP_TO_TABLE_LUA_SCRIPT, 6, userId, gameid, _min, _max, rhashkey, rfield) # TODO 替换GAME DATA中的金币数据值 ftlog.debug('dbuser->_setTableChipToRange', userId, gameid, _min, _max, eventId, intEventParam, clientId, tableId, rhashkey, 'result->', tdelta, tfinal, tfixed, delta, final, fixed) return tdelta, tfinal, tfixed, delta, final, fixed, appId, numberClientId
def _incrUserDatasLimit(userId, field, value, lowLimit, highLimit, chipNotEnoughOpMode, dataKey=None): from poker.entity.dao import sessiondata _, numberClientId = sessiondata.getClientIdNum(userId, None) appId = sessiondata.getGameId(userId) if dataKey == None: dataKey = UserDataSchema.mkey(userId) trueDetal, finalCount, fixCount = daobase.executeUserLua(userId, user_scripts.MAIN_INCR_CHIP_LUA_SCRIPT, 6, value, lowLimit, highLimit, chipNotEnoughOpMode, dataKey, field) if dataKey == UserDataSchema.mkey(userId): if _CACHE_USER_ENABLE: _cacheUser.replace_group_dict_data(userId, dataKey, field, finalCount) return trueDetal, finalCount, fixCount, appId, numberClientId
def _setTableChipToRange(userId, gameid, _min, _max, eventId, intEventParam, clientId, tableId, rhashkey, rfield): from poker.entity.dao import sessiondata _, numberClientId = sessiondata.getClientIdNum(userId, None) appId = sessiondata.getGameId(userId) tdelta, tfinal, tfixed, delta, final, fixed = daobase.executeUserLua(userId, user_scripts.MAIN_MOVE_CHIP_TO_TABLE_LUA_SCRIPT, 6, userId, gameid, _min, _max, rhashkey, rfield) if _CACHE_USER_ENABLE: _cacheUser.replace_group_dict_data(userId, UserDataSchema.mkey(userId), UserDataSchema.CHIP, final) # TODO 替换GAME DATA中的金币数据值 ftlog.debug('dbuser->_setTableChipToRange', userId, gameid, _min, _max, eventId, intEventParam, clientId, tableId, rhashkey, 'result->', tdelta, tfinal, tfixed, delta, final, fixed) return tdelta, tfinal, tfixed, delta, final, fixed, appId, numberClientId
def checkUserData(userId, clientId=None, appId=0): ''' 检查当前用户的数据是否是热数据(即存储在REDIS), 如果不是热数据, 那么重MYSQL中读取冷数据导入至热数据中 同时更新当前用户的数据生命时间 导入导出的用户数据包括user和个个游戏的所有数据 返回: 如果用户数据的最终状态为热数据,返回1 如果用户数据不存在,返回0 ''' ftlog.debug('MySqlSwap checkUserData->userId=', userId, 'clientId=', clientId, 'appId=', appId) userId = int(userId) ctfull = timestamp.formatTimeMs() isok = daobase.executeUserLua(userId, dataswap_scripts.CHECK_USER_DATA_LUA_SCRIPT, 2, userId, ctfull) ftlog.debug('MySqlSwap checkUserData->userId=', userId, 'clientId=', clientId, 'appId=', appId, 'isok=', isok) if isok == 1: return 1 if clientId: intClientId = pokerconf.clientIdToNumber(clientId) else: intClientId = configure.DEFAULT_CLIENT_ID appId = int(appId) return _tryReadDataFromMySql(userId, intClientId, appId, ctfull)
def handleEvent(self, event): ''' 当收到每局结束发送的event时,在activity.py中调用该方法 处理前台活动返回的配置包括客户端配置和服务器配置 ''' ftlog.debug("TYActivityPlayGamePresentGift handleEvent123:", event.role, event.roomLevel, event.roundNum, event.gameId, event.userId, event.clientId) gameId = event.gameId userId = event.userId activityId = self._clientConf.get("id") if not activityId: ftlog.debug("TYActivityPlayGamePresentGift handleEvent not find adtivityId") return if not self.checkOperative(): ftlog.debug("activity expired:", activityId) return # 对_config开始解析配置,conditions玩的局数,默认为1 for cond in self._serverConf["conditions"]: if cond == self._serverConf['accumulateField']: continue condValue = event.get(cond) if isinstance(self._serverConf[cond], list): if condValue not in self._serverConf[cond]: ftlog.debug("TYActivityPlayGamePresentGift handleEvent, condValue not match,cond:", cond) return else: if condValue != self._serverConf[cond]: ftlog.debug("TYActivityPlayGamePresentGift handleEvent, condValue not match,cond:", cond) return # 有效时间 if "dayTimeLimit" in self._serverConf: if not self._checkDayTime(): ftlog.debug("TYActivityPlayGamePresentGift handleEvent, dayTimeLimit not match") return # 所在房间等级 if "presentBased" in self._serverConf: presentBasedValue = event.get(self._serverConf["presentBased"]) presentBasedValue = gdata.getBigRoomId(presentBasedValue) ftlog.debug("TYActivityPlayGamePresentGift handleEvent11:", presentBasedValue) if not presentBasedValue: ftlog.debug("TYActivityPlayGamePresentGift handleEvent, presentBasedvalue not find, presentBased:", self._serverConf["presentBased"]) return else: if (not isinstance(presentBasedValue, int)) or str(presentBasedValue) not in self._serverConf[ "presentConfig"]: ftlog.debug("TYActivityPlayGamePresentGift handleEvent, presentBasedValue not match:", presentBasedValue) return presentNum = self._serverConf["presentConfig"][str(presentBasedValue)] else: presentNum = self._serverConf["presentNum"] # 是否可连续,1表示1天内连续、0表示可连续 accumulateType = self._serverConf["accumulateType"] # 需要领取奖励的局数 rewardNeedNum = self._serverConf["rewardNeedNum"] accumulateKey = "accumulate" hasRewardedKey = "hasRewarded" if accumulateType == 1: accumulateKey = "%s:%s" % (accumulateKey, time.strftime('%Y-%m-%d', time.localtime(time.time()))) hasRewardedKey = "%s:%s" % (hasRewardedKey, time.strftime('%Y-%m-%d', time.localtime(time.time()))) accumulateValue = event.get(self._serverConf["accumulateField"]) if not accumulateValue: ftlog.debug("TYActivityPlayGamePresentGift handleEvent, accumulateValue not find:", self._serverConf["accumulateField"]) return mustContinuous = 0 maxInterval = 0 accumulateIdentiferValue = "noidentifer" nowTimeStamp = int(time.time()) if 'accumulateMustContinuous' in self._serverConf: if self._serverConf["accumulateMustContinuous"] == 1: mustContinuous = 1 maxInterval = self._serverConf.get("accumulateMaxInterval", 300) # 判定是否是同一房间 if "accumulateIdentifer" in self._serverConf: accumulateIdentifer = self._serverConf["accumulateIdentifer"] accumulateIdentiferValue = event.get(accumulateIdentifer) accumulateIdentiferValue = gdata.getBigRoomId(accumulateIdentiferValue) ftlog.debug("this accumulateIdentiferValue bigRoomId is", accumulateIdentiferValue) if not accumulateIdentiferValue: ftlog.debug("TYActivityPlayGamePresentGift handleEvent, accumulateIdentiferValue not find:", accumulateIdentifer) return else: ftlog.error("TYActivityPlayGamePresentGift handleEvent, accumulateIdentifer not find") return userActivityKey = "TYActivity:%d:%d:%s" % (gameId, userId, activityId) ftlog.debug("playGamePresentGift handleEvent:", presentNum, accumulateKey, accumulateValue, accumulateType, accumulateIdentiferValue, mustContinuous, maxInterval, userActivityKey, rewardNeedNum, nowTimeStamp, hasRewardedKey) ret = daobase.executeUserLua(userId, self.HANDLE_EVENT_SCRIPT, 11, presentNum, accumulateKey, accumulateValue, accumulateType, accumulateIdentiferValue, mustContinuous, maxInterval, userActivityKey, rewardNeedNum, nowTimeStamp, hasRewardedKey) ftlog.debug("TYActivityPlayGamePresentGift handleEvent, executeUserLua ret:", ret)
def _exchange(self, userId, gameId, clientId, activityId, exchangeId): key = "TYActivity:%d:%d:%s" % (gameId, userId, activityId) ftlog.debug("getGifts key:", key) ftlog.debug("getGifts key!!!exchangeId:", exchangeId) num = daobase.executeUserCmd(userId, 'hget', key, 'giftNum') if not num: num = 0 giftsNum = num exConfig = self._getExchangeConfig(exchangeId) if not exConfig: return {"errorInfo": "config not exist", "errorCode": 2} needCount = exConfig["consumeCount"] limit = exConfig["limitTimes"] nowDateStr = time.strftime("%Y-%m-%d", time.localtime(time.time())) exTimesField = "exTimes:%d:%s" % (exchangeId, nowDateStr) key = "TYActivity:%d:%d:%s" % (gameId, userId, activityId) ftlog.debug("_exchange key:", key) exchangeResult = daobase.executeUserLua(userId, self.EXCHANGE_SCRIPT, 4, key, needCount, limit, exTimesField) if exchangeResult == 1: ftlog.info("exceed limit,", "userId", userId, "gameId", gameId, "activityId", activityId, "exchangeId", exchangeId, clientId) return { "exchangeResult": [], "reason": u"今日兑换次数已满!", "list": [], "giftsNum": giftsNum } if exchangeResult == 2: ftlog.info("no enough gifts,", "userId", userId, "gameId", gameId, "activityId", activityId, "exchangeId", exchangeId, clientId) ftlog.debug("self._clientConf:", self._clientConf) giftName = self._clientConf["config"]["gift"]["name"] info = u"您的%s不足!" % giftName ftlog.debug("_exchange key123:", key) ftlog.debug("_exchange key123:", giftsNum) return { "exchangeResult": [], "reason": info, "list": [], "giftsNum": giftsNum } result = {} result["exchangeResult"] = [] info = u"兑换成功,您获得" itemNum = len(exConfig["items"]) changed = [] from hall.entity.hallitem import itemSystem userAssets = itemSystem.loadUserAssets(userId) for item in exConfig["items"]: itemNum -= 1 asset = userAssets.addAsset(gameId, item['productId'], item['count'], int(time.time()), 'ACTIVITY_GETSCORE', 0) changed.append(asset) result["exchangeResult"].append({ "name": item["name"], "count": item["count"], "productId": item["productId"], "unit": item["unit"] }) if itemNum == 0: info = info + "%d%s%s。" % (item["count"], item["unit"], item["name"]) else: info = info + "%d%s%s," % (item["count"], item["unit"], item["name"]) ftlog.info('exchangeCode userId = ', userId, 'gameId = ', gameId, 'clientId = ', clientId, 'activityId = ', activityId, 'info = ', info) changeNames = TYAssetUtils.getChangeDataNames(changed) datachangenotify.sendDataChangeNotify(gameId, userId, changeNames) result["reason"] = info result["list"] = [] ''' 拿到当前剩余的积分,并返回 ''' ftlog.debug("_exchange key123:", key) result["giftsNum"] = giftsNum ftlog.debug("_exchange key123456:", giftsNum) ftlog.info("exchangeResult,", "userId", userId, "gameId", gameId, "activityId", activityId, "exchangeId", exchangeId, clientId, result) return result
def handleEvent(self, event): ''' 当收到每局结束发送的event时,在activity.py中调用该方法 处理前台活动返回的配置包括客户端配置和服务器配置 ''' ftlog.debug("TYActivityPlayGamePresentGift handleEvent123:", event.role, event.roomLevel, event.roundNum, event.gameId, event.userId, event.clientId) gameId = event.gameId userId = event.userId activityId = self._clientConf.get("id") if not activityId: ftlog.debug( "TYActivityPlayGamePresentGift handleEvent not find adtivityId" ) return if not self.checkOperative(): ftlog.debug("activity expired:", activityId) return # 对_config开始解析配置,conditions玩的局数,默认为1 for cond in self._serverConf["conditions"]: if cond == self._serverConf['accumulateField']: continue condValue = event.get(cond) if isinstance(self._serverConf[cond], list): if condValue not in self._serverConf[cond]: ftlog.debug( "TYActivityPlayGamePresentGift handleEvent, condValue not match,cond:", cond) return else: if condValue != self._serverConf[cond]: ftlog.debug( "TYActivityPlayGamePresentGift handleEvent, condValue not match,cond:", cond) return # 有效时间 if "dayTimeLimit" in self._serverConf: if not self._checkDayTime(): ftlog.debug( "TYActivityPlayGamePresentGift handleEvent, dayTimeLimit not match" ) return # 所在房间等级 if "presentBased" in self._serverConf: presentBasedValue = event.get(self._serverConf["presentBased"]) presentBasedValue = gdata.getBigRoomId(presentBasedValue) ftlog.debug("TYActivityPlayGamePresentGift handleEvent11:", presentBasedValue) if not presentBasedValue: ftlog.debug( "TYActivityPlayGamePresentGift handleEvent, presentBasedvalue not find, presentBased:", self._serverConf["presentBased"]) return else: if (not isinstance(presentBasedValue, int) ) or str(presentBasedValue ) not in self._serverConf["presentConfig"]: ftlog.debug( "TYActivityPlayGamePresentGift handleEvent, presentBasedValue not match:", presentBasedValue) return presentNum = self._serverConf["presentConfig"][str( presentBasedValue)] else: presentNum = self._serverConf["presentNum"] # 是否可连续,1表示1天内连续、0表示可连续 accumulateType = self._serverConf["accumulateType"] # 需要领取奖励的局数 rewardNeedNum = self._serverConf["rewardNeedNum"] accumulateKey = "accumulate" hasRewardedKey = "hasRewarded" if accumulateType == 1: accumulateKey = "%s:%s" % ( accumulateKey, time.strftime('%Y-%m-%d', time.localtime(time.time()))) hasRewardedKey = "%s:%s" % ( hasRewardedKey, time.strftime('%Y-%m-%d', time.localtime(time.time()))) accumulateValue = event.get(self._serverConf["accumulateField"]) if not accumulateValue: ftlog.debug( "TYActivityPlayGamePresentGift handleEvent, accumulateValue not find:", self._serverConf["accumulateField"]) return mustContinuous = 0 maxInterval = 0 accumulateIdentiferValue = "noidentifer" nowTimeStamp = int(time.time()) if 'accumulateMustContinuous' in self._serverConf: if self._serverConf["accumulateMustContinuous"] == 1: mustContinuous = 1 maxInterval = self._serverConf.get("accumulateMaxInterval", 300) # 判定是否是同一房间 if "accumulateIdentifer" in self._serverConf: accumulateIdentifer = self._serverConf[ "accumulateIdentifer"] accumulateIdentiferValue = event.get(accumulateIdentifer) accumulateIdentiferValue = gdata.getBigRoomId( accumulateIdentiferValue) ftlog.debug("this accumulateIdentiferValue bigRoomId is", accumulateIdentiferValue) if not accumulateIdentiferValue: ftlog.debug( "TYActivityPlayGamePresentGift handleEvent, accumulateIdentiferValue not find:", accumulateIdentifer) return else: ftlog.error( "TYActivityPlayGamePresentGift handleEvent, accumulateIdentifer not find" ) return userActivityKey = "TYActivity:%d:%d:%s" % (gameId, userId, activityId) ftlog.debug("playGamePresentGift handleEvent:", presentNum, accumulateKey, accumulateValue, accumulateType, accumulateIdentiferValue, mustContinuous, maxInterval, userActivityKey, rewardNeedNum, nowTimeStamp, hasRewardedKey) ret = daobase.executeUserLua( userId, self.HANDLE_EVENT_SCRIPT, 11, presentNum, accumulateKey, accumulateValue, accumulateType, accumulateIdentiferValue, mustContinuous, maxInterval, userActivityKey, rewardNeedNum, nowTimeStamp, hasRewardedKey) ftlog.debug( "TYActivityPlayGamePresentGift handleEvent, executeUserLua ret:", ret)
def _exchange(self, userId, gameId, clientId, activityId, exchangeId): key = "TYActivity:%d:%d:%s" % (gameId, userId, activityId) ftlog.debug("getGifts key:", key) ftlog.debug("getGifts key!!!exchangeId:", exchangeId) num = daobase.executeUserCmd(userId, 'hget', key, 'giftNum') if not num: num = 0 giftsNum = num exConfig = self._getExchangeConfig(exchangeId) if not exConfig: return {"errorInfo": "config not exist", "errorCode": 2} needCount = exConfig["consumeCount"] limit = exConfig["limitTimes"] nowDateStr = time.strftime("%Y-%m-%d", time.localtime(time.time())) exTimesField = "exTimes:%d:%s" % (exchangeId, nowDateStr) key = "TYActivity:%d:%d:%s" % (gameId, userId, activityId) ftlog.debug("_exchange key:", key) exchangeResult = daobase.executeUserLua(userId, self.EXCHANGE_SCRIPT, 4, key, needCount, limit, exTimesField) if exchangeResult == 1: ftlog.info("exceed limit,", "userId", userId, "gameId", gameId, "activityId", activityId, "exchangeId", exchangeId, clientId) return {"exchangeResult": [], "reason": u"今日兑换次数已满!", "list": [], "giftsNum": giftsNum} if exchangeResult == 2: ftlog.info("no enough gifts,", "userId", userId, "gameId", gameId, "activityId", activityId, "exchangeId", exchangeId, clientId) ftlog.debug("self._clientConf:", self._clientConf) giftName = self._clientConf["config"]["gift"]["name"] info = u"您的%s不足!" % giftName ftlog.debug("_exchange key123:", key) ftlog.debug("_exchange key123:", giftsNum) return {"exchangeResult": [], "reason": info, "list": [], "giftsNum": giftsNum} result = {} result["exchangeResult"] = [] info = u"兑换成功,您获得" itemNum = len(exConfig["items"]) changed = [] from hall.entity.hallitem import itemSystem userAssets = itemSystem.loadUserAssets(userId) for item in exConfig["items"]: itemNum -= 1 asset = userAssets.addAsset(gameId, item['productId'], item['count'], int(time.time()), 'ACTIVITY_GETSCORE', 0) changed.append(asset) result["exchangeResult"].append({"name": item["name"], "count": item["count"], "productId": item["productId"], "unit": item["unit"]}) if itemNum == 0: info = info + "%d%s%s。" % (item["count"], item["unit"], item["name"]) else: info = info + "%d%s%s," % (item["count"], item["unit"], item["name"]) ftlog.info('exchangeCode userId = ', userId, 'gameId = ', gameId, 'clientId = ', clientId, 'activityId = ', activityId, 'info = ', info) changeNames = TYAssetUtils.getChangeDataNames(changed) datachangenotify.sendDataChangeNotify(gameId, userId, changeNames) result["reason"] = info result["list"] = [] ''' 拿到当前剩余的积分,并返回 ''' ftlog.debug("_exchange key123:", key) result["giftsNum"] = giftsNum ftlog.debug("_exchange key123456:", giftsNum) ftlog.info("exchangeResult,", "userId", userId, "gameId", gameId, "activityId", activityId, "exchangeId", exchangeId, clientId, result) return result
def _setUserDatasForce(userId, datas): dataKey = UserDataSchema.mkey(userId) params = UserDataSchema.paramsDict2List(datas, 0) ret = daobase.executeUserLua(userId, user_scripts.MAIN_SET_HASH_DATA_FORCE, 2, dataKey, json.dumps(params)) return ret