local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local Topic, Amount = {}, {}
function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
local per = 90 -- percentage that will be transferred to player
function getPoints(cid)
local res, value = db.getResult("SELECT `premium_points` FROM `accounts` WHERE `name` = '" .. db.escapeString(getPlayerAccount(cid)) .. "' LIMIT 1;"), 0
if res:getID() ~= -1 then
value = res:getDataInt("premium_points")
res:free()
end
return value
end
function greetCallback(cid)
Topic[cid], Amount[cid] = 0, nil
return true
end
function creatureSayCallback(cid, type, msg)
if not npcHandler:isFocused(cid) then
return false
elseif msgcontains(msg, 'transfer') or msgcontains(msg, 'yes') then
selfSay("Ok, You have "..getPoints(cid).." premium points, how much you want to transfer?",cid)
Topic[cid] = 1
elseif Topic[cid] == 1 then
msg = tonumber(msg)
if not msg then
return selfSay("You must type the amount of points, try again!",cid)
end
msg = math.abs(msg)
if msg > getPoints(cid) then
return selfSay("You only have "..getPoints(cid)..", try again!",cid)
end
selfSay("Who do you want to transfer to? Remember that the comission is {".. 100-per.."%}, so you will only transfer {"..math.floor(msg * (per/100)).."} points, and {"..getPoints(cid) - math.floor(msg * (per/100)).."} points will go to me.",cid)
Topic[cid] = 2
Amount[cid] = msg
elseif Topic[cid] == 2 then
local to = getAccountIdByName(msg)
if to > 0 then
db.executeQuery("UPDATE `accounts` SET `premium_points` = `premium_points` - " .. Amount[cid] .. " WHERE `id` = " .. getPlayerAccountId(cid) .. " LIMIT 1;")
db.executeQuery("UPDATE `accounts` SET `premium_points` = `premium_points` + " .. math.floor(Amount[cid] * (per/100)) .. " WHERE `id` = " .. to .. " LIMIT 1;")
selfSay("You have successfuly transfered " .. math.floor(Amount[cid] * (per/100)) .. " points to " .. msg .. ".",cid)
Topic[cid], Amount[cid] = 0, nil
else
selfSay("Player doesnt exist, try again!",cid)
end
end
return true
end
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())