From fde792711b6eb23692c3268dc076d7a560755c33 Mon Sep 17 00:00:00 2001 From: Billy S Date: Wed, 17 Apr 2019 16:23:34 -0400 Subject: [PATCH] Added rank system to kingdoms --- mods/kingdoms/chat.lua | 52 +++++++++++++ mods/kingdoms/helpers.lua | 44 +++++++++++ mods/kingdoms/init.lua | 45 +++++++++++ mods/kingdoms/kingdoms.lua | 151 +++++++++++++++++++++++++++++++++++++ mods/kingdoms/mod.conf | 3 + mods/lib_chatcmdbuilder | 1 + 6 files changed, 296 insertions(+) create mode 100644 mods/kingdoms/chat.lua create mode 100644 mods/kingdoms/helpers.lua create mode 100644 mods/kingdoms/init.lua create mode 100644 mods/kingdoms/kingdoms.lua create mode 100644 mods/kingdoms/mod.conf create mode 160000 mods/lib_chatcmdbuilder diff --git a/mods/kingdoms/chat.lua b/mods/kingdoms/chat.lua new file mode 100644 index 0000000..7511818 --- /dev/null +++ b/mods/kingdoms/chat.lua @@ -0,0 +1,52 @@ +-- Register "overlord" (kingdom-creater/modifier) priv +minetest.register_privilege("overlord", { + description = "Can create / modifiy kingdoms", + give_to_singleplayer = false +}) + +-- Admin kingdom commands +ChatCmdBuilder.new("kingdoms_admin", function(cmd) + -- Add new kingdom + cmd:sub("add :name:word", function(name, kingdom_name) + return kingdoms.add_kingdom(kingdom_name, name) + end) + -- Add new kingdom with specified owner + cmd:sub("add :name:word :king:word", function(name, kingdom_name, king) + return kingdoms.add_kingdom(kingdom_name, king) + end) + -- Remove kingdom + cmd:sub("remove :name:word", function(name, kingdom_name) + return kingdoms.remove_kingdom(kingdom_name) + end) + -- Add player to kingdom + cmd:sub("join :victim:word :kingdom:word", function(name, victim, kingdom) + return kingdoms.add_player_to_kingdom(kingdom, victim) + end) + -- Remove player from kingdom + cmd:sub("remove :victim:word :kingdom:word", function(name, victim, kingdom) + return kingdoms.remove_player_from_kingdom(kingdom, victim) + end) + -- Set player rank in kingdom + cmd:sub("set_rank :victim:word :rank:word", function(name, victim, rank) + return kingdoms.set_player_rank(victim, rank) + end) + -- Add rank to kingdom + cmd:sub("add_rank :kingdom:word :rank:word", function(name, kingdom, rank) + return kingdoms.add_rank(kingdom, rank) + end) + -- Add rank to kingdom with specified privs + cmd:sub("add_rank :kingdom:word :rank:word :privs:text", function(name, kingdom, rank, privs) + return kingdoms.add_rank(kingdom, rank, kingdoms.helpers.split_into_keys(privs)) + end) +end, { + description = "Manage kingdoms (See '/kingdoms_admin help' for more information)", + privs = {overlord = true} +}) + +-- Player kingdoms comannds +ChatCmdBuilder.new("kingdoms", function(cmd) + -- List kingdoms + cmd:sub("list", function(name) + return true, kingdoms.list_kingdoms() + end) +end) diff --git a/mods/kingdoms/helpers.lua b/mods/kingdoms/helpers.lua new file mode 100644 index 0000000..40c0cf2 --- /dev/null +++ b/mods/kingdoms/helpers.lua @@ -0,0 +1,44 @@ +kingdoms.helpers = {} + +function kingdoms.helpers.copy_table(t) + local n = {} + for k,p in pairs(t) do + n[k] = p + end + return n +end + +function kingdoms.helpers.count_table(t) + local c = 0 + local idxs = {} + for k,p in pairs(t) do + table.insert(idxs, k) + c = c + 1 + end + return c, idxs +end + +function kingdoms.helpers.keys_to_str(t) + local str = "" + for k,p in pairs(t) do + if str == "" then + str = str .. k + else + str = str .. ", " .. k + end + end + return str +end + +function kingdoms.helpers.split_into_keys(str) + local out = {} + for k in string.gfind(str, "[%a_]+") do + out[k] = true + end + return out +end + +function kingdoms.helpers.save() + local ktable = minetest.serialize(kingdoms.kingdoms) + kingdoms.storage:set_string("kingdoms", ktable) +end diff --git a/mods/kingdoms/init.lua b/mods/kingdoms/init.lua new file mode 100644 index 0000000..b5c5efa --- /dev/null +++ b/mods/kingdoms/init.lua @@ -0,0 +1,45 @@ +-- Init +local storage = minetest.get_mod_storage() +local mp = minetest.get_modpath(minetest.get_current_modname()) +kingdoms = {} + +kingdoms.storage = storage + +-- Load helpers +dofile(mp .. "/helpers.lua") + +-- Define privs +kingdoms.kingdom_privs = { + kick = true, -- Kick member + join = true, -- Accept member + make_base = true, -- Place flag + interact = true, -- Interact with team areas + diplomat = true, -- Make / end wars +} + +-- Define default ranks +kingdoms.default_ranks = { + king = kingdoms.helpers.copy_table(kingdoms.kingdom_privs), + lord = {make_base = true, interact = true, join = true, kick = true}, + soldier = {make_base = true, interact = true} +} + +-- Load players +local pStr = storage:get_string("members") +if pStr == "" then + kingdoms.members = {} +else + kingdoms.members = minetest.deserialize(pStr) +end + +-- Load kingdoms +local kStr = storage:get_string("kingdoms") +if kStr == "" then + kingdoms.kingdoms = {} +else + kingdoms.kingdoms = minetest.deserialize(kStr) +end +dofile(mp .. "/kingdoms.lua") + +-- Register kingdoms chatcommand +dofile(mp .. "/chat.lua") diff --git a/mods/kingdoms/kingdoms.lua b/mods/kingdoms/kingdoms.lua new file mode 100644 index 0000000..b943454 --- /dev/null +++ b/mods/kingdoms/kingdoms.lua @@ -0,0 +1,151 @@ +-- Get info about members +function kingdoms.player_in_any_kingdoms(name) + return kingdoms.members[name] ~= nil +end + +-- Get info about kingdom(s) +function kingdoms.list_kingdoms() + local l = "" + for n,k in pairs(kingdoms.kingdoms) do + local mNum = kingdoms.helpers.count_table(k.members) + l = l .. n .. ": " .. tostring(mNum) .. " member(s)\n" + end + return l +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 + rank = "soldier" + 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 + +function kingdoms.remove_player_from_kingdom(kingdom_name, name) + -- 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 + if kingdoms.kingdoms[kingdom_name].members[name] == nil then + return false, "Player " .. name .. " is not in kingdom " .. kingdom_name + end + -- Remove + kingdoms.kingdoms[kingdom_name].members[name] = nil + kingdoms.members[name] = nil + -- 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 = {}, + ranks = kingdoms.helpers.copy_table(kingdoms.default_ranks) + } + -- Add owner + kingdoms.add_player_to_kingdom(name, king, "king") + -- 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 + return false, "Kingdom does not exist" + end + -- Remove + kingdoms.kingdoms[name] = nil + -- 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 + return false, "Kingdom does not exist" + 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 + return false, "Kingdom does not exist" + end + -- Check if rank exists + if kingdoms.kingdoms[name].ranks[rank] == nil then + return false, "Rank " .. rank .. " does not exist" + end + -- Remove rank + kingdoms.kingdoms[name].ranks[rank] = nil + -- Save + kingdoms.helpers.save() + return true, "Removed rank " .. rank " from kingdom " .. name +end diff --git a/mods/kingdoms/mod.conf b/mods/kingdoms/mod.conf new file mode 100644 index 0000000..44b5f57 --- /dev/null +++ b/mods/kingdoms/mod.conf @@ -0,0 +1,3 @@ +name = kingdoms +description = Protection / Teams system for PK +depends = lib_chatcmdbuilder diff --git a/mods/lib_chatcmdbuilder b/mods/lib_chatcmdbuilder new file mode 160000 index 0000000..4cce1cd --- /dev/null +++ b/mods/lib_chatcmdbuilder @@ -0,0 +1 @@ +Subproject commit 4cce1cdade233b855d62f4c5abeeffa694d885c2