Advertisement
Guest User

Untitled

a guest
Oct 16th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.98 KB | None | 0 0
  1. local keywordHandler = KeywordHandler:new()
  2. local npcHandler = NpcHandler:new(keywordHandler)
  3. NpcSystem.parseParameters(npcHandler)
  4.  
  5. local Topic, count, transfer = {}, {}, {}
  6.  
  7. function onCreatureAppear(cid)            npcHandler:onCreatureAppear(cid)        end
  8. function onCreatureDisappear(cid)        npcHandler:onCreatureDisappear(cid)        end
  9. function onCreatureSay(cid, type, msg)        npcHandler:onCreatureSay(cid, type, msg)    end
  10. function onThink()                npcHandler:onThink()                end
  11.  
  12.  
  13. local function getCount(s)
  14.     local b, e = s:find('%d+')
  15.     return b and e and math.min(4294967295, tonumber(s:sub(b, e))) or -1
  16. end
  17.  
  18. local function findPlayer(name)
  19.     local resultId = db.storeQuery('SELECT name FROM players WHERE name=' .. db.escapeString(name) .. ' LIMIT 1'), nil
  20.     if resultId == false then
  21.         return
  22.     end
  23.     local r = result.getDataString(resultId, "name")
  24.     result.free(resultId)
  25.     return r
  26. end
  27.  
  28. local function vocation(name)
  29.     local resultId = db.storeQuery('SELECT vocation FROM players WHERE name=' .. db.escapeString(name) .. ' LIMIT 1'), nil
  30.     if resultId == false then
  31.         return
  32.     end
  33.     local r = result.getDataInt(resultId, "vocation")
  34.     result.free(resultId)
  35.     return r
  36. end
  37.  
  38. local function updatePlayerBalance(name, value)
  39.     db.query('UPDATE players SET balance=' .. value .. ' WHERE name=' .. db.escapeString(name) .. ' LIMIT 1')
  40. end
  41.  
  42. function greet(cid)
  43.     Topic[cid], count[cid], transfer[cid] = nil, nil, nil
  44.     return true
  45. end
  46.  
  47. function creatureSayCallback(cid, type, msg)
  48.     if not npcHandler:isFocused(cid) then
  49.         return false
  50.     elseif msgcontains(msg, 'balance') then
  51.         npcHandler:say('Your account balance is ' .. getPlayerBalance(cid) .. ' gold.', cid)
  52.         Topic[cid] = nil
  53.     elseif msgcontains(msg, 'deposit') and msgcontains(msg, 'all') then
  54.         if getPlayerMoney(cid) == 0 then
  55.             npcHandler:say('You don\'t have any gold with you.', cid)
  56.             Topic[cid] = nil
  57.         else
  58.             count[cid] = getPlayerMoney(cid)
  59.             npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
  60.             Topic[cid] = 2
  61.         end
  62.     elseif msgcontains(msg, 'deposit') then
  63.         if getCount(msg) == 0 then
  64.             npcHandler:say('You are joking, aren\'t you??', cid)
  65.             Topic[cid] = nil
  66.         elseif getCount(msg) ~= -1 then
  67.             if getPlayerMoney(cid) >= getCount(msg) then
  68.                 count[cid] = getCount(msg)
  69.                 npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
  70.                 Topic[cid] = 2
  71.             else
  72.                 npcHandler:say('You do not have enough gold.', cid)
  73.                 Topic[cid] = nil
  74.             end
  75.         elseif getPlayerMoney(cid) == 0 then
  76.             npcHandler:say('You don\'t have any gold with you.', cid)
  77.             Topic[cid] = nil
  78.         else
  79.             npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
  80.             Topic[cid] = 1
  81.         end
  82.     elseif Topic[cid] == 1 then
  83.         if getCount(msg) == -1 then
  84.             npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
  85.             Topic[cid] = 1
  86.         elseif getPlayerMoney(cid) >= getCount(msg) then
  87.             count[cid] = getCount(msg)
  88.             npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
  89.             Topic[cid] = 2
  90.         else
  91.             npcHandler:say('You do not have enough gold.', cid)
  92.             Topic[cid] = nil
  93.         end
  94.     elseif msgcontains(msg, 'yes') and Topic[cid] == 2 then
  95.         if doPlayerRemoveMoney(cid, count[cid]) then
  96.             doPlayerSetBalance(cid, getPlayerBalance(cid) + count[cid])
  97.             updatePlayerBalance(getCreatureByName(cid), getPlayerBalance(cid))
  98.             npcHandler:say('Alright, we have added the amount of ' .. count[cid] .. ' gold to your balance. You can withdraw your money anytime you want to.', cid)
  99.         else
  100.             npcHandler:say('I am inconsolable, but it seems you have lost your gold. I hope you get it back.', cid)
  101.         end
  102.         Topic[cid] = nil
  103.     elseif msgcontains(msg, 'no') and Topic[cid] == 2 then
  104.         npcHandler:say('As you wish. Is there something else I can do for you?', cid)
  105.         Topic[cid] = nil
  106.     elseif msgcontains(msg, 'withdraw') then
  107.         if getCount(msg) == 0 then
  108.             npcHandler:say('Sure, you want nothing you get nothing!', cid)
  109.             Topic[cid] = nil
  110.         elseif getCount(msg) ~= -1 then
  111.             if getPlayerBalance(cid) >= getCount(msg) then
  112.                 count[cid] = getCount(msg)
  113.                 npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
  114.                 Topic[cid] = 4
  115.             else
  116.                 npcHandler:say('There is not enough gold on your account.', cid)
  117.                 Topic[cid] = nil
  118.             end
  119.         elseif getPlayerBalance(cid) == 0 then
  120.             npcHandler:say('You don\'t have any money on your bank account.', cid)
  121.             Topic[cid] = nil
  122.         else
  123.             npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
  124.             Topic[cid] = 3
  125.         end
  126.     elseif Topic[cid] == 3 then
  127.         if getCount(msg) == -1 then
  128.             npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
  129.             Topic[cid] = 3
  130.         elseif getPlayerBalance(cid) >= getCount(msg) then
  131.             count[cid] = getCount(msg)
  132.             npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
  133.             Topic[cid] = 4
  134.         else
  135.             npcHandler:say('There is not enough gold on your account.', cid)
  136.             Topic[cid] = nil
  137.         end
  138.     elseif msgcontains(msg, 'yes') and Topic[cid] == 4 then
  139.         if getPlayerBalance(cid) >= count[cid] then
  140.             doPlayerAddMoney(cid, count[cid])
  141.             doPlayerSetBalance(cid, getPlayerBalance(cid) - count[cid])
  142.             updatePlayerBalance(getCreatureByName(cid), getPlayerBalance(cid))
  143.             npcHandler:say('Here you are, ' .. count[cid] .. ' gold. Please let me know if there is something else I can do for you.', cid)
  144.         else
  145.             npcHandler:say('There is not enough gold on your account.', cid)
  146.         end
  147.         Topic[cid] = nil
  148.     elseif msgcontains(msg, 'no') and Topic[cid] == 4 then
  149.         npcHandler:say('The customer is king! Come back anytime you want to if you wish to withdraw your money.', cid)
  150.         Topic[cid] = nil
  151.     elseif msgcontains(msg, 'transfer') then
  152.         if getCount(msg) == 0 then
  153.             npcHandler:say('Please think about it. Okay?', cid)
  154.             Topic[cid] = nil
  155.         elseif getCount(msg) ~= -1 then
  156.        
  157.             count[cid] = getCount(msg)
  158.             if getPlayerBalance(cid) >= count[cid] then
  159.                 npcHandler:say('Who would you like to transfer ' .. count[cid] .. ' gold to?', cid)
  160.                 Topic[cid] = 6
  161.             else
  162.                 npcHandler:say('There is not enough gold on your account.', cid)
  163.                 Topic[cid] = nil
  164.             end
  165.  
  166.         else
  167.             npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
  168.             Topic[cid] = 5
  169.         end
  170.     elseif Topic[cid] == 5 then
  171.         if getCount(msg) == -1 then
  172.             npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
  173.             Topic[cid] = 5
  174.         else
  175.             count[cid] = getCount(msg)
  176.             if getPlayerBalance(cid) >= count[cid] then
  177.                 npcHandler:say('Who would you like to transfer ' .. count[cid] .. ' gold to?', cid)
  178.                 Topic[cid] = 6
  179.             else
  180.                 npcHandler:say('There is not enough gold on your account.', cid)
  181.                 Topic[cid] = nil
  182.             end
  183.         end
  184.     elseif Topic[cid] == 6 then
  185.         local v = getCreatureByName(msg)
  186.         if getPlayerBalance(cid) >= count[cid] then
  187.             local temp = getCreatureName(v)
  188.             if temp and (getPlayerTown(msg:lower()) ~= 7 or getPlayerTown(msg:lower()) ~= 9) then
  189.                 transfer[cid] = msg
  190.                 npcHandler:say('Would you really like to transfer ' .. count[cid] .. ' gold to ' .. temp .. '?', cid)
  191.                 Topic[cid] = 7
  192.             elseif getPlayerGUIDByName(msg:lower()) ~= 0 and getPlayerTown(msg:lower()) ~= 7 and getPlayerTown(msg:lower()) ~= 9 then
  193.                 transfer[cid] = msg
  194.                 npcHandler:say('Would you really like to transfer ' .. count[cid] .. ' gold to ' .. findPlayer(msg) .. '?', cid)
  195.                 Topic[cid] = 7              
  196.             else
  197.                 npcHandler:say('This player does not exist on this world or is from Rookgaardia/Dawn Hills.', cid)
  198.                 Topic[cid] = nil
  199.             end
  200.         else
  201.             npcHandler:say('There is not enough gold on your account.', cid)
  202.             Topic[cid] = nil
  203.         end
  204.     elseif Topic[cid] == 7 and msgcontains(msg, 'yes') then
  205.         if getPlayerBalance(cid) >= count[cid] then
  206.             local v = getCreatureByName(transfer[cid])
  207.             if v then
  208.                 doPlayerSetBalance(cid, getPlayerBalance(cid) - count[cid])
  209.                 updatePlayerBalance(getCreatureByName(cid), getPlayerBalance(cid))
  210.                 doPlayerSetBalance(v, getPlayerBalance(v) + count[cid])
  211.                 updatePlayerBalance(getCreatureByName(v), getPlayerBalance(v))
  212.                 npcHandler:say('Very well. You have transferred ' .. count[cid] .. ' gold to ' .. getCreatureName(v) .. '.', cid)
  213.             elseif findPlayer(transfer[cid]):lower() == transfer[cid]:lower() then
  214.                 doPlayerSetBalance(cid, getPlayerBalance(cid) - count[cid])
  215.                 updatePlayerBalance(getCreatureByName(cid), getPlayerBalance(cid))
  216.                 db.query('UPDATE players SET balance=balance+' .. count[cid] .. ' WHERE name=' .. db.escapeString(transfer[cid]) .. ' LIMIT 1')
  217.                 npcHandler:say('Very well. You have transferred ' .. count[cid] .. ' gold to ' .. findPlayer(transfer[cid]) .. '.', cid)
  218.             else
  219.                 npcHandler:say('This player does not exist.', cid)
  220.             end
  221.         else
  222.             npcHandler:say('There is not enough gold on your account.', cid)
  223.         end
  224.         Topic[cid] = nil
  225.     elseif Topic[cid] == 7 and msgcontains(msg, 'no') then
  226.         npcHandler:say('Alright, is there something else I can do for you?', cid)
  227.         Topic[cid] = nil
  228.     elseif msgcontains(msg, 'change gold') then
  229.         npcHandler:say('How many platinum coins would you like to get?', cid)
  230.         Topic[cid] = 8
  231.     elseif Topic[cid] == 8 then
  232.         if getCount(msg) < 1 then
  233.             npcHandler:say('Hmm, can I help you with something else?', cid)
  234.             Topic[cid] = nil
  235.         else
  236.             count[cid] = math.min(500, getCount(msg))
  237.             npcHandler:say('So you would like me to change ' .. count[cid] * 100 .. ' of your gold coins into ' .. count[cid] .. ' platinum coins?', cid)
  238.             Topic[cid] = 9
  239.         end
  240.     elseif Topic[cid] == 9 then
  241.         if msgcontains(msg, 'yes') then
  242.             if doPlayerRemoveItem(cid, 2148, count[cid] * 100) then
  243.                 npcHandler:say('Here you are.', cid)
  244.                 doPlayerAddItem(cid, 2152, count[cid])
  245.             else
  246.                 npcHandler:say('Sorry, you do not have enough gold coins.', cid)
  247.             end
  248.         else
  249.             npcHandler:say('Well, can I help you with something else?', cid)
  250.         end
  251.         Topic[cid] = nil
  252.     elseif msgcontains(msg, 'change platinum') then
  253.         npcHandler:say('Would you like to change your platinum coins into gold or crystal?', cid)
  254.         Topic[cid] = 10
  255.     elseif Topic[cid] == 10 then
  256.         if msgcontains(msg, 'gold') then
  257.             npcHandler:say('How many platinum coins would you like to change into gold?', cid)
  258.             Topic[cid] = 11
  259.         elseif msgcontains(msg, 'crystal') then
  260.             npcHandler:say('How many crystal coins would you like to get?', cid)
  261.             Topic[cid] = 13
  262.         else
  263.             npcHandler:say('Well, can I help you with something else?', cid)
  264.             Topic[cid] = nil
  265.         end
  266.     elseif Topic[cid] == 11 then
  267.         if getCount(msg) < 1 then
  268.             npcHandler:say('Hmm, can I help you with something else?', cid)
  269.             Topic[cid] = nil
  270.         else
  271.             count[cid] = math.min(500, getCount(msg))
  272.             npcHandler:say('So you would like me to change ' .. count[cid] .. ' of your platinum coins into ' .. count[cid] * 100 .. ' gold coins for you?', cid)
  273.             Topic[cid] = 12
  274.         end
  275.     elseif Topic[cid] == 12 then
  276.         if msgcontains(msg, 'yes') then
  277.             if doPlayerRemoveItem(cid, 2152, count[cid]) then
  278.                 npcHandler:say('Here you are.', cid)
  279.                 doPlayerAddItem(cid, 2148, count[cid] * 100)
  280.             else
  281.                 npcHandler:say('Sorry, you do not have enough platinum coins.', cid)
  282.             end
  283.         else
  284.             npcHandler:say('Well, can I help you with something else?', cid)
  285.         end
  286.         Topic[cid] = nil
  287.     elseif Topic[cid] == 13 then
  288.         if getCount(msg) < 1 then
  289.             npcHandler:say('Hmm, can I help you with something else?', cid)
  290.             Topic[cid] = nil
  291.         else
  292.             count[cid] = math.min(500, getCount(msg))
  293.             npcHandler:say('So you would like me to change ' .. count[cid] * 100 .. ' of your platinum coins into ' .. count[cid] .. ' crystal coins for you?', cid)
  294.             Topic[cid] = 14
  295.         end
  296.     elseif Topic[cid] == 14 then
  297.         if msgcontains(msg, 'yes') then
  298.             if doPlayerRemoveItem(cid, 2152, count[cid] * 100) then
  299.                 npcHandler:say('Here you are.', cid)
  300.                 doPlayerAddItem(cid, 2160, count[cid])
  301.             else
  302.                 npcHandler:say('Sorry, you do not have enough platinum coins.', cid)
  303.             end
  304.         else
  305.             npcHandler:say('Well, can I help you with something else?', cid)
  306.         end
  307.         Topic[cid] = nil
  308.     elseif msgcontains(msg, 'change crystal') then
  309.         npcHandler:say('How many crystal coins would you like to change into platinum?', cid)
  310.         Topic[cid] = 15
  311.     elseif Topic[cid] == 15 then
  312.         if getCount(msg) == -1 or getCount(msg) == 0 then
  313.             npcHandler:say('Hmm, can I help you with something else?', cid)
  314.             Topic[cid] = nil
  315.         else
  316.             count[cid] = math.min(500, getCount(msg))
  317.             npcHandler:say('So you would like me to change ' .. count[cid] .. ' of your crystal coins into ' .. count[cid] * 100 .. ' platinum coins for you?', cid)
  318.             Topic[cid] = 16
  319.         end
  320.     elseif Topic[cid] == 16 then
  321.         if msgcontains(msg, 'yes') then
  322.             if doPlayerRemoveItem(cid, 2160, count[cid]) then
  323.                 npcHandler:say('Here you are.', cid)
  324.                 doPlayerAddItem(cid, 2152, count[cid] * 100)
  325.             else
  326.                 npcHandler:say('Sorry, you do not have enough crystal coins.', cid)
  327.             end
  328.         else
  329.             npcHandler:say('Well, can I help you with something else?', cid)
  330.         end
  331.         Topic[cid] = nil
  332.     elseif msgcontains(msg, 'change') then
  333.         npcHandler:say('There are three different coin types in Tibia: 100 gold coins equal 1 platinum coin, 100 platinum coins equal 1 crystal coin. So if you\'d like to change 100 gold into 1 platinum, simply say \'{change gold}\' and then \'1 platinum\'.', cid)
  334.         Topic[cid] = nil
  335.     elseif msgcontains(msg, 'bank') then
  336.         npcHandler:say('We can change money for you. You can also access your bank account.', cid)
  337.         Topic[cid] = nil
  338.     end
  339.     return true
  340. end
  341.  
  342. npcHandler:setCallback(CALLBACK_GREET, greet)
  343. npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
  344. npcHandler:addModule(FocusModule:new())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement