Skip to content

Commit

Permalink
per suggestions of LW
Browse files Browse the repository at this point in the history
  • Loading branch information
farooqkz committed Jul 1, 2023
1 parent 5e1e3a8 commit 5be1f44
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 55 deletions.
29 changes: 16 additions & 13 deletions mods/ctf/ctf_modebase/bounties.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ local bounties = {}

ctf_modebase.bounties = {}
-- ^ This is for game's own bounties
ctf_modebase.contributed_bounties = {}
ctf_modebase.contributed_bounties = {
--[[
["player_name"] = {
total = score,
contributors = {"player1", "player2", ...}
},
...
--]]
}
-- ^ This is for user contributed bounties
-- Saving format: player_name(str) equals to a table
-- The table has "total" which is the total contributed bounty
-- on player_name's head and "contributors" is a mapping of donater_name(str)
-- to donate_amount(number).

local function get_contributors(name)
local b = ctf_modebase.contributed_bounties[name]
Expand Down Expand Up @@ -271,10 +275,10 @@ ctf_core.register_chatcommand_alias("bounty", "b", {
amount = math.floor(amount)
local bteam = ctf_teams.get(bname)
if bteam == nil then
return false, "This player is either not online or not in any team"
return false, "This player is either not online or isn't in a team"
end
if bteam == ctf_teams.get(name) then
return false, "You cannot put bounty on your teammate's head!"
return false, "You cannot put bounty on your teammate!"
end
if amount < 5 then
return false, "Sorry you must at least donate 15"
Expand All @@ -300,17 +304,16 @@ ctf_core.register_chatcommand_alias("bounty", "b", {
contributors[name] = amount
ctf_modebase.contributed_bounties[bname] = { total = amount, contributors = contributors }
else
if ctf_modebase.contributed_bounties[bname]["contributors"][name] == nil then
minetest.chat_send_all(name)
ctf_modebase.contributed_bounties[bname]["contributors"][name] = amount
ctf_modebase.contributed_bounties[bname]["total"] = ctf_modebase.contributed_bounties[bname]["total"] + amount
if ctf_modebase.contributed_bounties[bname].contributors[name] == nil then
ctf_modebase.contributed_bounties[bname].contributors[name] = amount
else
return false, "You cannot donate for someone's head twice"
ctf_modebase.contributed_bounties[bname].contributors[name] = ctf_modebase.contributed_bounties[bname].contributors[name] + amount
end
ctf_modebase.contributed_bounties[bname].total = ctf_modebase.contributed_bounties[bname].total + amount
end
minetest.chat_send_all(
minetest.colorize(
CHAT_COLOR,
string.format("%s donated %d for %s's head!", get_contributors(bname), amount, bname)))
string.format("%s put %d bounty on %s!", get_contributors(bname), amount, bname)))
end,
})
82 changes: 41 additions & 41 deletions mods/ctf/ctf_rankings/default.lua
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
return function(top)
local modstorage = assert(minetest.get_mod_storage(), "Can only init rankings at runtime!")
local modstorage = assert(minetest.get_mod_storage(), "Can only init rankings at runtime!")

for k, v in pairs(modstorage:to_table()["fields"]) do
local rank = minetest.parse_json(v)
if rank ~= nil and rank.score then
top:set(k, rank.score)
end
for k, v in pairs(modstorage:to_table()["fields"]) do
local rank = minetest.parse_json(v)
if rank ~= nil and rank.score then
top:set(k, rank.score)
end
end

return {
backend = "default",
top = top,
modstorage = modstorage,

get = function(self, pname)
pname = PlayerName(pname)
return {
backend = "default",
top = top,
modstorage = modstorage,

local rank_str = self.modstorage:get_string(pname)
get = function(self, pname)
pname = PlayerName(pname)

if not rank_str or rank_str == "" then
return false
end
local rank_str = self.modstorage:get_string(pname)

return minetest.parse_json(rank_str)
end,
set = function(self, pname, newrankings, erase_unset)
pname = PlayerName(pname)
if not rank_str or rank_str == "" then
return false
end

if not erase_unset then
local rank = self:get(pname)
if rank then
for k, v in pairs(newrankings) do
rank[k] = v
end
return minetest.parse_json(rank_str)
end,
set = function(self, pname, newrankings, erase_unset)
pname = PlayerName(pname)

newrankings = rank
if not erase_unset then
local rank = self:get(pname)
if rank then
for k, v in pairs(newrankings) do
rank[k] = v
end
end

self.top:set(pname, newrankings.score or 0)
self.modstorage:set_string(pname, minetest.write_json(newrankings))
end,
add = function(self, pname, amounts)
pname = PlayerName(pname)
newrankings = rank
end
end

local newrankings = self:get(pname) or {}
self.top:set(pname, newrankings.score or 0)
self.modstorage:set_string(pname, minetest.write_json(newrankings))
end,
add = function(self, pname, amounts)
pname = PlayerName(pname)

for k, v in pairs(amounts) do
newrankings[k] = (newrankings[k] or 0) + v
end
local newrankings = self:get(pname) or {}

self.top:set(pname, newrankings.score or 0)
self.modstorage:set_string(pname, minetest.write_json(newrankings))
for k, v in pairs(amounts) do
newrankings[k] = (newrankings[k] or 0) + v
end
}

self.top:set(pname, newrankings.score or 0)
self.modstorage:set_string(pname, minetest.write_json(newrankings))
end
}
end

0 comments on commit 5be1f44

Please sign in to comment.