def get_gift_for_user(conf, gifttype, userId, gameId, clientId): ftlog.debug("get_gift_for_user begin:") design = conf.get('design', {}) if gifttype in design: ftlog.debug("get_gift_for_user begin:1") from hall.entity.hallitem import itemSystem userAssets = itemSystem.loadUserAssets(userId) changed = [] if design[gifttype]["type"] == "user:coupon": x = random.uniform(1, design[gifttype]["limits"]) coinAdd = int(design[gifttype]["count"] * x) asset = userAssets.addAsset(gameId, design[gifttype]["type"], coinAdd, int(time.time()), 'HALL_FLIPCARD', 0) changed.append(asset) changeNames = TYAssetUtils.getChangeDataNames(changed) datachangenotify.sendDataChangeNotify(gameId, userId, changeNames) result = "恭喜你获得了%d" % coinAdd + "奖劵" return result elif design[gifttype]["type"] == "item:1007": x = random.uniform(1, design[gifttype]["limits"]) coinAdd = int(design[gifttype]["count"] * x) asset = userAssets.addAsset(gameId, design[gifttype]["type"], coinAdd, int(time.time()), 'HALL_FLIPCARD', 0) changed.append(asset) changeNames = TYAssetUtils.getChangeDataNames(changed) datachangenotify.sendDataChangeNotify(gameId, userId, changeNames) result = "恭喜你获得了%d" % coinAdd + "张参赛劵" return result if design[gifttype]["type"] == "item:4118": x = random.uniform(1, design[gifttype]["limits"]) coinAdd = int(design[gifttype]["count"] * x) asset = userAssets.addAsset(gameId, design[gifttype]["type"], coinAdd, int(time.time()), 'HALL_FLIPCARD', 0) changed.append(asset) changeNames = TYAssetUtils.getChangeDataNames(changed) datachangenotify.sendDataChangeNotify(gameId, userId, changeNames) result = "恭喜你获得了%d" % coinAdd + "天左轮手枪" return result elif design[gifttype]["type"] == "item:2003": x = random.uniform(1, design[gifttype]["limits"]) coinAdd = int(design[gifttype]["count"] * x) asset = userAssets.addAsset(gameId, design[gifttype]["type"], coinAdd, int(time.time()), 'HALL_FLIPCARD', 0) changed.append(asset) changeNames = TYAssetUtils.getChangeDataNames(changed) datachangenotify.sendDataChangeNotify(gameId, userId, changeNames) result = "恭喜你获得了%d" % coinAdd + "天记牌器" return result elif design[gifttype]["type"] == "coin": ftlog.debug("get_gift_for_user begin:2") x = random.uniform(1, design[gifttype]["limits"]) coinAdd = int(design[gifttype]["count"] * x) userchip.incrChip(userId, gameId, coinAdd, daoconst.CHIP_NOT_ENOUGH_OP_MODE_CLEAR_ZERO, 'HALL_FLIPCARD', 0, clientId) datachangenotify.sendDataChangeNotify(gameId, userId, 'chip') result = "恭喜你获得了%d" % coinAdd + "金币" return result else: result = "红包被领完了..." return result else: result = "红包领完了,我很抱歉" return result
def _buy(self, userId, gameId, clientId, activityId, buyId): buyConfig = self._getBuyConfig(buyId) if not buyConfig: return {"errorInfo": "config not exist", "errorCode": 2} consumeItemId = buyConfig["consume"] consumeName = buyConfig["consumeName"] consumeCount = buyConfig["count"] getCount = buyConfig["get"] unit = buyConfig["unit"] from hall.entity.hallitem import itemSystem userAssets = itemSystem.loadUserAssets(userId) _, count, _ = userAssets.consumeAsset(gameId, consumeItemId, consumeCount, int(time.time()), "ACTIVITY_BUY", 0) if count != consumeCount: info = u"您的%s不足!" % consumeName return {"reason": info} else: key = "TYActivity:%d:%d:%s" % (gameId, userId, activityId) daobase.executeUserCmd(userId, "hincrby", key, "giftNum", getCount) info = u"您获得了%d%s" % (getCount, unit) return {"reason": info}
def __getReward(self, rdskey, userId, gameId, actId, excode): config_ = daobase.executeMixCmd('HGET', 'excodeinfo:' + rdskey, 'rewards') try: config_ = json.loads(config_) except: return {} if not isinstance(config_, list): return {} rewards = {} items_ = config_ ftlog.debug('ExchangeCode.__getReward', 'items=', items_) from hall.entity.hallitem import itemSystem userAssets = itemSystem.loadUserAssets(userId) assetTupleList = [] for itemDict in items_: for assetKindId, count in itemDict.iteritems(): count = int(count) if assetKindId in self._rewardMap: assetKindId = self._rewardMap[assetKindId]['itemId'] assetTuple = userAssets.addAsset(gameId, assetKindId, count, int(time.time()), 'ACTIVITY_EXCHANGE', 0) rewards[assetKindId] = count assetTupleList.append(assetTuple) datachangenotify.sendDataChangeNotify(gameId, userId, TYAssetUtils.getChangeDataNames(assetTupleList)) ftlog.info('ExchangeCode->__statistics userid=', userId, 'excode=', excode, 'rewards=', rewards) msg = '兑换码兑换礼品成功,已自动加入到您的背包中,请查收。' message.send(9999, message.MESSAGE_TYPE_SYSTEM, userId, msg) result = TYAssetUtils.buildContentsString(assetTupleList) ftlog.debug('the last msg is result', result) return result
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 _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