Exemplo n.º 1
0
def getDailyQuestReward(userId, star, type="day"):
    """
    领取每日任务星级奖励
    """
    from newfish.entity.chest.chest_system import ChestFromType
    fromType = ChestFromType.Daily_Quest_Week_Chest if type == "week" else \
               ChestFromType.Daily_Quest_Daily_Chest
    reason = 0
    rewards = []
    finishedStar = 0
    key = _getUserDailyQuestWeeklyRewardKey(userId)
    ret = json.loads(weakdata.getWeekFishData(userId, key, "{}"))
    finishedWeekStar = ret.get("star", 0)
    gotRewardStars = _getUserQuestRewardData(userId, type)
    questInfo = getUserQuestInfoData(userId)
    all = len(questInfo) == len(config.getDailyQuestGroupOrder())
    if star in gotRewardStars:
        reason = 1  # 已经领取过奖励
    else:
        todayQuest, activeLv = getTodayQuest(userId)
        for k, v in todayQuest.iteritems():
            progress, state = questInfo.get(str(k), [0, QuestTaskState.Normal])
            if state == QuestTaskState.Received:
                finishedStar += v["taskLevel"]
        starConfig = config.getDailyQuestRewardConf(activeLv, all).get(str(star), {})
        if not starConfig or (type == "day" and finishedStar < star) or (type == "week" and finishedWeekStar < star):
            reason = 2  # 未达到领取条件
        else:
            rewards = starConfig.get("rewards")
            code = 0
            _insertQuestRewarded(userId, star, type)
            for reward in starConfig.get("rewards"):
                kindId = reward["itemId"]
                if util.isChestRewardId(kindId):
                    rewards = chest_system.getChestRewards(userId, kindId)
                    code = chest_system.deliveryChestRewards(userId, kindId, rewards, "DTASK_REWARD", fromType=fromType)
                else:
                    code = util.addRewards(userId, [reward], "DTASK_REWARD")
            if code != 0:
                ftlog.error("newfish->getDailyQuestReward =", userId, "rewards =", rewards)
    mo = MsgPack()
    mo.setCmd("task")
    mo.setResult("gameId", FISH_GAMEID)
    mo.setResult("userId", userId)
    mo.setResult("action", "dailyReward")
    mo.setResult("star", star)
    mo.setResult("type", type)
    mo.setResult("gotReward", gotRewardStars)
    mo.setResult("reason", reason)
    if reason == 0:
        gotRewardStars.append(star)
        mo.setResult("gotReward", gotRewardStars)
        mo.setResult("rewards", rewards)
        # _insertQuestRewarded(userId, star, type)
        module_tip.cancelModuleTipEvent(userId, "task", star)
    router.sendToUser(mo, userId)
    if ftlog.is_debug():
        ftlog.debug("daily_quest, userId =", userId, "star =", star, "type =", type, "mo =", mo)
