GE Lua Documentation

Press F to search!

aggregateAttempt

Definition


-- @/lua/ge/extensions/gameplay/missions/progress.lua:366

local function aggregateAttempt(id, attempt, progressKey)
  local mission = gameplay_missions_missions.getMissionById(id)
  if not mission then
    plog("E", "", "Trying to saveMissionAttempt nonexitent mission by ID: " .. dumps(id))
    return
  end
  progressKey = progressKey or mission.defaultProgressKey or "default"
  local progress = mission.saveData.progress[progressKey]
  local unlockMissionsBefore = gameplay_missions_unlocks.getSimpleUnlockedStatus()
  local unlockLeaguesBefore = {}
  if career_career.isActive() then
    unlockLeaguesBefore = career_modules_branches_leagues.getSimpleUnlockedStatus()
  end
  -- sanitize attempt
  sanitizeAttempt(attempt, mission)
  -- insert into progress
  table.insert(progress.attempts, attempt)

  local aggregateChange = { list = {}, newBestKeysByKey = {} }
  --log("","D",dumps(attempt))

  if not batchMode then
    plog("I", "aggregating regular progress.")
  end

  -- aggregate generic values
  progress.aggregate = aggregateProgress(progress, attempt, aggregateChange, mission)

  extensions.hook("onMissionAttemptAggregated", attempt, mission, progressKey)

    -- adjust dynamic rewards before adding the rewards
  M.setDynamicStarRewards(mission, mission.lastUserSettings)


  -- aggregate stars: which have been achieven and how often?
  -- this is for the whole mission, not for a progress key.
  local unlockedStarsChanged = {}
  local starRewards = {list = {}, sums = {}, sumList = {}, rewardMultiplierAdditionalAmount = 0, originalRewardsPerStar = {}}
  local branchMultiplier = 1
  local rewardBranch = nil
  if career_career and career_career.isActive() then
    local branch = career_branches.getBranchById(mission.careerSetup.skill)
    branchMultiplier = career_branches.getLevelRewardMultiplier(branch.id)
    if branchMultiplier then
      rewardBranch = branch.id
    end
    if not branchMultiplier then
      local parentBranch = career_branches.getBranchById(branch.parentId)
      branchMultiplier = career_branches.getLevelRewardMultiplier(parentBranch.id)
      if branchMultiplier then
        rewardBranch = parentBranch.id
      end
    end
    if not branchMultiplier then
      branchMultiplier = 1
    end
  end
  for star, _ in pairs(attempt.unlockedStars or {}) do
    if mission.careerSetup.starsActive[star] then

      mission.saveData.unlockedStars[star] = mission.saveData.unlockedStars[star] or 0

      if attempt.unlockedStars[star] then
        unlockedStarsChanged[star] = mission.saveData.unlockedStars[star] == 0
        mission.saveData.unlockedStars[star] = mission.saveData.unlockedStars[star] + 1
        if career_career and career_career.isActive() then
          local rewards = mission.careerSetup.starRewards[star] or {}
          starRewards.originalRewardsPerStar[star] = deepcopy(rewards)
          for _, reward in ipairs(rewards) do
            local baseAmount = (starRewards.sums[reward.attributeKey] or 0)
            starRewards.sums[reward.attributeKey] = baseAmount + reward.rewardAmount
            if reward.attributeKey == "money" then
              starRewards.sums[reward.attributeKey] = baseAmount + reward.rewardAmount * branchMultiplier
              starRewards.rewardMultiplierAdditionalAmount = starRewards.rewardMultiplierAdditionalAmount + reward.rewardAmount * (branchMultiplier - 1)
              starRewards.rewardMultiplierBasedOnBranch = rewardBranch
              starRewards.multiplierValue = branchMultiplier
            end
            local rCopy = deepcopy(reward)
            rCopy.sourceStar = star
            table.insert(starRewards.list, rCopy)
          end
        end
      end
    end
  end

  local ordered = tableKeysSorted(starRewards.sums)
  career_branches.orderAttributeKeysByBranchOrder(ordered)
  for _, key in ipairs(ordered) do
    table.insert(starRewards.sumList,{attributeKey = key, rewardAmount = starRewards.sums[key], icon = career_branches.getBranchIcon(key) })
  end

  -- add career rewards info
  local formattedRewards = M.addCareerRewardInfo(starRewards, mission, attempt)

  -- configurable aggregates
  for _, config in ipairs(mission.autoAggregates or {}) do
    if autoAggregate[config.type] then
      plog("I", "aggregating auto-" .. config.type .. ": " .. config.attemptKey)
      autoAggregate[config.type](progress, attempt, config, mission, aggregateChange)
    end
  end
  -- let the mission also aggregate, for leaderboards, custom scores etc
  if mission.aggregateAttempt then
    plog("I", "aggregating mission custom progress.")
    local succ, err, agg = xpcall(function()
      mission:aggregateAttempt(mission.saveData, progress, attempt, aggregate, aggregateChange)
    end, debug.traceback)
    if not succ then
      plog("E", "", "Error while aggregating attempt for mission ID: " .. dumps(id) .. ". Error follows:")
      plog("E", "", err)
    end
  end

  -- unlock quicktravel when attempt is at least passed or completed
  local quickTravelBefore = mission.saveData.quickTravelUnlocked
  mission.saveData.quickTravelUnlocked = attempt.type == 'completed' or attempt.type == 'passed' or mission.saveData.quickTravelUnlocked

  -- unlock userSettings when attempt is at least passed or completed
  local userSettingsBefore = mission.saveData.userSettingsUnlocked
  mission.saveData.userSettingsUnlocked = attempt.type == 'completed' or attempt.type == 'passed' or mission.saveData.userSettingsUnlocked


  -- do rewards
  if career_career and career_career.isActive() then
    local sumChange = {}
    for key, amount in pairs(starRewards.sums) do
      sumChange[key] = (sumChange[key] or 0) + amount
    end
    if next(sumChange) then
      career_modules_playerAttributes.addAttributes(sumChange, {tags={"gameplay","reward","mission"}, label="Received challenge rewards: " .. (translateLanguage(mission.name,mission.name,true) or "(Unnamed Mission)")})
    end
  end

  -- put into career playbook if active
  if career_career and career_career.isActive() and career_modules_playbookWriter then
    career_modules_playbookWriter.addMissionPlayedEntry(id, attempt.unlockedStars)
  end

  --reduce rewards for successive attempts
  M.reduceCareerRewardsForDefaultStars(mission)



  if not batchMode then
    gameplay_missions_unlocks.updateUnlockStatus()
    local unlockMissionsAfter = gameplay_missions_unlocks.getSimpleUnlockedStatus()
    local unlockChange = gameplay_missions_unlocks.getUnlockDiff(unlockMissionsBefore, unlockMissionsAfter)
    local unlockedMissions = unlockChange.missionsList or {}
    -- notify career for unlocked missions
    if career_career and career_career.isActive() and career_modules_logbook then
      for _, elem in ipairs(unlockedMissions) do
        career_modules_logbook.missionUnlocked(elem.id)
        extensions.hook("onMissionUnlocked", elem.id)
        gameplay_rawPois.clear()
      end
    end



    local ret = {
      formattedAttempt = M.formatAttemptSimple(attempt, mission),
      aggregateChange = aggregateChange,
      unlockChange = unlockChange,
      nextMissionsUnlock = gameplay_missions_unlocks.getMissionBasedUnlockDiff(mission, unlockChange),
      unlockedMissions = unlockedMissions,
      unlockedStarsAttempt = attempt.unlockedStars,
      unlockedStarsChanged = unlockedStarsChanged,
      starRewards = starRewards,
      formattedRewards = formattedRewards,
    }
    if career_career.isActive() then
      local unlockLeaguesAfter = career_modules_branches_leagues.getSimpleUnlockedStatus()
      ret.unlockedLeagues = career_modules_branches_leagues.getLeaguesForUnlockChange(unlockLeaguesBefore, unlockLeaguesAfter)
    end

    if quickTravelBefore ~= mission.saveData.quickTravelUnlocked then
      ret.quickTravelUnlockedChange = true
    end
    if userSettingsBefore ~= mission.saveData.userSettingsUnlocked then
      ret.userSettingsUnlockedChange = true
    end
    if not shipping_build then
      permaLog(ret)
    end
    return ret
  else
    return {}
  end
  -- actually save progress
  --saveMissionSaveData(id)
