Fixed unclaim bug, switched to metatables for factions

This commit is contained in:
shamoanjac 2016-08-08 03:30:29 +02:00
parent 55fe191cfd
commit 903abc15b6
2 changed files with 235 additions and 198 deletions

View File

@ -179,12 +179,12 @@ factions.register_command("unclaim", {
faction_permissions = {"claim"},
description = "Unclaim the plot of land you're on.",
on_success = function(player, faction, pos, chunkpos, args)
local chunk = factions.chunk[chunkpos]
if chunk ~= player_faction.name then
--TODO: error (not your faction's chunk)
local chunk = factions.chunks[chunkpos]
if chunk ~= faction.name then
send_error(player, "This chunk does not belong to you.")
return false
else
player_faction:unclaim_chunk(chunkpos)
faction:unclaim_chunk(chunkpos)
return true
end
end
@ -269,7 +269,7 @@ factions.register_command("create", {
end
local factionname = args.strings[1]
if factions.can_create_faction(factionname) then
new_faction = factions.new_faction(factionname)
new_faction = factions.new_faction(factionname, nil)
new_faction:add_player(player, new_faction.default_leader_rank)
return true
else
@ -441,7 +441,7 @@ factions.register_command("setspawn", {
})
factions.register_command("where", {
description = "See whose chunk you stand on",
description = "See whose chunk you stand on.",
infaction = false,
on_success = function(player, faction, pos, chunkpos, args)
local chunk = factions.chunks[chunkpos]
@ -455,6 +455,14 @@ factions.register_command("where", {
end
})
factions.register_command("help", {
description = "Shows help for commands.",
infaction = false,
on_success = function(player, faction, pos, chunkpos, args)
factions_chat.show_help(player)
end
})
-------------------------------------------------------------------------------
-- name: cmdhandler(playername,parameter)
--
@ -483,7 +491,7 @@ factions_chat.cmdhandler = function (playername,parameter)
local cmd = factions.commands[params[1]]
if not cmd then
send_error(player, "Unknown command.")
send_error(playername, "Unknown command.")
return false
end
@ -518,7 +526,7 @@ function factions_chat.show_help(playername)
for i in ipairs(v.format) do
table.insert(args, v.format[i])
end
MSG{"\t/factions "..k.." <"..table.concat(args, "> <").."> : "..v.description}
MSG("\t/factions "..k.." <"..table.concat(args, "> <").."> : "..v.description)
end
end

View File

@ -46,11 +46,29 @@ factions.can_create_faction = function(name)
end
end
---------------------
--! @brief create a faction object
factions.new_faction = function(name)
local faction = {
name = name,
factions.Faction = {
power = 0.,
players = {},
ranks = {["leader"] = {"disband", "claim", "playerlist", "build", "edit", "ranks"},
["member"] = {"build"}
},
leader = nil,
default_rank = "member",
default_leader_rank = "leader",
description = "Default faction description.",
invited_players = {},
land = {},
allies = {},
enemies = {},
join_free = false,
spawn = nil,
}
factions.Faction.__index = factions.Faction
function factions.Faction:new(faction)
faction = {
power = 0.,
players = {},
ranks = {["leader"] = {"disband", "claim", "playerlist", "build", "edit", "ranks"},
@ -66,198 +84,208 @@ factions.new_faction = function(name)
enemies = {},
join_free = false,
spawn = nil,
} or faction
setmetatable(faction, self)
return faction
end
----------------------
-- methods
increase_power = function(self, power)
self.power = self.power + power
factions.save()
end,
decrease_power = function(self, power)
self.power = self.power - power
factions.save()
end,
add_player = function(self, player, rank)
self.players[player] = rank or self.default_rank
factions.players[player] = self.name
self:on_player_join(player)
self.invited_players[player] = nil
factions.save()
end,
remove_player = function(self, player)
self.players[player] = nil
factions.players[player] = nil
self:on_player_leave(player)
factions.save()
end,
claim_chunk = function(self, chunkpos)
factions.chunks[chunkpos] = self.name
self.land[chunkpos] = true
self:on_claim_chunk(chunkpos)
factions.save()
end,
unclaim_chunk = function(self, chunkpos)
factions.chunks[chunkpos] = nil
self.land[chunkpos] = nil
self:on_unclaim_chunks(chunkpos)
factions.save()
end,
disband = function(self)
for i in ipairs(self.players) do -- remove players affiliation
factions.players[self.players[i]] = nil
end
for k, v in pairs(self.land) do -- remove chunk claims
factions.chunks[v] = nil
end
self:on_disband()
factions.factions[self.name] = nil
factions.save()
end,
set_leader = function(self, player)
self.leader = player
self.players[player] = self.default_leader_rank
self:on_new_leader()
factions.save()
end,
has_permission = function(self, player, permission)
local p = self.players[player]
if not p then
return false
end
local perms = self.ranks[p]
for i in ipairs(perms) do
if perms[i] == permission then
return true
end
end
return false
end,
set_description = function(self, new)
self.description = new
self:on_change_description()
factions.save()
end,
invite_player = function(self, player)
self.invited_players[player] = true
self:on_player_invited(player)
factions.save()
end,
revoke_invite = function(self, player)
self.invited_players[player] = nil
self:on_revoke_invite(player)
factions.save()
end,
is_invited = function(self, player)
return table.contains(self.invited_players, player)
end,
toggle_join_free = function(self, bool)
self.join_free = bool
self:on_toggle_join_free()
factions.save()
end,
can_join = function(self, player)
return self.join_free or self.invited_players[player]
end,
new_alliance = function(self, faction)
self.allies[faction] = true
self:on_new_alliance(faction)
if self.enemies[faction] then
self:end_enemy(faction)
end
factions.save()
end,
end_alliance = function(self, faction)
self.allies[faction] = nil
self:on_end_alliance(faction)
factions.save()
end,
new_enemy = function(self, faction)
self.enemies[faction] = true
self:on_new_enemy(faction)
if self.allies[faction] then
self:end_alliance(faction)
end
factions.save()
end,
end_enemy = function(self, faction)
self.enemies[faction] = nil
self:on_end_enemy(faction)
factions.save()
end,
set_spawn = function(self, pos)
self.spawn = pos
self:on_set_spawn()
factions.save()
end,
add_rank = function(self, rank, perms)
self.ranks[rank] = perms
self:on_new_rank(rank)
factions.save()
end,
delete_rank = function(self, rank, newrank)
for player, r in pairs(self.players) do
if r == rank then
self.players[player] = newrank
end
end
self.ranks[rank] = nil
self:on_delete_rank(rank, newrank)
factions.save()
end,
--------------------------
-- callbacks for events --
on_create = function(self) --! @brief called when the faction is added to the global faction list
--TODO: implement
end,
on_player_leave = function(self, player)
--TODO: implement
end,
on_player_join = function(self, player)
--TODO: implement
end,
on_claim_chunk = function(self, pos)
--TODO: implement
end,
on_unclaim_chunk = function(self, pos)
--TODO: implement
end,
on_disband = function(self, pos)
--TODO: implement
end,
on_new_leader = function(self)
--TODO: implement
end,
on_change_description = function(self)
--TODO: implement
end,
on_player_invited = function(self, player)
--TODO: implement
end,
on_toggle_join_free = function(self, player)
--TODO: implement
end,
on_new_alliance = function(self, faction)
--TODO: implement
end,
on_end_alliance = function(self, faction)
--TODO: implement
end,
on_set_spawn = function(self)
--TODO: implement
end,
on_add_rank = function(self, rank)
--TODO: implement
end,
on_delete_rank = function(self, rank, newrank)
--TODO: implement
end,
}
factions.new_faction = function(name)
local faction = factions.Faction:new(nil)
faction.name = name
factions.factions[name] = faction
factions.save()
return faction
end
function factions.Faction.increase_power(self, power)
self.power = self.power + power
factions.save()
end
function factions.Faction.decrease_power(self, power)
self.power = self.power - power
factions.save()
end
function factions.Faction.add_player(self, player, rank)
self.players[player] = rank or self.default_rank
factions.players[player] = self.name
self:on_player_join(player)
self.invited_players[player] = nil
factions.save()
end
function factions.Faction.remove_player(self, player)
self.players[player] = nil
factions.players[player] = nil
self:on_player_leave(player)
factions.save()
end
function factions.Faction.claim_chunk(self, chunkpos)
factions.chunks[chunkpos] = self.name
self.land[chunkpos] = true
self:on_claim_chunk(chunkpos)
factions.save()
end
function factions.Faction.unclaim_chunk(self, chunkpos)
factions.chunks[chunkpos] = nil
self.land[chunkpos] = nil
self:on_unclaim_chunk(chunkpos)
factions.save()
end
function factions.Faction.disband(self)
for i in ipairs(self.players) do -- remove players affiliation
factions.players[self.players[i]] = nil
end
for k, v in pairs(self.land) do -- remove chunk claims
factions.chunks[v] = nil
end
self:on_disband()
factions.factions[self.name] = nil
factions.save()
end
function factions.Faction.set_leader(self, player)
self.leader = player
self.players[player] = self.default_leader_rank
self:on_new_leader()
factions.save()
end
function factions.Faction.has_permission(self, player, permission)
local p = self.players[player]
if not p then
return false
end
local perms = self.ranks[p]
for i in ipairs(perms) do
if perms[i] == permission then
return true
end
end
return false
end
function factions.Faction.set_description(self, new)
self.description = new
self:on_change_description()
factions.save()
end
function factions.Faction.invite_player(self, player)
self.invited_players[player] = true
self:on_player_invited(player)
factions.save()
end
function factions.Faction.revoke_invite(self, player)
self.invited_players[player] = nil
self:on_revoke_invite(player)
factions.save()
end
function factions.Faction.is_invited(self, player)
return table.contains(self.invited_players, player)
end
function factions.Faction.toggle_join_free(self, bool)
self.join_free = bool
self:on_toggle_join_free()
factions.save()
end
function factions.Faction.can_join(self, player)
return self.join_free or self.invited_players[player]
end
function factions.Faction.new_alliance(self, faction)
self.allies[faction] = true
self:on_new_alliance(faction)
if self.enemies[faction] then
self:end_enemy(faction)
end
factions.save()
end
function factions.Faction.end_alliance(self, faction)
self.allies[faction] = nil
self:on_end_alliance(faction)
factions.save()
end
function factions.Faction.new_enemy(self, faction)
self.enemies[faction] = true
self:on_new_enemy(faction)
if self.allies[faction] then
self:end_alliance(faction)
end
factions.save()
end
function factions.Faction.end_enemy(self, faction)
self.enemies[faction] = nil
self:on_end_enemy(faction)
factions.save()
end
function factions.Faction.set_spawn(self, pos)
self.spawn = pos
self:on_set_spawn()
factions.save()
end
function factions.Faction.add_rank(self, rank, perms)
self.ranks[rank] = perms
self:on_new_rank(rank)
factions.save()
end
function factions.Faction.delete_rank(self, rank, newrank)
for player, r in pairs(self.players) do
if r == rank then
self.players[player] = newrank
end
end
self.ranks[rank] = nil
self:on_delete_rank(rank, newrank)
factions.save()
end
--------------------------
-- callbacks for events --
function factions.Faction.on_create(self) --! @brief called when the faction is added to the global faction list
--TODO: implement
end
function factions.Faction.on_player_leave(self, player)
--TODO: implement
end
function factions.Faction.on_player_join(self, player)
--TODO: implement
end
function factions.Faction.on_claim_chunk(self, pos)
--TODO: implement
end
function factions.Faction.on_unclaim_chunk(self, pos)
--TODO: implement
end
function factions.Faction.on_disband(self, pos)
--TODO: implement
end
function factions.Faction.on_new_leader(self)
--TODO: implement
end
function factions.Faction.on_change_description(self)
--TODO: implement
end
function factions.Faction.on_player_invited(self, player)
--TODO: implement
end
function factions.Faction.on_toggle_join_free(self, player)
--TODO: implement
end
function factions.Faction.on_new_alliance(self, faction)
--TODO: implement
end
function factions.Faction.on_end_alliance(self, faction)
--TODO: implement
end
function factions.Faction.on_set_spawn(self)
--TODO: implement
end
function factions.Faction.on_add_rank(self, rank)
--TODO: implement
end
function factions.Faction.on_delete_rank(self, rank, newrank)
--TODO: implement
end
--??????????????
function factions.fix_powercap(name)
factions.data.factions[name].powercap = #factions.dynamic_data.membertable[name] + 10
@ -363,6 +391,7 @@ function factions.load()
for chunkpos, val in pairs(faction.land) do
factions.chunks[chunkpos] = facname
end
setmetatable(faction, factions.Faction)
end
file:close()
end