bank_accounts/interest.lua

67 lines
3.2 KiB
Lua

-- interest.lua (Nutzt die neue, dedizierte API-Funktion)
-- KONFIGURATION
local BALANCE_INTEREST_RATE = 0.005
local CREDIT_INTEREST_RATE = 0.03
local INTEREST_CHECK_INTERVAL = 600
local ONE_DAY_IN_SECONDS = 86400
local timestamp_file_path = minetest.get_worldpath() .. "/bank_interest_timestamp.txt"
local function read_timestamp() local f = io.open(timestamp_file_path, "r"); if not f then return 0 end; local time = tonumber(f:read("*a")); f:close(); return time or 0 end
local function write_timestamp(time) local f = io.open(timestamp_file_path, "w"); if not f then return end; f:write(tostring(time)); f:close() end
local time_since_last_check = 0
bank_accounts.is_calculating_interest = false
minetest.register_globalstep(function(dtime)
time_since_last_check = time_since_last_check + dtime
if time_since_last_check < INTEREST_CHECK_INTERVAL then return end
time_since_last_check = 0
local last_timestamp = read_timestamp()
if os.time() - last_timestamp >= ONE_DAY_IN_SECONDS then
if bank_accounts.is_calculating_interest then return end
minetest.log("action", "[bank_accounts] Starting daily interest calculation...")
bank_accounts.is_calculating_interest = true
-- KORREKTUR: Nutzt die neue, dedizierte Funktion
local data = bank_accounts.get_all_data()
local changes_made = false
-- Zinsen auf Guthaben
if BALANCE_INTEREST_RATE > 0 then
for player_name, balance in pairs(data.balance) do
if balance > 0 then
local interest = balance * BALANCE_INTEREST_RATE
data.balance[player_name] = balance + interest
if not data.history then data.history = {} end; if not data.history[player_name] then data.history[player_name] = {} end
table.insert(data.history[player_name], 1, { timestamp = os.time(), type = "Interest Paid", account = "balance", amount = interest, new_total = data.balance[player_name], desc = S("Daily interest (@1%)", BALANCE_INTEREST_RATE * 100), other = "Bank" })
changes_made = true
end
end
end
-- Zinsen auf Kredit
if CREDIT_INTEREST_RATE > 0 then
for player_name, credit in pairs(data.credit) do
if credit > 0 then
local interest = credit * CREDIT_INTEREST_RATE
data.credit[player_name] = credit + interest
if not data.history then data.history = {} end; if not data.history[player_name] then data.history[player_name] = {} end
table.insert(data.history[player_name], 1, { timestamp = os.time(), type = "Interest Charged", account = "credit", amount = interest, new_total = data.credit[player_name], desc = S("Daily interest (@1%)", CREDIT_INTEREST_RATE * 100), other = "Bank" })
changes_made = true
end
end
end
if changes_made then
bank_accounts.save_all(data)
minetest.log("action", "[bank_accounts] Daily interest calculation complete.")
end
write_timestamp(os.time())
bank_accounts.is_calculating_interest = false
end
end)