From db914eec5a60e6561f6c2cb14956fe141002d0dd Mon Sep 17 00:00:00 2001 From: Coder12a <38924418+Coder12a@users.noreply.github.com> Date: Fri, 28 Jun 2019 13:28:12 -0500 Subject: [PATCH] Allow: players, claims, and ips to store more data --- claim_events.lua | 23 ++++++----- databases.lua | 100 ++++++++++++++++++++++++++++++++++++++++++++++ factions.lua | 61 +++------------------------- player_events.lua | 11 +++-- 4 files changed, 127 insertions(+), 68 deletions(-) diff --git a/claim_events.lua b/claim_events.lua index f3cbda0..c225af7 100644 --- a/claim_events.lua +++ b/claim_events.lua @@ -10,7 +10,7 @@ function factions.can_claim_parcel(name, parcelpos) local faction = factions.factions.get(name) if fn then - local fac = factions.factions.get(fn) + local fac = factions.factions.get(fn.faction) if fac.power < 0. and faction.power >= factions_config.power_per_parcel and not faction.allies[fn] and not faction.neutral[fn] then return true @@ -27,12 +27,16 @@ end --! @brief claim a parcel, update power and update global parcels table function factions.claim_parcel(name, parcelpos) -- check if claiming over other faction's territory - local otherfac = factions.parcels.get(parcelpos) - if otherfac then - factions.unclaim_parcel(otherfac, parcelpos) - factions.parcelless_check(otherfac) - end - factions.parcels.set(parcelpos, name) + local otherfac = factions.parcels.get(parcelpos) + + if otherfac then + local otherfac_name = otherfac.faction + factions.unclaim_parcel(otherfac_name, parcelpos) + factions.parcelless_check(otherfac_name) + end + local data = factions.create_claim_table() + data.faction = name + factions.parcels.set(parcelpos, data) local faction = factions.factions.get(name) @@ -85,8 +89,9 @@ function factions.parcelless_check(name) end function factions.get_parcel_faction(parcelpos) - local facname = factions.parcels.get(parcelpos) - if facname then + local data = factions.parcels.get(parcelpos) + if data then + local facname = data.faction local faction = factions.factions.get(facname) return faction, facname end diff --git a/databases.lua b/databases.lua index 5dfc1c0..e40b478 100644 --- a/databases.lua +++ b/databases.lua @@ -11,3 +11,103 @@ factions.player_ips = factions.root.sub_database("ips") -- Memory only storage. factions.onlineplayers = {} + +-- Table hook functions. + +-- Hook function to add or delete from the faction table. +function factions.on_create_faction_table(table) + return table +end + +-- Hook function to add or delete from the ip table. +function factions.on_create_ip_table(table) + return table +end + +-- Hook function to add or delete from the player table. +function factions.on_create_player_table(table) + return table +end + +-- Hook function to add or delete from the claim table. +function factions.on_create_claim_table(table) + return table +end + +-- Table creation. + +-- Create a empty faction. +function factions.create_faction_table() + local table = { + name = "", + --! @brief power of a faction (needed for parcel claiming) + power = factions_config.power, + --! @brief maximum power of a faction + maxpower = factions_config.maxpower, + --! @brief power currently in use + usedpower = 0., + --! @brief list of player names + players = {}, + --! @brief table of ranks/permissions + ranks = starting_ranks, + --! @brief name of the leader + leader = nil, + --! @brief spawn of the faction + spawn = {x = 0, y = 0, z = 0}, + --! @brief default joining rank for new members + default_rank = "member", + --! @brief default rank assigned to the leader + default_leader_rank = "leader", + --! @brief faction's description string + description = "Default faction description.", + --! @brief faction's message of the day. + message_of_the_day = "", + --! @brief list of players currently invited (can join with /f join) + invited_players = {}, + --! @brief table of claimed parcels (keys are parcelpos strings) + land = {}, + --! @brief table of allies + allies = {}, + -- + request_inbox = {}, + --! @brief table of enemies + enemies = {}, + --! + neutral = {}, + --! @brief table of parcels/factions that are under attack + attacked_parcels = {}, + --! @brief whether faction is closed or open (boolean) + join_free = false, + --! @brief gives certain privileges + is_admin = false, + --! @brief last time anyone logged on + last_logon = os.time(), + --! @brief how long this has been without parcels + no_parcel = os.time(), + } + return factions.on_create_faction_table(table) +end + +-- Create a empty ip table. +function factions.create_ip_table() + local table = { + ip = "" + } + return factions.on_create_ip_table(table) +end + +-- Create a empty player table. +function factions.create_player_table() + local table = { + faction = "" + } + return factions.on_create_player_table(table) +end + +-- Create a empty claim table. +function factions.create_claim_table() + local table = { + faction = "" + } + return factions.on_create_claim_table(table) +end diff --git a/factions.lua b/factions.lua index 7e982e5..0542277 100644 --- a/factions.lua +++ b/factions.lua @@ -70,60 +70,9 @@ if factions_config.faction_diplomacy == true then starting_ranks["leader"] = lt end -function factions.new() - return { - name = "", - --! @brief power of a faction (needed for parcel claiming) - power = factions_config.power, - --! @brief maximum power of a faction - maxpower = factions_config.maxpower, - --! @brief power currently in use - usedpower = 0., - --! @brief list of player names - players = {}, - --! @brief table of ranks/permissions - ranks = starting_ranks, - --! @brief name of the leader - leader = nil, - --! @brief spawn of the faction - spawn = {x=0, y=0, z=0}, - --! @brief default joining rank for new members - default_rank = "member", - --! @brief default rank assigned to the leader - default_leader_rank = "leader", - --! @brief faction's description string - description = "Default faction description.", - --! @brief faction's message of the day. - message_of_the_day = "", - --! @brief list of players currently invited (can join with /f join) - invited_players = {}, - --! @brief table of claimed parcels (keys are parcelpos strings) - land = {}, - --! @brief table of allies - allies = {}, - -- - request_inbox = {}, - --! @brief table of enemies - enemies = {}, - --! - neutral = {}, - --! @brief table of parcels/factions that are under attack - attacked_parcels = {}, - --! @brief whether faction is closed or open (boolean) - join_free = false, - --! @brief gives certain privileges - is_admin = false, - --! @brief last time anyone logged on - last_logon = os.time(), - --! @brief how long this has been without parcels - no_parcel = os.time(), - } -end - - --! @brief create a new empty faction function factions.new_faction(name) - local faction = factions.new() + local faction = factions.create_faction_table() faction.name = name factions.factions.set(name, faction) @@ -172,11 +121,13 @@ function factions.set_name(oldname, name) end for parcel in pairs(faction.land) do - factions.parcels.set(parcel, name) + local data = factions.create_claim_table() + data.faction = name + factions.parcels.set(parcel, data) end for playername in pairs(faction.players) do - local data = factions.players.get(playername) or {} + local data = factions.players.get(playername) or factions.create_player_table() data.faction = name factions.players.set(playername, data) end @@ -231,7 +182,7 @@ function factions.add_player(name, player, rank) faction.players[player] = rank or faction.default_rank - local data = factions.players.get(player) or {} + local data = factions.players.get(player) or factions.create_player_table() data.faction = name factions.players.set(player, data) diff --git a/player_events.lua b/player_events.lua index a81c69d..dac592a 100644 --- a/player_events.lua +++ b/player_events.lua @@ -1,7 +1,9 @@ local on_death = {} minetest.register_on_prejoinplayer(function(name, ip) - factions.player_ips.set(name, ip) + local data = factions.create_ip_table() + data.ip = ip + factions.player_ips.set(name, data) end) minetest.register_on_joinplayer(function(player) @@ -49,18 +51,19 @@ minetest.register_on_leaveplayer(function(player) end if faction then + faction.last_logon = os.time() + factions.factions.set(facname, faction) factions.onlineplayers[facname][name] = nil local id_name2 = name .. "factionName" local id_name3 = name .. "powerWatch" + if hud_ids[id_name2] then hud_ids[id_name2] = nil end if hud_ids[id_name3] then hud_ids[id_name3] = nil end - for k, v in pairs(factions.onlineplayers[facname]) do - return - end + factions.onlineplayers[facname] = nil on_death[name] = nil end