diff options
author | gpcf <gpcf@gpcf.eu> | 2024-09-22 16:14:10 +0200 |
---|---|---|
committer | gpcf <gpcf@gpcf.eu> | 2024-09-22 16:14:10 +0200 |
commit | 07497d8957d1969af9b08ee35899609892438bc8 (patch) | |
tree | 47436232ea67b27119a931a5bd4cc056724e2a36 | |
parent | 925b6f62e4ce62aa4c06ee2a3a444eebf5b5c307 (diff) | |
download | atm-07497d8957d1969af9b08ee35899609892438bc8.tar.gz atm-07497d8957d1969af9b08ee35899609892438bc8.tar.bz2 atm-07497d8957d1969af9b08ee35899609892438bc8.zip |
Add chat command for welfare, add unit tests and mod.conf
-rw-r--r-- | depends.txt | 3 | ||||
-rw-r--r-- | init.lua | 3 | ||||
-rw-r--r-- | interest.lua | 28 | ||||
-rw-r--r-- | mod.conf | 3 | ||||
-rw-r--r-- | spec/welfare_spec.lua | 58 | ||||
-rw-r--r-- | spec/welfare_spec.lua~ | 19 |
6 files changed, 106 insertions, 8 deletions
diff --git a/depends.txt b/depends.txt deleted file mode 100644 index 1dac83d..0000000 --- a/depends.txt +++ /dev/null @@ -1,3 +0,0 @@ -default -currency -mesecons?
\ No newline at end of file @@ -5,7 +5,8 @@ atm = {} atm.balance = {} atm.welfare = {} -atm.default_welfare = 1000 +atm.welfare_cap = 1000 -- +atm.welfare_amount = 5 atm.pending_transfers = {} atm.completed_transactions = {} atm.pth = minetest.get_worldpath().."/atm_accounts" diff --git a/interest.lua b/interest.lua index 696b691..54df1ce 100644 --- a/interest.lua +++ b/interest.lua @@ -5,11 +5,11 @@ function atm.welfare_payout() payout = false for _,player in ipairs(minetest.get_connected_players()) do local name = player:get_player_name() - local limit = atm.welfare[name] or atm.default_welfare + local limit = atm.welfare[name] or atm.welfare_cap atm.ensure_init(name) - if limit > 5 then - atm.balance[name] = math.floor(atm.balance[name] + 5) - atm.welfare[name] = limit - 5 + if limit > atm.welfare_amount then + atm.balance[name] = math.floor(atm.balance[name] + atm.welfare_amount) + atm.welfare[name] = limit - atm.welfare_amount payout = true end end @@ -25,3 +25,23 @@ minetest.register_globalstep(function(dtime) atm.welfare_payout() end end) + + +minetest.register_chatcommand("give_welfare", { + description = "Put a player on welfare payments", + params = "<player> <amount>", + privs = { kick = true }, + func = function (name, params) + local plname, amount = params:match("(%S+)%s+(%d+)") + if not plname then + return false, "Invalid parameters: "..params + end + if not atm.balance[plname] then + return false, plname.." does not have a bank account!" + end + atm.welfare[plname] = tonumber(amount) or atm.welfare_cap + minetest.chat_send_player(name, "Player "..plname.." put on welfare successfully.") + return true + end, +}) + diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..19c1b72 --- /dev/null +++ b/mod.conf @@ -0,0 +1,3 @@ +name = atm +depends = default, currency +optional_depends = mesecons
\ No newline at end of file diff --git a/spec/welfare_spec.lua b/spec/welfare_spec.lua new file mode 100644 index 0000000..d41cf71 --- /dev/null +++ b/spec/welfare_spec.lua @@ -0,0 +1,58 @@ +require("mineunit") + +mineunit("core") + +mineunit("common/chatcommands") +mineunit("server") +mineunit("player") + +sourcefile("init") + +mineunit:mods_loaded() + +minetest.safe_file_write = function () + +end + +describe("Welfare payout", function() + it("sets up balance correctly", function() + local newplayer = Player("newplayer") + mineunit:execute_on_joinplayer(newplayer) + atm.ensure_init("newplayer") + assert.equals(atm.balance["newplayer"], atm.startbalance) + end) + it("pays out welfare to players", function() + atm.ensure_init("newplayer") + local oldbalance = atm.balance["newplayer"] + atm.welfare_payout() + assert.equals(atm.balance["newplayer"], oldbalance + atm.welfare_amount) + assert.equals(atm.welfare["newplayer"], atm.welfare_cap - atm.welfare_amount) + end) + it("does not payout welfare if welfare cap is exhausted", function() + atm.ensure_init("newplayer") + local oldbalance = atm.balance["newplayer"] + atm.welfare["newplayer"] = atm.welfare_amount - 1 + local oldwelfare = atm.welfare['newplayer'] + atm.welfare_payout() + assert.equals(atm.balance["newplayer"], oldbalance) + assert.equals(atm.welfare["newplayer"], oldwelfare) + end) + it("allows moderators to put players on welfare", function() + local moderator = Player("admin", {kick = true, ban = true}) + moderator:send_chat_message("/give_welfare newplayer 555") + assert.equals(atm.welfare["newplayer"], 555) + moderator:send_chat_message("/give_welfare newplayer bbbb") + assert.equals(atm.welfare["newplayer"], 555) + end) + it("bans normal players from giving themselves welfare", function() + local player = Player("foobar") + mineunit:execute_on_joinplayer(player) + atm.welfare_payout() + local oldwelfare = atm.welfare['foobar'] + player:send_chat_message("/give_welfare foobar 555") + assert.equals(atm.welfare["foobar"], oldwelfare) + end) + +end +) + diff --git a/spec/welfare_spec.lua~ b/spec/welfare_spec.lua~ new file mode 100644 index 0000000..dd04618 --- /dev/null +++ b/spec/welfare_spec.lua~ @@ -0,0 +1,19 @@ +require("mineunit") + +mineunit("core") + +mineunit("common/chatcommands") +mineunit("server") +mineunit("player") + +sourcefile("init") + +mineunit:mods_loaded() + + +describe("Welfare payout", function() + it("sets up balance correctly", function() + local newplayer = Player("newplayer") + mineunit:execute_on_joinplayer(newplayer) + assert.equals(atm.balance["newplayer"], atm. + |