Merge branch 'master' of https://github.com/shamoanjac/factions into shamoanjac-master

Test
This commit is contained in:
Anthony Recascino 2016-08-19 22:41:53 -04:00
commit 9a38f54caa
2 changed files with 59 additions and 31 deletions

View File

@ -54,7 +54,7 @@ factions.register_command = function(cmd_name, cmd)
local argtype = self.format[i] local argtype = self.format[i]
local arg = argv[i] local arg = argv[i]
if argtype == "faction" then if argtype == "faction" then
local fac = factions.factions[arg] local fac = factions.get_faction(arg)
if not fac then if not fac then
send_error(player, "Specified faction "..arg.." does not exist") send_error(player, "Specified faction "..arg.." does not exist")
return false return false
@ -63,9 +63,8 @@ factions.register_command = function(cmd_name, cmd)
end end
elseif argtype == "player" then elseif argtype == "player" then
local pl = minetest.get_player_by_name(arg) 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.") send_error(player, "Player is not online.")
--TODO: track existing players for offsync invites and the like
return false return false
else else
table.insert(args.players, pl) table.insert(args.players, pl)
@ -83,12 +82,11 @@ factions.register_command = function(cmd_name, cmd)
end end
-- checks permissions -- checks permissions
local player_faction = factions.players[player] local player_faction = factions.get_player_faction(player)
if self.infaction and not player_faction then if self.infaction and not player_faction then
minetest.chat_send_player(player, "This command is only available within a faction.") minetest.chat_send_player(player, "This command is only available within a faction.")
return false return false
end end
player_faction = factions.factions[player_faction]
if self.faction_permissions then if self.faction_permissions then
for i in ipairs(self.faction_permissions) do for i in ipairs(self.faction_permissions) do
if not player_faction:has_permission(player, self.faction_permissions[i]) then if not player_faction:has_permission(player, self.faction_permissions[i]) then
@ -166,11 +164,11 @@ factions.register_command ("claim", {
faction:claim_parcel(parcelpos) faction:claim_parcel(parcelpos)
return true return true
else else
local parcel = factions.parcels[parcelpos] local parcel_faction = factions.get_parcel_faction(parcelpos)
if not parcel then if not parcel_faction then
send_error(player, "You faction cannot claim any (more) parcel(s).") send_error(player, "You faction cannot claim any (more) parcel(s).")
return false return false
elseif parcel == faction.name then elseif parcel_faction.name == faction.name then
send_error(player, "This parcel already belongs to your faction.") send_error(player, "This parcel already belongs to your faction.")
return false return false
else else
@ -185,8 +183,8 @@ factions.register_command("unclaim", {
faction_permissions = {"claim"}, faction_permissions = {"claim"},
description = "Unclaim the plot of land you're on.", description = "Unclaim the plot of land you're on.",
on_success = function(player, faction, pos, parcelpos, args) on_success = function(player, faction, pos, parcelpos, args)
local parcel = factions.parcels[parcelpos] local parcel_faction = factions.get_parcel_faction(parcelpos)
if parcel ~= faction.name then if parcel_faction.name ~= faction.name then
send_error(player, "This parcel does not belong to you.") send_error(player, "This parcel does not belong to you.")
return false return false
else else
@ -250,12 +248,21 @@ factions.register_command("kick", {
description = "Kick a player from your faction.", description = "Kick a player from your faction.",
on_success = function(player, faction, pos, parcelpos, args) on_success = function(player, faction, pos, parcelpos, args)
local victim = args.players[1] local victim = args.players[1]
<<<<<<< HEAD
if factions.players[victim:get_player_name()] == faction.name if factions.players[victim:get_player_name()] == faction.name
and victim:get_player_name() ~= faction.leader then -- can't kick da king and victim:get_player_name() ~= faction.leader then -- can't kick da king
faction:remove_player(victim) 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 return true
elseif not victim_faction then
send_error(player, victim:get_player_name().." is not in your faction.")
return false
else 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 return false
end end
end end
@ -344,7 +351,6 @@ factions.register_command("invite", {
description = "Invite a player to your faction.", description = "Invite a player to your faction.",
on_success = function(player, faction, pos, parcelpos, args) on_success = function(player, faction, pos, parcelpos, args)
faction:invite_player(args.players[1]:get_player_name()) faction:invite_player(args.players[1]:get_player_name())
--TODO: message
return true return true
end end
}) })
@ -355,7 +361,6 @@ factions.register_command("uninvite", {
description = "Revoke a player's invite.", description = "Revoke a player's invite.",
on_success = function(player, faction, pos, parcelpos, args) on_success = function(player, faction, pos, parcelpos, args)
faction:revoke_invite(args.players[1]:get_player_name()) faction:revoke_invite(args.players[1]:get_player_name())
--TODO: message
return true return true
end end
}) })
@ -367,7 +372,6 @@ factions.register_command("delete", {
description = "Delete a faction.", description = "Delete a faction.",
on_success = function(player, faction, pos, parcelpos, args) on_success = function(player, faction, pos, parcelpos, args)
args.factions[1]:disband() args.factions[1]:disband()
--TODO: message
return true return true
end end
}) })
@ -445,8 +449,9 @@ factions.register_command("where", {
description = "See whose parcel you stand on.", description = "See whose parcel you stand on.",
infaction = false, infaction = false,
on_success = function(player, faction, pos, parcelpos, args) on_success = function(player, faction, pos, parcelpos, args)
local parcel = factions.parcels[parcelpos] local parcel_faction = factions.get_parcel_faction(parcelpos)
minetest.chat_send_player(player, "You are standing on parcel "..parcelpos..", part of "..(parcel or "Wilderness")) 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 return true
end end
}) })
@ -531,14 +536,15 @@ factions.register_command("free", {
infaction = false, infaction = false,
global_privileges = {"faction_admin"}, global_privileges = {"faction_admin"},
on_success = function(player, faction, pos, parcelpos, args) on_success = function(player, faction, pos, parcelpos, args)
local fac = factions.parcels[parcelpos] local parcel_faction = factions.get_parcel_faction(parcelpos)
if not fac then if not parcel_faction then
send_error(player, "No claim at this position") send_error(player, "No claim at this position")
return false return false
end else
faction:unclaim_parcel(parcelpos) parcel_faction:unclaim_parcel(parcelpos)
return true return true
end end
end
}) })
factions.register_command("chat", { factions.register_command("chat", {
@ -571,12 +577,12 @@ factions_chat.cmdhandler = function (playername,parameter)
local player = minetest.env:get_player_by_name(playername) local player = minetest.env:get_player_by_name(playername)
local params = parameter:split(" ") local params = parameter:split(" ")
local player_faction = factions.players[playername] local player_faction = factions.get_player_faction(playername)
if parameter == nil or if parameter == nil or
parameter == "" then parameter == "" then
if player_faction 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 else
minetest.chat_send_player(playername, "You are part of no faction") minetest.chat_send_player(playername, "You are part of no faction")
end end

View File

@ -32,9 +32,10 @@ factions.power_per_parcel = .5
factions.power_per_death = .25 factions.power_per_death = .25
factions.power_per_tick = .125 factions.power_per_tick = .125
factions.tick_time = 60. factions.tick_time = 60.
factions.power_per_attack = 2. factions.power_per_attack = 10.
factions.faction_name_max_length = 50 factions.faction_name_max_length = 50
factions.rank_name_max_length = 25 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) --! @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 --! @brief banner texture string
banner = "bg_white.png", banner = "bg_white.png",
--! @brief gives certain privileges --! @brief gives certain privileges
is_admin = false is_admin = false,
--! @brief last time anyone logged on
last_logon = os.time(),
} or faction } or faction
setmetatable(faction, self) setmetatable(faction, self)
return faction return faction
@ -231,14 +234,14 @@ function factions.Faction.unclaim_parcel(self, parcelpos)
end end
--! @brief disband faction, updates global players and parcels table --! @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 for k, _ in pairs(self.players) do -- remove players affiliation
factions.players[k] = nil factions.players[k] = nil
end end
for k, v in pairs(self.land) do -- remove parcel claims for k, v in pairs(self.land) do -- remove parcel claims
factions.parcels[k] = nil factions.parcels[k] = nil
end end
self:on_disband() self:on_disband(reason)
factions.factions[self.name] = nil factions.factions[self.name] = nil
factions.save() factions.save()
end end
@ -389,9 +392,8 @@ function factions.Faction.is_online(self)
end end
function factions.Faction.attack_parcel(self, parcelpos) 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 if attacked_faction then
attacked_faction = factions.factions[attacked_faction]
self.power = self.power - factions.power_per_attack self.power = self.power - factions.power_per_attack
if attacked_faction.attacked_parcels[parcelpos] then if attacked_faction.attacked_parcels[parcelpos] then
attacked_faction.attacked_parcels[parcelpos][self.name] = true 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.") self:broadcast("Parcel ("..pos..") has been unclaimed.")
end end
function factions.Faction.on_disband(self, pos) function factions.Faction.on_disband(self, reason)
minetest.chat_send_all("Faction "..self.name.." has been disbanded.") local msg = "Faction "..self.name.." has been disbanded."
if reason then
msg = msg.." ("..reason..")"
end
minetest.chat_send_all(msg)
end end
function factions.Faction.on_new_leader(self) function factions.Faction.on_new_leader(self)
@ -642,6 +648,9 @@ function factions.load()
if #faction.name > factions.faction_name_max_length then if #faction.name > factions.faction_name_max_length then
faction:disband() faction:disband()
end end
if not faction.last_logon then
faction.last_logon = os.time()
end
end end
file:close() file:close()
end end
@ -696,10 +705,14 @@ end
factions.faction_tick = function() factions.faction_tick = function()
local now = os.time()
for facname, faction in pairs(factions.factions) do for facname, faction in pairs(factions.factions) do
if faction:is_online() then if faction:is_online() then
faction:increase_power(factions.power_per_tick) faction:increase_power(factions.power_per_tick)
end end
if faction.last_logon - now > factions.maximum_faction_inactivity then
faction:disband()
end
end 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 return not player_faction:has_permission(player, "claim") and player_faction.power > 0. and not parcel_faction.is_admin
end end
-- no faction -- no faction
if not parcel_faction or not player_faction then if not parcel_faction then
return default_is_protected(pos, player) return default_is_protected(pos, player)
<<<<<<< HEAD
elseif not player_faction then elseif not player_faction then
return true return true
else else
if not not player_faction then if not not player_faction then
=======
elseif player_faction then
>>>>>>> 4d53e4cc4017fc499bf9c966cd3a6de5cacbe0e8
if parcel_faction.name == player_faction.name then if parcel_faction.name == player_faction.name then
return not parcel_faction:has_permission(player, "build") return not parcel_faction:has_permission(player, "build")
else else
return not parcel_faction:parcel_is_attacked_by(parcelpos, player_faction) return not parcel_faction:parcel_is_attacked_by(parcelpos, player_faction)
end end
<<<<<<< HEAD
end end
=======
else
return true
>>>>>>> 4d53e4cc4017fc499bf9c966cd3a6de5cacbe0e8
end end
end end