Advertisement
Guest User

Sir Islam

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