From 7c98a705a835fa4b323c5f53fa4c78fa9c74a896 Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Fri, 19 Aug 2016 23:10:01 +0200 Subject: [PATCH 1/5] Yet another protection fix --- factions.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/factions.lua b/factions.lua index 7930105..b679df0 100644 --- a/factions.lua +++ b/factions.lua @@ -389,9 +389,8 @@ function factions.Faction.is_online(self) end function factions.Faction.attack_parcel(self, parcelpos) - local attacked_faction = factions.parcels[parcelpos] + local attacked_faction = factions.get_parcel_faction(parcelpos) if attacked_faction then - attacked_faction = factions.factions[attacked_faction] self.power = self.power - factions.power_per_attack if attacked_faction.attacked_parcels[parcelpos] then attacked_faction.attacked_parcels[parcelpos][self.name] = true @@ -783,16 +782,16 @@ minetest.is_protected = function(pos, player) return not player_faction:has_permission(player, "claim") and player_faction.power > 0. and not parcel_faction.is_admin end -- no faction - if not parcel_faction or not player_faction then + if not parcel_faction then return default_is_protected(pos, player) - elseif not player_faction then - return true - else - if parcel_faction.name == player_faction then + elseif player_faction then + if parcel_faction.name == player_faction.name then return not parcel_faction:has_permission(player, "build") else return not parcel_faction:parcel_is_attacked_by(parcelpos, player_faction) end + else + return true end end From 296aeb5cc344b936f62e0773f38ba3ff57f3acfa Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Fri, 19 Aug 2016 23:10:34 +0200 Subject: [PATCH 2/5] Power required to attack is now 10 --- factions.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/factions.lua b/factions.lua index b679df0..94e047a 100644 --- a/factions.lua +++ b/factions.lua @@ -32,7 +32,7 @@ factions.power_per_parcel = .5 factions.power_per_death = .25 factions.power_per_tick = .125 factions.tick_time = 60. -factions.power_per_attack = 2. +factions.power_per_attack = 10. factions.faction_name_max_length = 50 factions.rank_name_max_length = 25 From 417bbeee60fcb7efab14fb82569235b38425291a Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Sat, 20 Aug 2016 00:12:40 +0200 Subject: [PATCH 3/5] Factions will now get disbanded after a week --- factions.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/factions.lua b/factions.lua index 94e047a..0281bf5 100644 --- a/factions.lua +++ b/factions.lua @@ -35,6 +35,7 @@ factions.tick_time = 60. factions.power_per_attack = 10. factions.faction_name_max_length = 50 factions.rank_name_max_length = 25 +factions.maximum_faction_inactivity = 604800 -- 1 week --------------------- --! @brief returns whether a faction can be created or not (allows for implementation of blacklists and the like) @@ -112,7 +113,9 @@ function factions.Faction:new(faction) --! @brief banner texture string banner = "bg_white.png", --! @brief gives certain privileges - is_admin = false + is_admin = false, + --! @brief last time anyone logged on + last_logon = os.time(), } or faction setmetatable(faction, self) return faction @@ -641,6 +644,9 @@ function factions.load() if #faction.name > factions.faction_name_max_length then faction:disband() end + if not faction.last_logon then + faction.last_logon = os.time() + end end file:close() end @@ -695,10 +701,14 @@ end factions.faction_tick = function() + local now = os.time() for facname, faction in pairs(factions.factions) do if faction:is_online() then faction:increase_power(factions.power_per_tick) end + if faction.last_logon > factions.maximum_faction_inactivity then + faction:disband() + end end end From a4f09c7d8e860eaf0af553483b500ca54f5c0dc1 Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Sat, 20 Aug 2016 00:15:45 +0200 Subject: [PATCH 4/5] Added possibility to explain a faction disband --- factions.lua | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/factions.lua b/factions.lua index 0281bf5..12b575d 100644 --- a/factions.lua +++ b/factions.lua @@ -234,14 +234,14 @@ function factions.Faction.unclaim_parcel(self, parcelpos) end --! @brief disband faction, updates global players and parcels table -function factions.Faction.disband(self) +function factions.Faction.disband(self, reason) for k, _ in pairs(self.players) do -- remove players affiliation factions.players[k] = nil end for k, v in pairs(self.land) do -- remove parcel claims factions.parcels[k] = nil end - self:on_disband() + self:on_disband(reason) factions.factions[self.name] = nil factions.save() end @@ -451,8 +451,12 @@ function factions.Faction.on_unclaim_parcel(self, pos) self:broadcast("Parcel ("..pos..") has been unclaimed.") end -function factions.Faction.on_disband(self, pos) - minetest.chat_send_all("Faction "..self.name.." has been disbanded.") +function factions.Faction.on_disband(self, reason) + local msg = "Faction "..self.name.." has been disbanded." + if reason then + msg = msg.." ("..reason..")" + end + minetest.chat_send_all(msg) end function factions.Faction.on_new_leader(self) From 4d53e4cc4017fc499bf9c966cd3a6de5cacbe0e8 Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Sat, 20 Aug 2016 00:47:35 +0200 Subject: [PATCH 5/5] Update code to use new API functions --- chatcommands.lua | 48 ++++++++++++++++++++++++------------------------ factions.lua | 2 +- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/chatcommands.lua b/chatcommands.lua index b4e0f91..e1ff654 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -54,7 +54,7 @@ factions.register_command = function(cmd_name, cmd) local argtype = self.format[i] local arg = argv[i] if argtype == "faction" then - local fac = factions.factions[arg] + local fac = factions.get_faction(arg) if not fac then send_error(player, "Specified faction "..arg.." does not exist") return false @@ -63,9 +63,8 @@ factions.register_command = function(cmd_name, cmd) end elseif argtype == "player" then local pl = minetest.get_player_by_name(arg) - if not pl then + if not pl or not factions.player[arg] then send_error(player, "Player is not online.") - --TODO: track existing players for offsync invites and the like return false else table.insert(args.players, pl) @@ -83,12 +82,11 @@ factions.register_command = function(cmd_name, cmd) end -- checks permissions - local player_faction = factions.players[player] + local player_faction = factions.get_player_faction(player) if self.infaction and not player_faction then minetest.chat_send_player(player, "This command is only available within a faction.") return false end - player_faction = factions.factions[player_faction] if self.faction_permissions then for i in ipairs(self.faction_permissions) do if not player_faction:has_permission(player, self.faction_permissions[i]) then @@ -166,11 +164,11 @@ factions.register_command ("claim", { faction:claim_parcel(parcelpos) return true else - local parcel = factions.parcels[parcelpos] - if not parcel then + local parcel_faction = factions.get_parcel_faction(parcelpos) + if not parcel_faction then send_error(player, "You faction cannot claim any (more) parcel(s).") return false - elseif parcel == faction.name then + elseif parcel_faction.name == faction.name then send_error(player, "This parcel already belongs to your faction.") return false else @@ -185,8 +183,8 @@ factions.register_command("unclaim", { faction_permissions = {"claim"}, description = "Unclaim the plot of land you're on.", on_success = function(player, faction, pos, parcelpos, args) - local parcel = factions.parcels[parcelpos] - if parcel ~= faction.name then + local parcel_faction = factions.get_parcel_faction(parcelpos) + if parcel_faction.name ~= faction.name then send_error(player, "This parcel does not belong to you.") return false else @@ -250,12 +248,15 @@ factions.register_command("kick", { description = "Kick a player from your faction.", on_success = function(player, faction, pos, parcelpos, args) local victim = args.players[1] - if factions.players[victim:get_player_name()] == faction.name - and victim:get_player_name() ~= faction.leader then -- can't kick da king + local victim_faction = factions.get_player_faction(victim:get_player_name()) + if victim_faction and victim:get_player_name() ~= faction.leader then -- can't kick da king faction:remove_player(player) return true + elseif not victim_faction then + send_error(player, victim:get_player_name().." is not in your faction.") + return false else - send_error(player, "Cannot kick player "..victim:get_player_name()) + send_error(player, victim:get_player_name().." cannot be kicked from your faction.") return false end end @@ -344,7 +345,6 @@ factions.register_command("invite", { description = "Invite a player to your faction.", on_success = function(player, faction, pos, parcelpos, args) faction:invite_player(args.players[1]:get_player_name()) - --TODO: message return true end }) @@ -355,7 +355,6 @@ factions.register_command("uninvite", { description = "Revoke a player's invite.", on_success = function(player, faction, pos, parcelpos, args) faction:revoke_invite(args.players[1]:get_player_name()) - --TODO: message return true end }) @@ -367,7 +366,6 @@ factions.register_command("delete", { description = "Delete a faction.", on_success = function(player, faction, pos, parcelpos, args) args.factions[1]:disband() - --TODO: message return true end }) @@ -445,8 +443,9 @@ factions.register_command("where", { description = "See whose parcel you stand on.", infaction = false, on_success = function(player, faction, pos, parcelpos, args) - local parcel = factions.parcels[parcelpos] - minetest.chat_send_player(player, "You are standing on parcel "..parcelpos..", part of "..(parcel or "Wilderness")) + local parcel_faction = factions.get_parcel_faction(parcelpos) + local place_name = (parcel_faction and parcel_faction.name) or "Wilderness" + minetest.chat_send_player(player, "You are standing on parcel "..parcelpos..", part of "..place_name) return true end }) @@ -531,13 +530,14 @@ factions.register_command("free", { infaction = false, global_privileges = {"faction_admin"}, on_success = function(player, faction, pos, parcelpos, args) - local fac = factions.parcels[parcelpos] - if not fac then + local parcel_faction = factions.get_parcel_faction(parcelpos) + if not parcel_faction then send_error(player, "No claim at this position") return false + else + parcel_faction:unclaim_parcel(parcelpos) + return true end - faction:unclaim_parcel(parcelpos) - return true end }) @@ -571,12 +571,12 @@ factions_chat.cmdhandler = function (playername,parameter) local player = minetest.env:get_player_by_name(playername) local params = parameter:split(" ") - local player_faction = factions.players[playername] + local player_faction = factions.get_player_faction(playername) if parameter == nil or parameter == "" then if player_faction then - minetest.chat_send_player(playername, "You are in faction "..player_faction..". Type /f help for a list of commands.") + minetest.chat_send_player(playername, "You are in faction "..player_faction.name..". Type /f help for a list of commands.") else minetest.chat_send_player(playername, "You are part of no faction") end diff --git a/factions.lua b/factions.lua index 12b575d..940f100 100644 --- a/factions.lua +++ b/factions.lua @@ -710,7 +710,7 @@ factions.faction_tick = function() if faction:is_online() then faction:increase_power(factions.power_per_tick) end - if faction.last_logon > factions.maximum_faction_inactivity then + if faction.last_logon - now > factions.maximum_faction_inactivity then faction:disband() end end