Exemplo n.º 2
0
def getQuestReward(userId, taskId, rewardflyPos):
    """
    领取每日任务奖励
    """
    code = 1
    reason = 2
    rewards = []
    questInfo = getUserQuestInfoData(userId)
    progress, state = questInfo.get(str(taskId), [0, QuestTaskState.Normal])
    # 未达到领取条件
    if state == QuestTaskState.Normal:
        reason = 2
    # 已经领取过奖励
    elif state == QuestTaskState.Received:
        reason = 1
    elif state == QuestTaskState.Complete:
        reason = 0
        questInfo[str(taskId)] = [progress, QuestTaskState.Received]
        setUserQuestInfoData(userId, questInfo)
        module_tip.cancelModuleTipEvent(userId, "task", taskId)     # -1
        todayQuest, activeLv = getTodayQuest(userId)
        quest = todayQuest.get(taskId, {})
        rewards = _processQuestReward(userId, quest)                # quest.get("rewards", [])
        code = util.addRewards(userId, rewards, "DTASK_REWARD")
        taskLevel = quest.get("taskLevel", 0)
        # 更新周奖励的星级
        finishedWeekStar = _incrDailyQuestWeekStar(userId, taskLevel)
        questData = getDailyQuestData(userId)
        finishedStar = questData.get("finishedStar", 0)
        all = len(questInfo) == len(config.getDailyQuestGroupOrder())
        dailyQuestRewardFinishedStars = config.getDailyQuestRewardFinishedStars("day", all)
        for star in dailyQuestRewardFinishedStars:
            if finishedStar >= star > finishedStar - taskLevel:
                module_tip.addModuleTipEvent(userId, "task", star)
                break
        dailyQuestRewardFinishedWeekStars = config.getDailyQuestRewardFinishedStars("week", all)
        for star in dailyQuestRewardFinishedWeekStars:
            if finishedWeekStar >= star > finishedWeekStar - taskLevel:
                module_tip.addModuleTipEvent(userId, "task", star)
                break
        from newfish.entity.event import DailyTaskRewardEvent
        from newfish.game import TGFish
        event = DailyTaskRewardEvent(userId, FISH_GAMEID, taskId, taskLevel)
        TGFish.getEventBus().publishEvent(event)
    mo = MsgPack()
    mo.setCmd("task")
    mo.setResult("gameId", FISH_GAMEID)
    mo.setResult("userId", userId)
    mo.setResult("action", "questReward")
    mo.setResult("taskId", taskId)
    mo.setResult("rewardflyPos", rewardflyPos)
    mo.setResult("reason", reason)
    if reason == 0:
        mo.setResult("code", code)
        mo.setResult("rewards", rewards)
    router.sendToUser(mo, userId)
Exemplo n.º 3
0
def getTodayQuest(userId):
    """
    获取当日所有每日任务配置
    """
    activeLv = 0
    groupIdList = config.getDailyQuestGroupOrder()
    gunLevel_m = util.getGunLevelVal(userId, config.MULTIPLE_MODE)
    todayQuest = {}
    dailyQuestConf = config.getDailyQuestConf()
    questInfo = getUserQuestInfoData(userId)
    if len(questInfo) == 0:
        key = _getUserDailyQuestGroupLvKey(userId)
        groupLvData = gamedata.getGameAttrJson(userId, FISH_GAMEID, key, {})
        update = False
        for groupId in groupIdList:
            groupId = str(groupId)
            lvData = groupLvData.get(groupId, [1, 0, 0])
            lv = lvData[0]
            if lvData[1] >= 2:
                lv += 1
            taskId, _lv = getQuestTaskId(groupId, lv, gunLevel_m, userId)
            if _lv != lvData[0]:
                update = True
                groupLvData[groupId] = [_lv, 0, lvData[2]]
            if taskId:
                questInfo[str(taskId)] = [0, QuestTaskState.Normal]
                todayQuest[int(taskId)] = dailyQuestConf.get(str(taskId))
                activeLv += dailyQuestConf.get(str(taskId)).get("activeLv", 0)
        setUserQuestInfoData(userId, questInfo)
        if update:
            gamedata.setGameAttr(userId, FISH_GAMEID, key, json.dumps(groupLvData))
    else:
        questInfo = getUserQuestInfoData(userId)
        for taskId, _ in questInfo.iteritems():
            taskId = int(taskId)
            _quest = dailyQuestConf.get(str(taskId))
            if _quest:
                todayQuest[taskId] = _quest
                activeLv += _quest.get("activeLv", 0)
    activeLv = int(activeLv)
    old = activeLv
    activeLvList = sorted([int(key) for key in config.getDailyQuestRewardConf().keys()])
    if activeLv <= activeLvList[0]:
        activeLv = activeLvList[0]
    elif activeLv >= activeLvList[-1]:
        activeLv = activeLvList[-1]
    else:
        for val in activeLvList:
            if old >= val:
                activeLv = val
    if ftlog.is_debug():
        ftlog.debug("daily_quest, userId =", userId, "questInfo =", questInfo, "activeLv =", activeLv, old, activeLvList)
    return todayQuest, activeLv
