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