Fix/mission commands fix (#24)
This commit is contained in:
Binary file not shown.
@@ -3,6 +3,7 @@ local Persistence = {}
|
||||
do
|
||||
---@class PersistentData
|
||||
---@field dead_units table<string, DeathState>
|
||||
---@field random_missions table<string, MissionState>
|
||||
---@field activeStage integer|nil
|
||||
|
||||
---@class DeathState
|
||||
@@ -19,6 +20,7 @@ do
|
||||
---@type PersistentData
|
||||
local tables = {
|
||||
dead_units = {},
|
||||
random_missions = {},
|
||||
activeStage = nil
|
||||
}
|
||||
|
||||
@@ -58,7 +60,9 @@ do
|
||||
end
|
||||
|
||||
local json = f:read("*a")
|
||||
local lua = net.json2lua(json)
|
||||
f:close()
|
||||
|
||||
local lua = net.json2lua(json) --[[@as PersistentData]]
|
||||
|
||||
if lua.activeStage then
|
||||
logger:info("Found active stage from save: " .. lua.activeStage)
|
||||
@@ -82,7 +86,7 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
f:close()
|
||||
|
||||
end
|
||||
|
||||
local writeToFile = function()
|
||||
@@ -133,12 +137,14 @@ do
|
||||
logger:info("Initiating Persistence Manager")
|
||||
|
||||
if lfs == nil or io == nil then
|
||||
env.error("[Spearhead][Persistence] lfs and io seem to be sanitized. Persistence is skipped and disabled")
|
||||
logger:error("lfs and io seem to be sanitized. Persistence is skipped and disabled")
|
||||
return
|
||||
end
|
||||
|
||||
path = (SpearheadConfig.Persistence.directory or (lfs.writedir() .. "\\Data" )) .. "\\" .. (SpearheadConfig.Persistence.fileName or "Spearhead_Persistence.json")
|
||||
|
||||
logger:info("Persistence file path: " .. path)
|
||||
|
||||
createFileIfNotExists()
|
||||
loadTablesFromFile()
|
||||
timer.scheduleFunction(UpdateContinuous, nil, timer.getTime() + 120)
|
||||
@@ -152,6 +158,25 @@ do
|
||||
updateRequired = true
|
||||
end
|
||||
|
||||
---comment
|
||||
---@param missionName string
|
||||
---@param pickedZone string
|
||||
Persistence.RegisterPickedRandomMission = function(missionName, pickedZone)
|
||||
if enabled == false then return end
|
||||
|
||||
if tables.random_missions == nil then tables.random_missions = {} end
|
||||
tables.random_missions[string.lower(missionName)] = pickedZone
|
||||
updateRequired = true
|
||||
end
|
||||
|
||||
---Get the picked random mission from the persistence file
|
||||
---@param missionName string
|
||||
---@return string?
|
||||
Persistence.GetPickedRandomMission = function(missionName)
|
||||
if enabled == false then return nil end
|
||||
return tables.random_missions[string.lower(missionName)]
|
||||
end
|
||||
|
||||
---Get the active stage as in the persistance file
|
||||
---@return integer|nil
|
||||
Persistence.GetActiveStage = function()
|
||||
|
||||
@@ -35,7 +35,8 @@ do -- INIT UTIL
|
||||
end
|
||||
|
||||
---Gets a random from the list
|
||||
---@param list table
|
||||
---@param list Array
|
||||
---@return any @random element from the list
|
||||
function UTIL.randomFromList(list)
|
||||
local max = #list
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
---@field code string
|
||||
---@field priority MissionPriority
|
||||
---@field location Vec2?
|
||||
---@field zoneName string
|
||||
---@field private _state MissionState
|
||||
---@field private _zoneName string
|
||||
---@field private _database Database
|
||||
---@field private _logger Logger
|
||||
---@field private _missionBriefing string?
|
||||
@@ -76,7 +76,7 @@ function Mission.New(zoneName, priority, database, logger)
|
||||
local o = {}
|
||||
local self = setmetatable(o, Mission)
|
||||
|
||||
self._zoneName = zoneName
|
||||
self.zoneName = zoneName
|
||||
self.name = parsed.missionName
|
||||
self.missionType = parsed.type
|
||||
self.displayMissionType = self.missionType or "unknown"
|
||||
@@ -352,7 +352,7 @@ end
|
||||
function Mission:NotifyMissionComplete()
|
||||
|
||||
self._missionCommandsHelper:RemoveMissionToCommands(self)
|
||||
self._logger:info("Mission Completed: " .. self._zoneName)
|
||||
self._logger:info("Mission Completed: " .. self.zoneName)
|
||||
trigger.action.outText("Mission " .. self.name .. " [" .. self.code .. "] was completed succesfully" , 20)
|
||||
|
||||
for _, listener in pairs(self._completeListeners) do
|
||||
|
||||
@@ -77,7 +77,6 @@ function StageBase.New(databaseManager, logger, airbaseName)
|
||||
for blueUnitName, blueUnitPos in pairs(blueUnitsPos) do
|
||||
for redUnitName, redUnitPos in pairs(redUnitsPos) do
|
||||
local distance = Spearhead.Util.VectorDistance3d(blueUnitPos, redUnitPos)
|
||||
env.info("distance: " .. tostring(distance))
|
||||
if distance <= cleanup_distance then
|
||||
self._cleanup_units[redUnitName] = true
|
||||
end
|
||||
|
||||
@@ -121,6 +121,8 @@ function Stage:superNew(database, stageConfig, logger, initData, missionPriority
|
||||
end
|
||||
|
||||
local randomMissionNames = database:getRandomMissionsForStage(self.zoneName)
|
||||
|
||||
---@type table<string, Array<Mission>>
|
||||
local randomMissionByName = {}
|
||||
for _, missionZoneName in pairs(randomMissionNames) do
|
||||
local mission = Spearhead.classes.stageClasses.Missions.Mission.New(missionZoneName, self._missionPriority, database, logger)
|
||||
@@ -132,9 +134,14 @@ function Stage:superNew(database, stageConfig, logger, initData, missionPriority
|
||||
end
|
||||
end
|
||||
|
||||
for _, missions in pairs(randomMissionByName) do
|
||||
local mission = Spearhead.Util.randomFromList(missions)
|
||||
for missionName, missions in pairs(randomMissionByName) do
|
||||
|
||||
local missionZonePicked = Spearhead.classes.persistence.Persistence.GetPickedRandomMission(missionName)
|
||||
if missionZonePicked == nil then
|
||||
local mission = Spearhead.Util.randomFromList(missions) --[[@as Mission]]
|
||||
if mission then
|
||||
Spearhead.classes.persistence.Persistence.RegisterPickedRandomMission(mission.name, mission.zoneName)
|
||||
|
||||
self._db.missionsByCode[mission.code] = mission
|
||||
if mission.missionType == "SAM" then
|
||||
table.insert(self._db.sams, mission)
|
||||
@@ -142,6 +149,19 @@ function Stage:superNew(database, stageConfig, logger, initData, missionPriority
|
||||
table.insert(self._db.missions, mission)
|
||||
end
|
||||
end
|
||||
else
|
||||
self._logger:info("Using persisted random mission with name: " .. missionName .. " and zone: " .. missionZonePicked)
|
||||
for _, mission in pairs(missions) do
|
||||
if string.lower(mission.zoneName) == string.lower(missionZonePicked) then
|
||||
self._db.missionsByCode[mission.code] = mission
|
||||
if mission.missionType == "SAM" then
|
||||
table.insert(self._db.sams, mission)
|
||||
else
|
||||
table.insert(self._db.missions, mission)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, mission in pairs(self._db.missionsByCode) do
|
||||
|
||||
@@ -31,8 +31,8 @@ function MissionCommandsHelper.getOrCreate(logLevel)
|
||||
---@param time number
|
||||
---@return number
|
||||
self.updateContinuous = function(selfA, time)
|
||||
if selfA.updateNeeded == false and timer.getTime() - selfA.lastUpdate < 10 then
|
||||
return time + 2
|
||||
if selfA.updateNeeded == false then
|
||||
return time + 10
|
||||
end
|
||||
|
||||
for _, unit in pairs(Spearhead.DcsUtil.getAllPlayerUnits()) do
|
||||
@@ -46,7 +46,7 @@ function MissionCommandsHelper.getOrCreate(logLevel)
|
||||
|
||||
selfA.lastUpdate = timer.getTime()
|
||||
selfA.updateNeeded = false
|
||||
return time + 3
|
||||
return time + 10
|
||||
end
|
||||
|
||||
timer.scheduleFunction(self.updateContinuous, self, timer.getTime() + 5)
|
||||
@@ -149,6 +149,7 @@ function MissionCommandsHelper:AddOverviewCommand(groupID)
|
||||
end
|
||||
|
||||
---@private
|
||||
---@param groupID number
|
||||
function MissionCommandsHelper:AddPinnedMission(groupID)
|
||||
|
||||
local pinndedMission = self.pinnedByGroup[tostring(groupID)]
|
||||
@@ -216,7 +217,6 @@ function MissionCommandsHelper:addMissionCommands(groupId, mission)
|
||||
path = { [1] = folderNames.secondary }
|
||||
end
|
||||
|
||||
|
||||
if path then
|
||||
self._logger:debug("Registering command: [" .. mission.code .. "]" .. mission.name)
|
||||
local missionFolderName = "[" .. mission.code .. "]" .. mission.name
|
||||
|
||||
+2
-2
@@ -61,7 +61,7 @@ SpearheadConfig = {
|
||||
--- io and lfs cannot be sanitized in the MissionScripting.lua
|
||||
|
||||
--- enables or disables the persistence logic in spearhead
|
||||
enabled = false,
|
||||
enabled = true,
|
||||
|
||||
--- sets the directory where the persistence file is stored
|
||||
--- if nil then lfs.writedir() will be used.
|
||||
@@ -69,7 +69,7 @@ SpearheadConfig = {
|
||||
directory = nil ,
|
||||
|
||||
--- the filename of the persistence file. Should end with .json for convention, but any text extension should do.
|
||||
fileName = "Spearhead_Persistence.json"
|
||||
fileName = "Spearhead_Persistence_Dev.json"
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user