Exemplo n.º 4
0
def refreshDailyQuestGroupLv(userId):
    """
    刷新每日任务难度等级
    """
    gunLevel_m = util.getGunLevelVal(userId, config.MULTIPLE_MODE)
    curDayStartTs = util.getDayStartTimestamp(int(time.time()))
    initLv = getInitQuestLv(userId)
    dailyQuestConf = config.getDailyQuestConf()
    key = _getUserDailyQuestGroupLvKey(userId)
    groupLvData = gamedata.getGameAttrJson(userId, FISH_GAMEID, key, {})
    questInfo = getUserQuestInfoData(userId)
    for taskId, state in questInfo.iteritems():
        state = state[-1]
        if ftlog.is_debug():
            ftlog.debug("daily_quest, userId =", userId, taskId, state, dailyQuestConf.get(str(taskId), {}))
        groupId = dailyQuestConf.get(str(taskId), {}).get("groupId", 0)
        if groupId:
            groupId = str(groupId)
            if groupId in groupLvData.keys():
                # 玩家没有连续登陆, 清零难度经验进度
                ts = 0
                if len(groupLvData[groupId]) > 2:
                    ts = groupLvData[groupId][2]
                dayStartTs = util.getDayStartTimestamp(ts)
                if dayStartTs + 86400 < curDayStartTs:
                    groupLvData[groupId][1] = 0
                    if ftlog.is_debug():
                        ftlog.debug("daily_quest, userId =", userId, "groupId =", groupId, "reset exp !")
                else:
                    if state >= QuestTaskState.Complete:        # 完成任务增加难度经验
                        if groupLvData[groupId][1] < 2:
                            groupLvData[groupId][1] += 1
                            if ftlog.is_debug():
                                ftlog.debug("daily_quest, userId =", userId, "groupId =", groupId, "increase exp !")
                    else:                                       # 未完成任务, 且前一天游戏时长超过20分钟则削减难度经验
                        _key = GameData.playGameTime % (FISH_GAMEID, userId, dayStartTs)
                        playGameTime = daobase.executeUserCmd(userId, "GET", _key) or 0
                        if playGameTime >= 20:
                            # 降级变更为1天不完成并且游戏时长大于20分钟就降级,即满足要求+1,不满足-2
                            if groupLvData[groupId][1] > -2 and groupLvData[groupId][0] > 1:
                                groupLvData[groupId][1] -= 2    # 1
                                if ftlog.is_debug():
                                    ftlog.debug("daily_quest, userId =", userId, "groupId =", groupId, "decrease exp !")
                    # if groupLvData[groupId][1] >= 2:
                    #     groupLvData[groupId][0] += 1
                    #     groupLvData[groupId][1] = 0
                    #     ftlog.debug("daily_quest, userId =", userId, "groupId =", groupId, "increase lv !")
                    if groupLvData[groupId][1] <= -2 and groupLvData[groupId][0] > 1:
                        groupLvData[groupId][0] -= 1
                        groupLvData[groupId][1] = 0
                        if ftlog.is_debug():
                            ftlog.debug("daily_quest, userId =", userId, "groupId =", groupId, "decrease lv !")
                groupLvData[groupId][2] = curDayStartTs
            else:
                _, lv = getQuestTaskId(groupId, initLv, gunLevel_m, userId)
                groupLvData[groupId] = [lv, 0, curDayStartTs]
            # groupLvData[groupId][0] = min(10, max(1, groupLvData[groupId][0]))
    if len(groupLvData) == 0:
        groupIdList = config.getDailyQuestGroupOrder()
        for groupId in groupIdList:
            _, lv = getQuestTaskId(groupId, initLv, gunLevel_m, userId)
            groupLvData[groupId] = [lv, 0, curDayStartTs]
    else:
        for k in groupLvData.keys():
            groupLvData[k][2] = curDayStartTs
    gamedata.setGameAttr(userId, FISH_GAMEID, key, json.dumps(groupLvData))
    if ftlog.is_debug():
        ftlog.debug("daily_quest, user ="******"groupLvData =", groupLvData)
