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:
mrSkortch 2023-01-12 18:12:41 -07:00
parent 0c37aaaa09
commit be5023e2fd
2 changed files with 9486 additions and 111 deletions

147
mist.lua
View File

@ -35,7 +35,7 @@ mist = {}
-- don't change these -- don't change these
mist.majorVersion = 4 mist.majorVersion = 4
mist.minorVersion = 5 mist.minorVersion = 5
mist.build = 115 mist.build = 116
-- forward declaration of log shorthand -- forward declaration of log shorthand
local log local log
@ -746,17 +746,21 @@ do -- the main scope
mist.DBs.groupsByName[group_data.groupName] = mist.utils.deepCopy(group_data) mist.DBs.groupsByName[group_data.groupName] = mist.utils.deepCopy(group_data)
mist.DBs.groupsById[group_data.groupId] = 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 for unit_ind, unit_data in pairs(group_data.units) do
mist.DBs.unitsByName[unit_data.unitName] = mist.utils.deepCopy(unit_data) local copy = mist.utils.deepCopy(unit_data)
mist.DBs.unitsById[unit_data.unitId] = 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... 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) --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 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.humansByName[unit_data.unitName] = mist.utils.deepCopy(copy)
mist.DBs.humansById[unit_data.unitId] = mist.utils.deepCopy(unit_data) mist.DBs.humansById[unit_data.unitId] = mist.utils.deepCopy(copy)
--if Unit.getByName(unit_data.unitName) then --if Unit.getByName(unit_data.unitName) then
-- mist.DBs.activeHumans[unit_data.unitName] = mist.utils.deepCopy(unit_data) -- mist.DBs.activeHumans[unit_data.unitName] = mist.utils.deepCopy(unit_data)
-- mist.DBs.activeHumans[unit_data.unitName].playerName = Unit.getByName(unit_data.unitName):getPlayerName() -- mist.DBs.activeHumans[unit_data.unitName].playerName = Unit.getByName(unit_data.unitName):getPlayerName()
@ -901,9 +905,10 @@ do -- the main scope
end end
end end
local function dbUpdate(event, objType, origGroupName) local function dbUpdate(event, oType, origGroupName)
--dbLog:info('dbUpdate') --dbLog:info('dbUpdate: $1', event)
local newTable = {} local newTable = {}
local objType = oType
newTable.startTime = 0 newTable.startTime = 0
if type(event) == 'string' then -- if name of an object. if type(event) == 'string' then -- if name of an object.
local newObject local newObject
@ -911,6 +916,7 @@ do -- the main scope
newObject = Group.getByName(event) newObject = Group.getByName(event)
elseif StaticObject.getByName(event) then elseif StaticObject.getByName(event) then
newObject = StaticObject.getByName(event) newObject = StaticObject.getByName(event)
objType = "static"
-- log:info('its static') -- log:info('its static')
else else
log:warn('$1 is not a Group or Static Object. This should not be possible. Sent category is: $2', event, objType) 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 = {} newTable.units = {}
if objType == 'group' then if objType == 'group' then
for unitId, unitData in pairs(unitOneRef) do for unitId, unitData in pairs(unitOneRef) do
local point = unitData:getPoint()
newTable.units[unitId] = {} newTable.units[unitId] = {}
newTable.units[unitId].unitName = unitData:getName() newTable.units[unitId].unitName = unitData:getName()
newTable.units[unitId].x = mist.utils.round(unitData:getPosition().p.x) newTable.units[unitId].x = mist.utils.round(point.x)
newTable.units[unitId].y = mist.utils.round(unitData:getPosition().p.z) newTable.units[unitId].y = mist.utils.round(point.z)
newTable.units[unitId].point = {} newTable.units[unitId].point = {}
newTable.units[unitId].point.x = newTable.units[unitId].x newTable.units[unitId].point.x = newTable.units[unitId].x
newTable.units[unitId].point.y = newTable.units[unitId].y 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].speed = mist.vec.mag(unitData:getVelocity())
newTable.units[unitId].heading = mist.getHeading(unitData, true) newTable.units[unitId].heading = mist.getHeading(unitData, true)
@ -1028,15 +1035,16 @@ do -- the main scope
end end
else -- its a static else -- its a static
newTable.category = 'static' newTable.category = 'static'
local point = newObject:getPoint()
newTable.units[1] = {} newTable.units[1] = {}
newTable.units[1].unitName = newObject:getName() newTable.units[1].unitName = newObject:getName()
newTable.units[1].category = 'static' newTable.units[1].category = 'static'
newTable.units[1].x = mist.utils.round(newObject:getPosition().p.x) newTable.units[1].x = mist.utils.round(point.x)
newTable.units[1].y = mist.utils.round(newObject:getPosition().p.z) newTable.units[1].y = mist.utils.round(point.z)
newTable.units[1].point = {} newTable.units[1].point = {}
newTable.units[1].point.x = newTable.units[1].x newTable.units[1].point.x = newTable.units[1].x
newTable.units[1].point.y = newTable.units[1].y 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].heading = mist.getHeading(newObject, true)
newTable.units[1].type = newObject:getTypeName() newTable.units[1].type = newObject:getTypeName()
newTable.units[1].unitId = tonumber(newObject:getID()) newTable.units[1].unitId = tonumber(newObject:getID())
@ -1181,21 +1189,29 @@ do -- the main scope
end end
end end
local function updateDBTables() local updateChecker = {}
local i = #writeGroups
local savesPerRun = math.ceil(i/10)
if savesPerRun < 5 then local function writeDBTables(newEntry)
savesPerRun = 5
end
if i > 0 then
-- dbLog:info('updateDBTables')
local ldeepCopy = mist.utils.deepCopy local ldeepCopy = mist.utils.deepCopy
for x = 1, i do local newTable = newEntry.data
-- dbLog:info(writeGroups[x]) --dbLog:info(newTable)
local newTable = writeGroups[x].data
local updated = writeGroups[x].isUpdated 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 local mistCategory
--dbLog:info('define categoryy')
if type(newTable.category) == 'string' then if type(newTable.category) == 'string' then
mistCategory = string.lower(newTable.category) mistCategory = string.lower(newTable.category)
end end
@ -1214,16 +1230,16 @@ do -- the main scope
newTable.category = mistCategory newTable.category = mistCategory
end end
--dbLog:info('Update unitsBy') --dbLog:info('Update unitsBy')
state = 1
for newId, newUnitData in pairs(newTable.units) do for newId, newUnitData in pairs(newTable.units) do
--dbLog:info(newId) --dbLog:info(newId)
newUnitData.category = mistCategory newUnitData.category = mistCategory
if newUnitData.unitId then
--dbLog:info('byId')
mist.DBs.unitsById[tonumber(newUnitData.unitId)] = ldeepCopy(newUnitData)
end
--dbLog:info(updated) --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. 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') --dbLog:info('Updating Unit Tables')
local refNum = mist.DBs.unitsByName[newUnitData.unitName].dbNum
for i = 1, #mist.DBs.unitsByCat[mistCategory] do for i = 1, #mist.DBs.unitsByCat[mistCategory] do
if mist.DBs.unitsByCat[mistCategory][i].unitName == newUnitData.unitName then if mist.DBs.unitsByCat[mistCategory][i].unitName == newUnitData.unitName then
--dbLog:info('Entry Found, Rewriting for unitsByCat') --dbLog:info('Entry Found, Rewriting for unitsByCat')
@ -1231,6 +1247,13 @@ do -- the main scope
break break
end end
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 for i = 1, #mist.DBs.unitsByNum do
if mist.DBs.unitsByNum[i].unitName == newUnitData.unitName then if mist.DBs.unitsByNum[i].unitName == newUnitData.unitName then
--dbLog:info('Entry Found, Rewriting for unitsByNum') --dbLog:info('Entry Found, Rewriting for unitsByNum')
@ -1238,30 +1261,36 @@ do -- the main scope
break break
end end
end end
end
else else
state = 1.3
--dbLog:info('Unitname not in use, add as normal') --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.unitsByCat[mistCategory][#mist.DBs.unitsByCat[mistCategory] + 1] = ldeepCopy(newUnitData)
mist.DBs.unitsByNum[#mist.DBs.unitsByNum + 1] = ldeepCopy(newUnitData) mist.DBs.unitsByNum[#mist.DBs.unitsByNum + 1] = ldeepCopy(newUnitData)
end end
if newUnitData.unitId then
--dbLog:info('byId')
mist.DBs.unitsById[tonumber(newUnitData.unitId)] = ldeepCopy(newUnitData)
end
mist.DBs.unitsByName[newUnitData.unitName] = ldeepCopy(newUnitData) mist.DBs.unitsByName[newUnitData.unitName] = ldeepCopy(newUnitData)
end end
-- this is a really annoying DB to populate. Gotta create new tables in case its missing -- this is a really annoying DB to populate. Gotta create new tables in case its missing
--dbLog:info('write mist.DBs.units') --dbLog:info('write mist.DBs.units')
state = 2
if not mist.DBs.units[newTable.coalition] then if not mist.DBs.units[newTable.coalition] then
mist.DBs.units[newTable.coalition] = {} mist.DBs.units[newTable.coalition] = {}
end end
state = 3
if not mist.DBs.units[newTable.coalition][newTable.country] then if not mist.DBs.units[newTable.coalition][newTable.country] then
mist.DBs.units[newTable.coalition][(newTable.country)] = {} mist.DBs.units[newTable.coalition][(newTable.country)] = {}
mist.DBs.units[newTable.coalition][(newTable.country)].countryId = newTable.countryId mist.DBs.units[newTable.coalition][(newTable.country)].countryId = newTable.countryId
end end
state = 4
if not mist.DBs.units[newTable.coalition][newTable.country][mistCategory] then if not mist.DBs.units[newTable.coalition][newTable.country][mistCategory] then
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] = {} mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] = {}
end end
state = 5
if updated == true then if updated == true then
--dbLog:info('Updating DBsUnits') --dbLog:info('Updating DBsUnits')
for i = 1, #mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] do for i = 1, #mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] do
@ -1272,25 +1301,65 @@ do -- the main scope
end end
end end
else 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) mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][#mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] + 1] = ldeepCopy(newTable)
end end
state = 6
if newTable.groupId then if newTable.groupId then
--dbLog:info('Make groupsById')
mist.DBs.groupsById[newTable.groupId] = ldeepCopy(newTable) mist.DBs.groupsById[newTable.groupId] = ldeepCopy(newTable)
end end
--dbLog:info('make groupsByName')
mist.DBs.groupsByName[newTable.name] = ldeepCopy(newTable) mist.DBs.groupsByName[newTable.name] = ldeepCopy(newTable)
--dbLog:info('add to dynGroups')
mist.DBs.dynGroupsAdded[#mist.DBs.dynGroupsAdded + 1] = ldeepCopy(newTable) 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
local savesPerRun = math.ceil(i/10)
if savesPerRun < 5 then
savesPerRun = 5
end
if i > 0 then
--dbLog:info('updateDBTables: $1', #writeGroups)
for x = 1, i do
local res = writeDBTables(writeGroups[x])
if res and res == true then
--dbLog:info('result: complete')
writeGroups[x] = nil writeGroups[x] = nil
else
writeGroups[x] = nil
end
end
if x%savesPerRun == 0 then if x%savesPerRun == 0 then
coroutine.yield() coroutine.yield()
end end
end
if timer.getTime() > lastUpdateTime then if timer.getTime() > lastUpdateTime then
lastUpdateTime = timer.getTime() lastUpdateTime = timer.getTime()
end end
--dbLog:info('endUpdateTables') --dbLog:info('endUpdateTables')
end end
end end

9306
mist_4_5_16.lua Normal file

File diff suppressed because it is too large Load Diff