local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
local shopWindow = {}
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 function removePoints(player, amount)
if not player then return false end
local accountId = player:getAccountId()
return db.query(string.format("UPDATE `accounts` SET `premium_points` = GREATEST(0, `premium_points` - %d) WHERE `id` = %d", amount, accountId))
end
local function getPoints(player)
if not player then return 0 end
local accountId = player:getAccountId()
local resultId = db.storeQuery(string.format("SELECT `premium_points` FROM `accounts` WHERE `id` = %d", accountId))
if resultId then
local points = result.getDataInt(resultId, "premium_points")
result.free(resultId)
return points
end
return 0
end
-- Items available for purchase with their premium points cost
local t = {
[12396] = {price = 400},
[12575] = {price = 400},
[7440] = {price = 200},
[7443] = {price = 400},
[8981] = {price = 600},
[5468] = {price = 250},
[2346] = {price = 200}
}
local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)
local player = Player(cid)
if not player then return false end
if not t[item] then
selfSay("Sorry, I don't sell this item.", cid)
return false
end
local totalCost = t[item].price * amount
local playerPoints = getPoints(player)
if playerPoints >= totalCost then
if removePoints(player, totalCost) then
if player:addItem(item, amount) then
selfSay("Here is your item!", cid)
else
selfSay("Sorry, you don't have enough space in your inventory.", cid)
return false
end
end
else
selfSay(string.format("You don't have enough premium points. You need %d premium points.", totalCost), cid)
return false
end
return true
end
function creatureSayCallback(cid, type, msg)
if not npcHandler:isFocused(cid) then
return false
end
local talkUser = NPCHANDLER_CONVbehavior == CONVERSATION_DEFAULT and 0 or cid
local shopWindow = {}
msg = msg:lower()
if (msgcontains(msg, 'trade') or msgcontains(msg, 'troca')) then
for itemId, itemInfo in pairs(t) do
local itemType = ItemType(itemId)
local itemName = itemType:getName()
table.insert(shopWindow, {
id = itemId,
subType = 0,
buy = itemInfo.price,
sell = 0,
name = itemName
})
end
openShopWindow(cid, shopWindow, onBuy, function() end)
elseif (msgcontains(msg, 'balance') or msgcontains(msg, 'points') or msgcontains(msg, 'saldo')) then
local player = Player(cid)
if player then
local playerPoints = getPoints(player)
selfSay(string.format("You currently have %d premium points.", playerPoints), cid)
end
else
selfSay("I don't understand. Say 'trade' to see available items or 'balance' to check your premium points.", cid)
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())