potinho
Advanced OT User
Hello guys,
I'm trying to implement on my server an NPC that sells some stuff for premium points. I got this one from another post, but when I say "list" or "buy something" to him the console crashes and the server crashes. can you help me? I use Gesior 2012.
donatios.lua
in lib-function i have:
I'm trying to implement on my server an NPC that sells some stuff for premium points. I got this one from another post, but when I say "list" or "buy something" to him the console crashes and the server crashes. can you help me? I use Gesior 2012.
donatios.lua
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
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 getPremiumPoints(cid)
local res = db.getResult('SELECT premium_points FROM accounts WHERE account_id = '..getPlayerAccountId(cid))
if(res:getID() == -1) then
return 0
end
local ret = res:getDataInt("points")
res:free()
return tonumber(ret)
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 items = {
["Premium Coin"] = {points = 1, itemid = 5263},
["exp scrollt"] = {points = 2, itemid = 5161}
}
local function addCptl(first, rest)
return first:upper()..rest:lower()
end
local x = items[msg:gsub("(%a)([%w_']*)", addCptl)]
local buy = (msgcontains(msg, 'buy'))
if buy then
selfSay('Which donate item would you like to buy?', cid)
talkState[talkUser] = 2
end
if x and talkState[talkUser] >= 1 then
selfSay('Do you want to buy 1 '..msg..' for '..x.points..' premium points?', cid)
xmsg = msg
talkState[talkUser] = 3
end
local agree = (msgcontains(msg, 'yes'))
if agree and talkState[talkUser] == 3 then
x = items[xmsg:gsub("(%a)([%w_']*)", addCptl)]
if getPremiumPoints(cid) >= x.points then
selfSay('Here you are, have fun with it.', cid)
doPlayerAddItem(cid, x.itemid, 1)
db.executeQuery('UPDATE accounts SET premium_points=points-'..x.points..' WHERE account_id=' .. getPlayerAccountId(cid))
talkState[talkUser] = 1
else
selfSay('You don\'t have enough points.', cid)
talkState[talkUser] = 1
end
end
local list = (msgcontains(msg, 'list'))
if list then
text = 'Donation Items\n'
for i, x in pairs(items) do
text = text .. "\n" .. i .. " - "..x.points.." points"
end
doShowTextDialog(cid, 7391, "" .. text)
talkState[talkUser] = 1
end
if not x and not list and not buy and not agree and talkState[talkUser] == 2 then
selfSay('You can\'t buy this item from me, look in the {list} which items you can buy.', cid)
elseif not x and not list and not buy and not agree and talkState[talkUser] ~= 2 then
selfSay('What? I don\'t understand what you mean with '..msg..'.', cid)
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
in lib-function i have:
Lua:
function getAccountPoints(cid)
local res = db.getResult('select `premium_points` from accounts where name = \''..getPlayerAccount(cid)..'\'')
if(res:getID() == -1) then
return false
end
local ret = res:getDataInt("premium_points")
res:free()
return tonumber(ret)
end
function doAccountRemovePoints(cid, count)
return db.executeQuery("UPDATE `accounts` SET `premium_points` = '".. getAccountPoints(cid) - count .."' WHERE `name` ='"..getPlayerAccount(cid).."'")
end