diff --git a/chatcommands.lua b/chatcommands.lua index 4a4cade..0285afc 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,21 @@ factions.register_command("kick", { description = "Kick a player from your faction.", on_success = function(player, faction, pos, parcelpos, args) local victim = args.players[1] +<<<<<<< HEAD if factions.players[victim:get_player_name()] == faction.name and victim:get_player_name() ~= faction.leader then -- can't kick da king faction:remove_player(victim) +======= + 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) +>>>>>>> 4d53e4cc4017fc499bf9c966cd3a6de5cacbe0e8 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 +351,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 +361,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 +372,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 +449,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 +536,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 +577,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 06f63cc..1adaf31 100644 --- a/factions.lua +++ b/factions.lua @@ -32,9 +32,10 @@ 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 +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 @@ -231,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 @@ -389,9 +392,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 @@ -449,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) @@ -642,6 +648,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 @@ -696,10 +705,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 - now > factions.maximum_faction_inactivity then + faction:disband() + end end end @@ -783,18 +796,27 @@ 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) +<<<<<<< HEAD elseif not player_faction then return true else if not not player_faction then +======= + elseif player_faction then +>>>>>>> 4d53e4cc4017fc499bf9c966cd3a6de5cacbe0e8 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 +<<<<<<< HEAD end +======= + else + return true +>>>>>>> 4d53e4cc4017fc499bf9c966cd3a6de5cacbe0e8 end end