kingdoms_game/mods/kingdoms/kingdoms.lua

266 lines
8.4 KiB
Lua
Raw Normal View History

2019-04-17 16:23:34 -04:00
-- Get info about members
function kingdoms.player_in_any_kingdoms(name)
2019-04-18 20:48:07 -04:00
if kingdoms.members[name] ~= nil then
return true, kingdoms.members[name].kingdom
else
return false
end
2019-04-17 16:23:34 -04:00
end
2019-04-18 20:48:07 -04:00
function kingdoms.player_has_priv(name, priv)
if kingdoms.members[name] == nil then
return false
end
local rank = kingdoms.members[name].rank
local kingdom = kingdoms.kingdoms[kingdoms.members[name].kingdom]
return kingdom.ranks[rank][priv] ~= nil
2019-04-17 16:23:34 -04:00
end
-- Add / remove / modify members
function kingdoms.add_player_to_kingdom(kingdom_name, name, rank)
-- Check if player exists
if minetest.player_exists(name) ~= true then
return false, "Player " .. name .. " has not joined yet"
end
-- Check if kingdom exists
local k = kingdoms.kingdoms[kingdom_name]
if k == nil then
return false, "Kingdom " .. kingdom_name .. " does not exist"
end
-- Check if player is in kingdom
local isIn, kIn = kingdoms.player_in_any_kingdoms(name)
if isIn then
return false, "Player " .. name .. " is already in kingdom " .. kIn
end
-- Check rank
if rank == nil then
2019-04-18 20:48:07 -04:00
rank = kingdoms.kingdoms[kingdom_name].default_rank
2019-04-17 16:23:34 -04:00
elseif kingdoms.kingdoms[kingdom_name].ranks[rank] == nil then
return false, "Rank " .. rank .. " does not exist"
end
-- Add player to kingdom
kingdoms.kingdoms[kingdom_name].members[name] = true
kingdoms.members[name] = {rank = rank, kingdom = kingdom_name}
-- Save
kingdoms.helpers.save()
return true, "Added " .. name .. " to kingdom " .. kingdom_name
end
2019-04-18 20:48:07 -04:00
function kingdoms.remove_player_from_kingdom(name)
2019-04-17 16:23:34 -04:00
-- Check if player is in kingdom
2019-04-18 20:48:07 -04:00
if kingdoms.members[name] == nil then
return false, "Player " .. name .. " is not in a kingdom"
2019-04-17 16:23:34 -04:00
end
-- Remove
2019-04-18 20:48:07 -04:00
local kingdom_name = kingdoms.members[name].kingdom
2019-04-17 16:23:34 -04:00
kingdoms.kingdoms[kingdom_name].members[name] = nil
kingdoms.members[name] = nil
2019-04-21 13:01:49 -04:00
-- If there are 0 members, delete kingdom
if kingdoms.helpers.count_table(kingdoms.kingdoms[kingdom_name].members) == 0 then
kingdoms.remove_kingdom(kingdom_name)
end
2019-04-17 16:23:34 -04:00
-- Save
kingdoms.helpers.save()
return true, "Removed " .. name .. " from kingdom " .. kingdom_name
end
function kingdoms.set_player_rank(name, rank)
-- Check if player is in a kingdom
if kingdoms.player_in_any_kingdoms(name) ~= true then
return false, "Player " .. name .. " is not in a kingdom"
end
-- Check if rank exists
local k = kingdoms.members[name].kingdom
if kingdoms.kingdoms[k].ranks[rank] == nil then
return false, "Rank " .. rank .. " does not exist in kingdom " .. k
end
-- Set rank
kingdoms.members[name].rank = rank
return true, "Set player " .. name .. "'s rank to " .. rank
end
-- Add / remove / modify kingdoms
function kingdoms.add_kingdom(name, king)
-- Check if kingdom already exists
if kingdoms.kingdoms[name] ~= nil then
return false, "Kingdom already exists"
end
-- Create new entry
kingdoms.kingdoms[name] = {
name = name,
members = {},
2019-04-21 14:46:27 -04:00
news = {},
2019-04-18 20:48:07 -04:00
ranks = kingdoms.helpers.copy_table(kingdoms.default_ranks),
default_rank = "soldier",
2019-04-21 10:40:33 -04:00
restricted = false,
color = "White"
2019-04-17 16:23:34 -04:00
}
-- Add owner
kingdoms.add_player_to_kingdom(name, king, "king")
2019-04-21 18:33:42 -04:00
kingdoms.add_news("Kingdom " .. name .. " was created")
2019-04-17 16:23:34 -04:00
-- Save
kingdoms.helpers.save()
return true, "Added kingdom " .. name
end
function kingdoms.remove_kingdom(name)
-- Check if kingdom exists
if kingdoms.kingdoms[name] == nil then
2019-04-18 20:48:07 -04:00
return false, "Kingdom " .. name .. " does not exist"
2019-04-17 16:23:34 -04:00
end
2019-04-18 20:48:07 -04:00
-- Remove members
for n,_ in pairs(kingdoms.kingdoms[name].members) do
kingdoms.members[n] = nil
end
-- Remove applications
for k, p in pairs(kingdoms.pending) do
if p == name then
kingdoms.pending[k] = nil
end
end
2019-04-21 13:01:49 -04:00
-- Remove markers
for hpos, m in pairs(kingdoms.markers) do
if m.kingdom == name then
minetest.set_node(m.pos, {name = "air"})
kingdoms.markers[hpos] = nil
end
end
2019-04-18 20:48:07 -04:00
-- Remove kingdom
2019-04-17 16:23:34 -04:00
kingdoms.kingdoms[name] = nil
2019-04-21 18:33:42 -04:00
kingdoms.add_news("Kingdom " .. name .. " was disbanded")
2019-04-17 16:23:34 -04:00
-- Save
kingdoms.helpers.save()
return true, "Removed kingdom " .. name
end
-- Add / remove / modify ranks
function kingdoms.add_rank(name, rank, privs)
-- Check if kingdom exists
if kingdoms.kingdoms[name] == nil then
2019-04-18 20:48:07 -04:00
return false, "Kingdom " .. name .. " does not exist"
2019-04-17 16:23:34 -04:00
end
-- Check if rank exists
if kingdoms.kingdoms[name].ranks[rank] ~= nil then
return false, "Rank " .. rank .. " already exists"
end
-- Validate privs
if privs == nil then
privs = kingdoms.helpers.copy_table(kingdoms.default_ranks.soldier)
else
for priv, _ in pairs(privs) do
if kingdoms.kingdom_privs[priv] == nil then
return false, "Invalid priv " .. priv
end
end
end
-- Add rank
kingdoms.kingdoms[name].ranks[rank] = privs
-- Save
kingdoms.helpers.save()
return true, "Added rank " .. rank .. " to kingdom " .. name .. " with privs " .. kingdoms.helpers.keys_to_str(privs)
end
function kingdoms.remove_rank(name, rank)
-- Check if kingdom exists
if kingdoms.kingdoms[name] == nil then
2019-04-18 20:48:07 -04:00
return false, "Kingdom " .. name .. " does not exist"
2019-04-17 16:23:34 -04:00
end
-- Check if rank exists
if kingdoms.kingdoms[name].ranks[rank] == nil then
2019-04-18 20:48:07 -04:00
return false, "Rank " .. rank .. " does not exist in kingdom " .. name
2019-04-17 16:23:34 -04:00
end
-- Remove rank
kingdoms.kingdoms[name].ranks[rank] = nil
2019-04-18 20:48:07 -04:00
-- Demote all members with that rank
for n,_ in pairs(kingdoms.kingdoms[name].members) do
if kingdoms.members[n].rank == rank then
kingdoms.members[n].rank = kingdoms.kingdoms[name].default_rank
end
end
-- Save
kingdoms.helpers.save()
return true, "Removed rank " .. rank .. " from kingdom " .. name
end
function kingdoms.set_rank_privs(name, rank, privs)
-- Check if kingdom exists
if kingdoms.kingdoms[name] == nil then
return false, "Kingdom " .. name .. " does not exist"
end
-- Check if rank exists
if kingdoms.kingdoms[name].ranks[rank] == nil then
return false, "Rank " .. rank .. " does not exist"
end
-- Check if privs are valid
2019-04-21 13:01:49 -04:00
if privs["all"] == nil then
for priv, _ in pairs(privs) do
if kingdoms.kingdom_privs[priv] == nil then
return false, "Invalid priv " .. priv
end
2019-04-18 20:48:07 -04:00
end
2019-04-21 13:01:49 -04:00
else
privs = kingdoms.helpers.copy_table(kingdoms.kingdom_privs)
2019-04-18 20:48:07 -04:00
end
-- Set privs
kingdoms.kingdoms[name].ranks[rank] = privs
-- Save
kingdoms.helpers.save()
return true, "Set privs of rank " .. rank .. " to " .. kingdoms.helpers.keys_to_str(privs)
end
function kingdoms.set_default_rank(name, rank)
-- Check if kingdom exists
if kingdoms.kingdoms[name] == nil then
return false, "Kingdom " .. name .. " does not exist"
end
-- Check if rank exists
if kingdoms.kingdoms[name].ranks[rank] == nil then
return false, "Rank " .. rank .. " does not exist"
end
-- Set as default
kingdoms.kingdoms[name].default_rank = rank
-- Save
kingdoms.helpers.save()
return true, "Set default rank of kingdom " .. name .. " to " .. rank
end
function kingdoms.toggle_restricted(name)
-- Check if kingdom exists
if kingdoms.kingdoms[name] == nil then
return false, "Kingdom " .. name .. " does not exist"
end
-- Toggle
kingdoms.kingdoms[name].restricted = not kingdoms.kingdoms[name].restricted
2019-04-17 16:23:34 -04:00
-- Save
kingdoms.helpers.save()
2019-04-18 20:48:07 -04:00
return true, kingdoms.kingdoms[name].restricted and "Kingdom " .. name .. " restriction enabled" or
"Kingdom " .. name .. " restriction disabled"
2019-04-17 16:23:34 -04:00
end
2019-04-21 10:40:33 -04:00
function kingdoms.set_color(name, color)
-- Check if kingdom exists
if kingdoms.kingdoms[name] == nil then
return false, "Kingdom " .. name .. " does not exist"
end
-- Check if color exists
if kingdoms.colors[color] == nil then
return false, "Invaild color" .. color
end
-- Set color
kingdoms.kingdoms[name].color = color
-- Update all loaded markers
local n
local new_itemstr = "kingdoms:marker_" .. string.lower(color)
for _,m in pairs(kingdoms.markers) do
if m.kingdom == name then
n = minetest.get_node_or_nil(m.pos)
if n ~= nil then
minetest.swap_node(m.pos, {name = new_itemstr})
end
end
end
-- Save
kingdoms.helpers.save()
return true, "Changed color of kingdom " .. name .. " to " .. color
end