mirror of
https://github.com/mrSkortch/MissionScriptingTools.git
synced 2025-08-15 10:47:23 +00:00
Database Update Changes
ADDED: dbNum entry to unit table to define which index the the unit is at in the unitsByNum DB table OPTIMIZED: multiple calls of getPosition to just be a single getPoint call MODIFIED: process for updating and writing DB files. Moved the editing of DB into its own function ADDED: error messages and handling if a DB entry fails to be updated ADDED: mist.forceAddToDB function which accepts a groupName or a static objects name to force add it to the databases.
This commit is contained in:
parent
0c37aaaa09
commit
be5023e2fd
289
mist.lua
289
mist.lua
@ -35,7 +35,7 @@ mist = {}
|
||||
-- don't change these
|
||||
mist.majorVersion = 4
|
||||
mist.minorVersion = 5
|
||||
mist.build = 115
|
||||
mist.build = 116
|
||||
|
||||
-- forward declaration of log shorthand
|
||||
local log
|
||||
@ -746,17 +746,21 @@ do -- the main scope
|
||||
mist.DBs.groupsByName[group_data.groupName] = mist.utils.deepCopy(group_data)
|
||||
mist.DBs.groupsById[group_data.groupId] = mist.utils.deepCopy(group_data)
|
||||
for unit_ind, unit_data in pairs(group_data.units) do
|
||||
mist.DBs.unitsByName[unit_data.unitName] = mist.utils.deepCopy(unit_data)
|
||||
mist.DBs.unitsById[unit_data.unitId] = mist.utils.deepCopy(unit_data)
|
||||
local copy = mist.utils.deepCopy(unit_data)
|
||||
local num = #mist.DBs.unitsByNum + 1
|
||||
copy.dbNum = num
|
||||
|
||||
mist.DBs.unitsByName[unit_data.unitName] = mist.utils.deepCopy(copy)
|
||||
mist.DBs.unitsById[unit_data.unitId] = mist.utils.deepCopy(copy)
|
||||
|
||||
mist.DBs.unitsByCat[unit_data.category] = mist.DBs.unitsByCat[unit_data.category] or {} -- future-proofing against new categories...
|
||||
table.insert(mist.DBs.unitsByCat[unit_data.category], mist.utils.deepCopy(unit_data))
|
||||
table.insert(mist.DBs.unitsByCat[unit_data.category], mist.utils.deepCopy(copy))
|
||||
--dbLog:info('inserting $1', unit_data.unitName)
|
||||
table.insert(mist.DBs.unitsByNum, mist.utils.deepCopy(unit_data))
|
||||
table.insert(mist.DBs.unitsByNum, mist.utils.deepCopy(copy))
|
||||
|
||||
if unit_data.skill and (unit_data.skill == "Client" or unit_data.skill == "Player") then
|
||||
mist.DBs.humansByName[unit_data.unitName] = mist.utils.deepCopy(unit_data)
|
||||
mist.DBs.humansById[unit_data.unitId] = mist.utils.deepCopy(unit_data)
|
||||
mist.DBs.humansByName[unit_data.unitName] = mist.utils.deepCopy(copy)
|
||||
mist.DBs.humansById[unit_data.unitId] = mist.utils.deepCopy(copy)
|
||||
--if Unit.getByName(unit_data.unitName) then
|
||||
-- mist.DBs.activeHumans[unit_data.unitName] = mist.utils.deepCopy(unit_data)
|
||||
-- mist.DBs.activeHumans[unit_data.unitName].playerName = Unit.getByName(unit_data.unitName):getPlayerName()
|
||||
@ -901,9 +905,10 @@ do -- the main scope
|
||||
end
|
||||
end
|
||||
|
||||
local function dbUpdate(event, objType, origGroupName)
|
||||
--dbLog:info('dbUpdate')
|
||||
local function dbUpdate(event, oType, origGroupName)
|
||||
--dbLog:info('dbUpdate: $1', event)
|
||||
local newTable = {}
|
||||
local objType = oType
|
||||
newTable.startTime = 0
|
||||
if type(event) == 'string' then -- if name of an object.
|
||||
local newObject
|
||||
@ -911,6 +916,7 @@ do -- the main scope
|
||||
newObject = Group.getByName(event)
|
||||
elseif StaticObject.getByName(event) then
|
||||
newObject = StaticObject.getByName(event)
|
||||
objType = "static"
|
||||
-- log:info('its static')
|
||||
else
|
||||
log:warn('$1 is not a Group or Static Object. This should not be possible. Sent category is: $2', event, objType)
|
||||
@ -982,15 +988,16 @@ do -- the main scope
|
||||
newTable.units = {}
|
||||
if objType == 'group' then
|
||||
for unitId, unitData in pairs(unitOneRef) do
|
||||
local point = unitData:getPoint()
|
||||
newTable.units[unitId] = {}
|
||||
newTable.units[unitId].unitName = unitData:getName()
|
||||
|
||||
newTable.units[unitId].x = mist.utils.round(unitData:getPosition().p.x)
|
||||
newTable.units[unitId].y = mist.utils.round(unitData:getPosition().p.z)
|
||||
newTable.units[unitId].x = mist.utils.round(point.x)
|
||||
newTable.units[unitId].y = mist.utils.round(point.z)
|
||||
newTable.units[unitId].point = {}
|
||||
newTable.units[unitId].point.x = newTable.units[unitId].x
|
||||
newTable.units[unitId].point.y = newTable.units[unitId].y
|
||||
newTable.units[unitId].alt = mist.utils.round(unitData:getPosition().p.y)
|
||||
newTable.units[unitId].alt = mist.utils.round(point.y)
|
||||
newTable.units[unitId].speed = mist.vec.mag(unitData:getVelocity())
|
||||
|
||||
newTable.units[unitId].heading = mist.getHeading(unitData, true)
|
||||
@ -1028,15 +1035,16 @@ do -- the main scope
|
||||
end
|
||||
else -- its a static
|
||||
newTable.category = 'static'
|
||||
local point = newObject:getPoint()
|
||||
newTable.units[1] = {}
|
||||
newTable.units[1].unitName = newObject:getName()
|
||||
newTable.units[1].category = 'static'
|
||||
newTable.units[1].x = mist.utils.round(newObject:getPosition().p.x)
|
||||
newTable.units[1].y = mist.utils.round(newObject:getPosition().p.z)
|
||||
newTable.units[1].x = mist.utils.round(point.x)
|
||||
newTable.units[1].y = mist.utils.round(point.z)
|
||||
newTable.units[1].point = {}
|
||||
newTable.units[1].point.x = newTable.units[1].x
|
||||
newTable.units[1].point.y = newTable.units[1].y
|
||||
newTable.units[1].alt = mist.utils.round(newObject:getPosition().p.y)
|
||||
newTable.units[1].alt = mist.utils.round(point.y)
|
||||
newTable.units[1].heading = mist.getHeading(newObject, true)
|
||||
newTable.units[1].type = newObject:getTypeName()
|
||||
newTable.units[1].unitId = tonumber(newObject:getID())
|
||||
@ -1181,6 +1189,151 @@ do -- the main scope
|
||||
end
|
||||
end
|
||||
|
||||
local updateChecker = {}
|
||||
|
||||
|
||||
local function writeDBTables(newEntry)
|
||||
local ldeepCopy = mist.utils.deepCopy
|
||||
local newTable = newEntry.data
|
||||
--dbLog:info(newTable)
|
||||
|
||||
local state = 0
|
||||
if updateChecker[newTable.name] then
|
||||
dbLog:warn("Failed to add to database: $1. Stopped at state: $2", newTable.name, updateChecker[newTable.name])
|
||||
return false
|
||||
else
|
||||
--dbLog:info('define default state')
|
||||
updateChecker[newTable.name] = 0
|
||||
--dbLog:info('define default state1')
|
||||
state = updateChecker[newTable.name]
|
||||
--dbLog:info('define default state2')
|
||||
end
|
||||
|
||||
local updated = newEntry.isUpdated
|
||||
local mistCategory
|
||||
--dbLog:info('define categoryy')
|
||||
if type(newTable.category) == 'string' then
|
||||
mistCategory = string.lower(newTable.category)
|
||||
end
|
||||
|
||||
if string.upper(newTable.category) == 'GROUND_UNIT' then
|
||||
mistCategory = 'vehicle'
|
||||
newTable.category = mistCategory
|
||||
elseif string.upper(newTable.category) == 'AIRPLANE' then
|
||||
mistCategory = 'plane'
|
||||
newTable.category = mistCategory
|
||||
elseif string.upper(newTable.category) == 'HELICOPTER' then
|
||||
mistCategory = 'helicopter'
|
||||
newTable.category = mistCategory
|
||||
elseif string.upper(newTable.category) == 'SHIP' then
|
||||
mistCategory = 'ship'
|
||||
newTable.category = mistCategory
|
||||
end
|
||||
--dbLog:info('Update unitsBy')
|
||||
state = 1
|
||||
for newId, newUnitData in pairs(newTable.units) do
|
||||
--dbLog:info(newId)
|
||||
newUnitData.category = mistCategory
|
||||
|
||||
--dbLog:info(updated)
|
||||
if mist.DBs.unitsByName[newUnitData.unitName] and updated == true then --if unit existed before and something was updated, write over the entry for a given unit name just in case.
|
||||
state = 1.1
|
||||
--dbLog:info('Updating Unit Tables')
|
||||
local refNum = mist.DBs.unitsByName[newUnitData.unitName].dbNum
|
||||
for i = 1, #mist.DBs.unitsByCat[mistCategory] do
|
||||
if mist.DBs.unitsByCat[mistCategory][i].unitName == newUnitData.unitName then
|
||||
--dbLog:info('Entry Found, Rewriting for unitsByCat')
|
||||
mist.DBs.unitsByCat[mistCategory][i] = ldeepCopy(newUnitData)
|
||||
break
|
||||
end
|
||||
end
|
||||
state = 1.2
|
||||
--dbLog:info('updateByNum')
|
||||
if refNum then -- easy way
|
||||
--dbLog:info('refNum exists, Rewriting for unitsByCat')
|
||||
mist.DBs.unitsByNum[refNum] = ldeepCopy(newUnitData)
|
||||
else --- the hard way
|
||||
--dbLog:info('iterate unitsByNum')
|
||||
for i = 1, #mist.DBs.unitsByNum do
|
||||
if mist.DBs.unitsByNum[i].unitName == newUnitData.unitName then
|
||||
--dbLog:info('Entry Found, Rewriting for unitsByNum')
|
||||
mist.DBs.unitsByNum[i] = ldeepCopy(newUnitData)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
state = 1.3
|
||||
--dbLog:info('Unitname not in use, add as normal')
|
||||
newUnitData.dbNum = #mist.DBs.unitsByNum + 1
|
||||
mist.DBs.unitsByCat[mistCategory][#mist.DBs.unitsByCat[mistCategory] + 1] = ldeepCopy(newUnitData)
|
||||
mist.DBs.unitsByNum[#mist.DBs.unitsByNum + 1] = ldeepCopy(newUnitData)
|
||||
end
|
||||
if newUnitData.unitId then
|
||||
--dbLog:info('byId')
|
||||
mist.DBs.unitsById[tonumber(newUnitData.unitId)] = ldeepCopy(newUnitData)
|
||||
end
|
||||
mist.DBs.unitsByName[newUnitData.unitName] = ldeepCopy(newUnitData)
|
||||
end
|
||||
-- this is a really annoying DB to populate. Gotta create new tables in case its missing
|
||||
--dbLog:info('write mist.DBs.units')
|
||||
state = 2
|
||||
if not mist.DBs.units[newTable.coalition] then
|
||||
mist.DBs.units[newTable.coalition] = {}
|
||||
end
|
||||
state = 3
|
||||
if not mist.DBs.units[newTable.coalition][newTable.country] then
|
||||
mist.DBs.units[newTable.coalition][(newTable.country)] = {}
|
||||
mist.DBs.units[newTable.coalition][(newTable.country)].countryId = newTable.countryId
|
||||
end
|
||||
state = 4
|
||||
if not mist.DBs.units[newTable.coalition][newTable.country][mistCategory] then
|
||||
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] = {}
|
||||
end
|
||||
state = 5
|
||||
if updated == true then
|
||||
--dbLog:info('Updating DBsUnits')
|
||||
for i = 1, #mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] do
|
||||
if mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][i].groupName == newTable.groupName then
|
||||
--dbLog:info('Entry Found, Rewriting')
|
||||
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][i] = ldeepCopy(newTable)
|
||||
break
|
||||
end
|
||||
end
|
||||
else
|
||||
--dbLog:info('adding to DBs Units')
|
||||
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][#mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] + 1] = ldeepCopy(newTable)
|
||||
end
|
||||
state = 6
|
||||
|
||||
if newTable.groupId then
|
||||
--dbLog:info('Make groupsById')
|
||||
mist.DBs.groupsById[newTable.groupId] = ldeepCopy(newTable)
|
||||
end
|
||||
--dbLog:info('make groupsByName')
|
||||
mist.DBs.groupsByName[newTable.name] = ldeepCopy(newTable)
|
||||
--dbLog:info('add to dynGroups')
|
||||
mist.DBs.dynGroupsAdded[#mist.DBs.dynGroupsAdded + 1] = ldeepCopy(newTable)
|
||||
--dbLog:info('clear entry')
|
||||
updateChecker[newTable.name] = nil
|
||||
--dbLog:info('return')
|
||||
return true
|
||||
end
|
||||
|
||||
function mist.forceAddToDB(object)
|
||||
-- object is static object or group.
|
||||
-- call dbUpdate to get the table
|
||||
|
||||
local tbl = dbUpdate(object)
|
||||
if tbl then
|
||||
local res = writeDBTables(tbl)
|
||||
if not res then
|
||||
log:warn("Failed to force add to DBs: $1", object)
|
||||
end
|
||||
end
|
||||
-- call writeDBTables with that table.
|
||||
end
|
||||
|
||||
local function updateDBTables()
|
||||
local i = #writeGroups
|
||||
|
||||
@ -1189,108 +1342,24 @@ do -- the main scope
|
||||
savesPerRun = 5
|
||||
end
|
||||
if i > 0 then
|
||||
-- dbLog:info('updateDBTables')
|
||||
local ldeepCopy = mist.utils.deepCopy
|
||||
--dbLog:info('updateDBTables: $1', #writeGroups)
|
||||
|
||||
for x = 1, i do
|
||||
-- dbLog:info(writeGroups[x])
|
||||
local newTable = writeGroups[x].data
|
||||
local updated = writeGroups[x].isUpdated
|
||||
local mistCategory
|
||||
if type(newTable.category) == 'string' then
|
||||
mistCategory = string.lower(newTable.category)
|
||||
end
|
||||
|
||||
if string.upper(newTable.category) == 'GROUND_UNIT' then
|
||||
mistCategory = 'vehicle'
|
||||
newTable.category = mistCategory
|
||||
elseif string.upper(newTable.category) == 'AIRPLANE' then
|
||||
mistCategory = 'plane'
|
||||
newTable.category = mistCategory
|
||||
elseif string.upper(newTable.category) == 'HELICOPTER' then
|
||||
mistCategory = 'helicopter'
|
||||
newTable.category = mistCategory
|
||||
elseif string.upper(newTable.category) == 'SHIP' then
|
||||
mistCategory = 'ship'
|
||||
newTable.category = mistCategory
|
||||
end
|
||||
--dbLog:info('Update unitsBy')
|
||||
for newId, newUnitData in pairs(newTable.units) do
|
||||
--dbLog:info(newId)
|
||||
newUnitData.category = mistCategory
|
||||
if newUnitData.unitId then
|
||||
--dbLog:info('byId')
|
||||
mist.DBs.unitsById[tonumber(newUnitData.unitId)] = ldeepCopy(newUnitData)
|
||||
end
|
||||
--dbLog:info(updated)
|
||||
if mist.DBs.unitsByName[newUnitData.unitName] and updated == true then--if unit existed before and something was updated, write over the entry for a given unit name just in case.
|
||||
--dbLog:info('Updating Unit Tables')
|
||||
for i = 1, #mist.DBs.unitsByCat[mistCategory] do
|
||||
if mist.DBs.unitsByCat[mistCategory][i].unitName == newUnitData.unitName then
|
||||
--dbLog:info('Entry Found, Rewriting for unitsByCat')
|
||||
mist.DBs.unitsByCat[mistCategory][i] = ldeepCopy(newUnitData)
|
||||
break
|
||||
end
|
||||
end
|
||||
for i = 1, #mist.DBs.unitsByNum do
|
||||
if mist.DBs.unitsByNum[i].unitName == newUnitData.unitName then
|
||||
--dbLog:info('Entry Found, Rewriting for unitsByNum')
|
||||
mist.DBs.unitsByNum[i] = ldeepCopy(newUnitData)
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
--dbLog:info('Unitname not in use, add as normal')
|
||||
mist.DBs.unitsByCat[mistCategory][#mist.DBs.unitsByCat[mistCategory] + 1] = ldeepCopy(newUnitData)
|
||||
mist.DBs.unitsByNum[#mist.DBs.unitsByNum + 1] = ldeepCopy(newUnitData)
|
||||
end
|
||||
mist.DBs.unitsByName[newUnitData.unitName] = ldeepCopy(newUnitData)
|
||||
|
||||
|
||||
end
|
||||
-- this is a really annoying DB to populate. Gotta create new tables in case its missing
|
||||
--dbLog:info('write mist.DBs.units')
|
||||
if not mist.DBs.units[newTable.coalition] then
|
||||
mist.DBs.units[newTable.coalition] = {}
|
||||
end
|
||||
|
||||
if not mist.DBs.units[newTable.coalition][newTable.country] then
|
||||
mist.DBs.units[newTable.coalition][(newTable.country)] = {}
|
||||
mist.DBs.units[newTable.coalition][(newTable.country)].countryId = newTable.countryId
|
||||
end
|
||||
if not mist.DBs.units[newTable.coalition][newTable.country][mistCategory] then
|
||||
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] = {}
|
||||
end
|
||||
|
||||
if updated == true then
|
||||
--dbLog:info('Updating DBsUnits')
|
||||
for i = 1, #mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] do
|
||||
if mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][i].groupName == newTable.groupName then
|
||||
--dbLog:info('Entry Found, Rewriting')
|
||||
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][i] = ldeepCopy(newTable)
|
||||
break
|
||||
end
|
||||
end
|
||||
local res = writeDBTables(writeGroups[x])
|
||||
if res and res == true then
|
||||
--dbLog:info('result: complete')
|
||||
writeGroups[x] = nil
|
||||
else
|
||||
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][#mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] + 1] = ldeepCopy(newTable)
|
||||
end
|
||||
|
||||
|
||||
if newTable.groupId then
|
||||
mist.DBs.groupsById[newTable.groupId] = ldeepCopy(newTable)
|
||||
end
|
||||
|
||||
mist.DBs.groupsByName[newTable.name] = ldeepCopy(newTable)
|
||||
mist.DBs.dynGroupsAdded[#mist.DBs.dynGroupsAdded + 1] = ldeepCopy(newTable)
|
||||
|
||||
writeGroups[x] = nil
|
||||
if x%savesPerRun == 0 then
|
||||
coroutine.yield()
|
||||
writeGroups[x] = nil
|
||||
end
|
||||
end
|
||||
if x%savesPerRun == 0 then
|
||||
coroutine.yield()
|
||||
end
|
||||
if timer.getTime() > lastUpdateTime then
|
||||
lastUpdateTime = timer.getTime()
|
||||
end
|
||||
|
||||
--dbLog:info('endUpdateTables')
|
||||
end
|
||||
end
|
||||
|
||||
9306
mist_4_5_16.lua
Normal file
9306
mist_4_5_16.lua
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user