With this script players will receive an interest in their bank account each month (30th, 27th for February).
The script works in a global event with an interval of two hours, but relax, players won't receive interest every 2 hours, just once at day and at month.
First, execute this in MySQL
Now, create a file in globalevents/scripts and paste this:
Paste this at globalevents.xml:
Enjoy it and report bugs if you find it.
- You can configure the amount of interest gained for free and premium accounts.
- The info about every interest given can be readed in a log file, declared by yourself.
The script works in a global event with an interval of two hours, but relax, players won't receive interest every 2 hours, just once at day and at month.
First, execute this in MySQL
SQL:
CREATE TABLE IF NOT EXISTS `bank_interest` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`day` int(11) NOT NULL DEFAULT '0',
`month` int(10) unsigned NOT NULL DEFAULT '0',
`year` int(11) NOT NULL DEFAULT '0',
`given` int(11) NOT NULL DEFAULT '0',
`highest` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
Now, create a file in globalevents/scripts and paste this:
Lua:
local minBalance = 100 --min balance required to gain interest.
local interest =
{
free = 2, --2%
premmy = 2.2 --2.2%
}
local day = 30
local log = --save info in a log file?
{
enabled = true,
dir = "data/logs/bankInterest.txt"
}
function onThink(interval, lastExecution, thinkInterval)
local t = os.date('*t')
if t.month == 2 then --February
day = 27
end
local total = 0
local highest = {}
local l = db.getResult("select * from bank_interest order by id desc;")
local checkMonth = false
if l:getID() ~= -1 and l:getRows() > 0 then
checkMonth = true
end
if t.day == day then
if checkMonth and t.month == l:getDataInt("month") then
return true
end
local f = io.open(log.dir, "a+")
local query = db.getResult("select * from players;")
if log.enabled and f ~= nil then
f:write("\n\n---------------------------------------------------", "\nStartup at " .. os.date() .. ":\n\n")
end
if query:getID() ~= -1 then
while(true) do
local balance = query:getDataInt("balance")
if balance >= minBalance then
total = total + 1
local st = db.getResult("select * from accounts where id = " .. query:getDataInt("account_id") .. ";"):getDataInt("premdays")
if log.enabled and f ~= nil then
f:write("\n>> Added " .. math.floor((query:getDataInt("balance") * (st > 0 and interest.premmy or interest.free) / 100)) .. " gold to the account balance of " .. query:getDataString("name"))
end
table.insert(highest, math.floor((query:getDataInt("balance") * (st > 0 and interest.premmy or interest.free) / 100)))
db.executeQuery("update players set balance = balance + " .. math.floor((query:getDataInt("balance") * (st > 0 and interest.premmy or interest.free) / 100)) .. " where id = " .. query:getDataInt("id") .. ";")
end
if not query:next() then break end
end
table.sort(highest, function(a, b) return a > b end)
db.executeQuery("insert into bank_interest values (null, " .. t.day .. ", " .. t.month .. ", " .. t.year .. ", " .. total .. ", " .. highest[1] .. ");")
end
if log.enabled and f ~= nil then
f:write("\n---------------------------------------------------")
f:close()
end
end
return true
end
Paste this at globalevents.xml:
XML:
<globalevent name="bankInterest" interval="7200" event="script" value="bankInterest.lua"/>
Enjoy it and report bugs if you find it.