Remove: whole keys only if empty

This commit is contained in:
Coder12a 2019-06-28 15:43:28 -05:00
parent db914eec5a
commit d00c120cd5
3 changed files with 32 additions and 5 deletions

View File

@ -52,7 +52,7 @@ end
--! @brief claim a parcel, update power and update global parcels table
function factions.unclaim_parcel(name, parcelpos)
factions.parcels.remove(parcelpos)
factions.remove_key(factions.parcels, parcelpos, nil, "faction", true)
local faction = factions.factions.get(name)

View File

@ -99,7 +99,7 @@ end
-- Create a empty player table.
function factions.create_player_table()
local table = {
faction = ""
faction = "",
}
return factions.on_create_player_table(table)
end
@ -111,3 +111,30 @@ function factions.create_claim_table()
}
return factions.on_create_claim_table(table)
end
-- helper functions
function factions.db_is_empty(table)
for k, v in pairs(table) do
return false
end
return true
end
function factions.remove_key(db, db_name, db_data, key, write)
if not db_data then
db_data = db.get(db_name)
end
db_data[key] = nil
if factions.db_is_empty(db_data) then
db.remove(db_name)
return nil
end
if write then
db.set(db_name, db_data)
end
return db_data
end

View File

@ -220,7 +220,7 @@ function factions.remove_player(name, player)
factions.factions.set(name, faction)
factions.players.remove(player)
factions.remove_key(factions.players, player, nil, "faction", true)
factions.on_player_leave(name, player)
if factions_config.enable_power_per_player then
@ -281,11 +281,11 @@ function factions.disband(name, reason)
end
for k, _ in pairs(faction.players) do -- remove players affiliation
factions.players.remove(k)
factions.remove_key(factions.players, k, nil, "faction", true)
end
for k, v in pairs(faction.land) do -- remove parcel claims
factions.parcels.remove(k)
factions.remove_key(factions.parcels, k, nil, "faction", true)
end
factions.on_disband(name, reason)