aboutsummaryrefslogtreecommitdiff
path: root/welfare.lua
blob: 964c2f6e65798f5b24371cff71eb0e3b91ae7458 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
-- adapted from the income.lua file from the currency mod.
local timer = 0

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.welfare_cap
		atm.ensure_init(name)
		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
	if payout then
		atm.saveaccounts()
	end
end

minetest.register_globalstep(function(dtime)
	timer = timer + dtime;
	if timer >= 1000 then
		timer = 0
		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.log("action", "[ATM] Player "..plname.." put on welfare by "..name)
		minetest.chat_send_player(name, "Player "..plname.." put on welfare successfully.")
		return true
	end,
})
				
minetest.register_chatcommand("view_finances", {
	description = "view a player's finances",
	params = "<player>",
	privs = { kick = true },
	func = function (name, plname)
		if not atm.balance[plname] then
			return false, plname.." does not have a bank account!"
		end
		minetest.chat_send_player(name, "Account balance: "..atm.balance[plname])
		if atm.welfare[plname] then
			minetest.chat_send_player(name, "Remaining welfare: "..atm.welfare[plname])
		end
		return true
	end
})