Exemplo n.º 5
0
def getDailyQuestData(userId):
    """
    获取玩家每日任务数据
    """
    level = util.getUserLevel(userId)
    if level < config.getCommonValueByKey("dailyQuestOpenLevel"):
        return {}
    if not util.isFinishAllNewbieTask(userId):
        return {}
    lang = util.getLanguage(userId)
    # 替换为新版任务时,重置玩家每日任务数据.
    key = _getUserDailyQuestGroupLvKey(userId)
    groupLvData = gamedata.getGameAttrJson(userId, FISH_GAMEID, key, {})
    if len(groupLvData) == 0:
        refreshDailyQuestData(userId)
    groupIdList = config.getDailyQuestGroupOrder()
    dailyQuestData = {}
    todayQuest, activeLv = getTodayQuest(userId)
    # userData = _getUserQuestData(userId)
    finishedStar = 0
    key = _getUserDailyQuestWeeklyRewardKey(userId)
    ret = json.loads(weakdata.getWeekFishData(userId, key, "{}"))
    finishedWeekStar = ret.get("star", 0)
    questInfo = getUserQuestInfoData(userId)
    tasks = []
    update = False
    questList = sorted(todayQuest.items(), key=lambda val: groupIdList.index(val[1]["groupId"]))
    all = len(todayQuest) == len(groupIdList)
    # for k, v in todayQuest.iteritems():
    for _val in questList:
        k, v = _val
        task = {}
        task["taskId"] = k
        task["taskLevel"] = v["taskLevel"]
        task["targetsNum"] = v["targetsNum"]
        # progress = userData.get(v["taskType"], 0)
        progress, state = questInfo.get(str(k), [0, QuestTaskState.Normal])
        if progress >= task["targetsNum"] or state >= QuestTaskState.Complete:
            progress = task["targetsNum"]
            # 已领取
            if state == QuestTaskState.Received:
                finishedStar += task["taskLevel"]
            elif state == QuestTaskState.Normal:
                state = QuestTaskState.Complete
                questInfo[str(k)] = [progress, state]
                update = True
        task["progress"] = progress
        task["taskState"] = state
        task["rewards"] = v["rewards"]
        task["chestRewards"] = []
        for val in v["rewards"]:
            itemId = val["name"]
            if util.isChestRewardId(itemId):
                task["chestRewards"].append({"chestId": itemId, "chestInfo": chest_system.getChestInfo(itemId)})
        # if v["taskType"] == TaskType.CoinNum and v["des"].find("%s") >= 0:
        #    task["des"] = v["des"] % util.formatScore(v["targetsNum"])
        # elif v["des"].find("%d") >= 0:
        #    task["des"] = v["des"] % v["targetsNum"]
        # else:
        #    task["des"] = v["des"]
        vDesId = v["des"]
        vDes = config.getMultiLangTextConf(str(vDesId), lang=lang)
        if v["taskType"] == TaskType.CoinNum and vDes.find("%s") >= 0:
            task["des"] = vDes % util.formatScore(v["targetsNum"], lang=lang)
        elif vDes.find("%d") >= 0:
            task["des"] = vDes % v["targetsNum"]
        elif vDes.count("{}") >= 2:
            task["des"] = vDes.format(v["gunX"], v["targetsNum"])
        else:
            task["des"] = vDes
        tasks.append(task)
    if update:
        setUserQuestInfoData(userId, questInfo)
    dailyQuestReward = config.getDailyQuestRewardConf(activeLv, all)
    gotReward = _getUserQuestRewardData(userId, "day")
    gotWeekReward = _getUserQuestRewardData(userId, "week")
    dailyQuestData["tasks"] = tasks
    rewardData = [dailyQuestReward[str(key)] for key in sorted(map(int, dailyQuestReward.keys()))]
    for v in rewardData:
        v["chestRewards"] = []
        for val in v["rewards"]:
            itemId = val["itemId"]
            if util.isChestRewardId(itemId):
                v["chestRewards"].append({"chestId": itemId, "chestInfo": chest_system.getChestInfo(itemId)})
    dailyQuestData["rewardData"] = rewardData
    dailyQuestData["finishedStar"] = finishedStar
    dailyQuestData["gotReward"] = gotReward
    dailyQuestData["finishedWeekStar"] = finishedWeekStar
    dailyQuestData["gotWeekReward"] = gotWeekReward
    dailyQuestData["refreshData"] = config.getDailyQuestRefreshConf()
    dailyQuestData["refreshedTimes"] = gamedata.getGameAttrInt(userId, FISH_GAMEID, GameData.refreshDailyQuestTimes)
    return dailyQuestData