end

Callers

@/lua/ge/extensions/flowgraph/nodes/activity/aggregateAttempt.lua
    local progressKey = self.pinIn.progressKey.value or self.mgr.activity.currentProgressKey or self.mgr.activity.defaultProgressKey
    local totalChange = gameplay_missions_progress.aggregateAttempt(self.mgr.activity.id, attempt, progressKey)
    local aggregateChange = totalChange.aggregateChange
@/lua/ge/extensions/gameplay/missions/missionTypes/flowMission.lua
    local attempt = data.attempt or gameplay_missions_progress.newAttempt("abandoned")
    --gameplay_missions_progress.aggregateAttempt(self.id, attempt)
    --gameplay_missions_progress.saveMissionSaveData(self.id)
@/gameplay/missionTypes/rallyStage/constructor.lua

function C:aggregateAttempt(saveData, progress, attempt, aggregate, aggregateChange)
  -- Configuration for survivor pace calculation
@/lua/ge/extensions/gameplay/rally/loop/rallyAttempts.lua
local function aggregate(attempt, missionId)
  local totalChange = gameplay_missions_progress.aggregateAttempt(missionId, attempt)
  return totalChange
@/gameplay/missionTypes/rallyLoop/constructor.lua

-- function C:aggregateAttempt(saveData, progress, attempt, aggregate, aggregateChange)
--   -- Configuration for survivor pace calculation
@/gameplay/missionTypes/timeTrial/constructor.lua

function C:aggregateAttempt(saveData, progress, attempt, aggregate, aggregateChange)
  -- write which vehicle has been used for which star, for reward reduction.
@/gameplay/missionTypes/dragStripRace/constructor.lua

function C:aggregateAttempt(saveData, progress, attempt, aggregate, aggregateChange)
  -- write which vehicle has been used for which star, for reward reduction.
@/gameplay/missionTypes/dragStripAPM/constructor.lua

function C:aggregateAttempt(saveData, progress, attempt, aggregate, aggregateChange)
  -- write which vehicle has been used for which star, for reward reduction.
@/lua/ge/extensions/gameplay/missions/progress.lua
    local succ, err, agg = xpcall(function()
      mission:aggregateAttempt(mission.saveData, progress, attempt, aggregate, aggregateChange)
    end, debug.traceback)
      batchMode = true
      aggregateAttempt(mission.id, attempt, progressKey)
      batchMode = false
  dump(attempt)
  local totalChange = M.aggregateAttempt(id, attempt, mission.defaultProgressKey)
      local attempt = M.newAttempt(mission:getRandomizedAttempt())
      local totalChange = M.aggregateAttempt(id, attempt, progressKey)
      if dumpChange then