beeki

Bank by Beeki.

May 29th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.95 KB | None | 0 0
  1. local config = {
  2.         pin = false, -- players can protect their money with pin code (like in cash machines) (true/false)
  3.         pinMinLength = 4, -- minimum pin length
  4.         pinMaxLength = 4, -- maximum pin length
  5.         pinStorage = 3006, -- only if pin enabled (used to store player pin)
  6.         transferDisabledVocations = {0} -- disable non vocation characters
  7. }
  8.  
  9. local talkState = {}
  10. local count = {}
  11. local transfer = {}
  12. local pin = {}
  13.  
  14. local keywordHandler = KeywordHandler:new()
  15. local npcHandler = NpcHandler:new(keywordHandler)
  16. NpcSystem.parseParameters(npcHandler)
  17.  
  18. function onCreatureAppear(cid)                  npcHandler:onCreatureAppear(cid)                end
  19. function onCreatureDisappear(cid)               npcHandler:onCreatureDisappear(cid)             end
  20. function onCreatureSay(cid, type, msg)          npcHandler:onCreatureSay(cid, type, msg)        end
  21. function onThink()                              npcHandler:onThink()                            end
  22.  
  23. if(config.pin) then
  24.         bank_pin = {
  25.                 get = function(cid)
  26.                         return getPlayerStorageValue(cid, config.pinStorage)
  27.                 end,
  28.  
  29.                 set = function(cid, code)
  30.                         return setPlayerStorageValue(cid, config.pinStorage, code)
  31.                 end,
  32.  
  33.                 logged = function(cid)
  34.                         return pin[cid] == bank_pin.get(cid)
  35.                 end,
  36.  
  37.                 validate = function(code)
  38.                         if(not isNumber(code)) then
  39.                                 return false
  40.                         end
  41.  
  42.                         local length = tostring(code):len()
  43.                         return (length >= config.pinMinLength and length <= config.pinMaxLength)
  44.                 end
  45.         }
  46. end
  47.  
  48. if(not getPlayerBalance) then
  49.         getPlayerBalance = function(cid)
  50.                 local result = db.getResult("SELECT `balance` FROM `players` WHERE `id` = " .. getPlayerGUID(cid))
  51.                 if(result:getID() == -1) then
  52.                         return false
  53.                 end
  54.  
  55.                 local value = tonumber(result:getDataString("balance"))
  56.                 result:free()
  57.                 return value
  58.         end
  59.  
  60.         doPlayerSetBalance = function(cid, balance)
  61.                 db.executeQuery("UPDATE `players` SET `balance` = " .. balance .. " WHERE `id` = " .. getPlayerGUID(cid))
  62.                 return true
  63.         end
  64.  
  65.         doPlayerWithdrawMoney = function(cid, amount)
  66.                 local balance = getPlayerBalance(cid)
  67.                 if(amount > balance or not doPlayerAddMoney(cid, amount)) then
  68.                         return false
  69.                 end
  70.  
  71.                 doPlayerSetBalance(cid, balance - amount)
  72.                 return true
  73.         end
  74.  
  75.         doPlayerDepositMoney = function(cid, amount)
  76.                 if(not doPlayerRemoveMoney(cid, amount)) then
  77.                         return false
  78.                 end
  79.  
  80.                 doPlayerSetBalance(cid, getPlayerBalance(cid) + amount)
  81.                 return true
  82.         end
  83.  
  84.         doPlayerTransferMoneyTo = function(cid, target, amount)
  85.                 local balance = getPlayerBalance(cid)
  86.                 if(amount > balance) then
  87.                         return false
  88.                 end
  89.  
  90.                 local tid = getPlayerByName(target)
  91.                 if(tid > 0) then
  92.                         doPlayerSetBalance(tid, getPlayerBalance(tid) + amount)
  93.                 else
  94.                         if(playerExists(target) == FALSE) then
  95.                                 return false
  96.                         end
  97.  
  98.                         db.executeQuery("UPDATE `player_storage` SET `value` = `value` + '" .. amount .. "' WHERE `player_id` = (SELECT `id` FROM `players` WHERE `name` = '" .. escapeString(player) .. "') AND `key` = '" .. balance_storage .. "'")
  99.                 end
  100.  
  101.                 doPlayerSetBalance(cid, getPlayerBalance(cid) - amount)
  102.                 return true
  103.         end
  104. end
  105.  
  106. if(not doPlayerSave) then
  107.         local function doPlayerSave(cid)
  108.                 return true
  109.         end
  110. end
  111.  
  112. local function getPlayerVocationByName(name)
  113.         local result = db.getResult("SELECT `vocation` FROM `players` WHERE `name` = " .. db.escapeString(name))
  114.         if(result:getID() == -1) then
  115.                 return false
  116.         end
  117.  
  118.         local value = result:getDataString("vocation")
  119.         result:free()
  120.         return value
  121. end
  122.  
  123. local function isValidMoney(money)
  124.         return (isNumber(money) and money > 0 and money < 4294967296)
  125. end
  126.  
  127. local function getCount(string)
  128.         local b, e = string:find("%d+")
  129.         local money = b and e and tonumber(string:sub(b, e)) or -1
  130.         if(isValidMoney(money)) then
  131.                 return money
  132.         end
  133.         return -1
  134. end
  135.  
  136. function greetCallback(cid)
  137.         talkState[cid], count[cid], transfer[cid], pin[cid] = 0, nil, nil, nil
  138.         return true
  139. end
  140.  
  141. function creatureSayCallback(cid, type, msg)
  142.  
  143.         if(not npcHandler:isFocused(cid)) then
  144.                 return false
  145.         end
  146.  
  147. ---------------------------- pin -------------------------
  148.         if(config.pin) then
  149.                 if(talkState[cid] == "verify-pin") then
  150.                         talkState[cid] = 0
  151.                         pin[cid] = getCount(msg)
  152.                         if(not bank_pin.logged(cid)) then
  153.                                 selfSay("Invalid pin code entered. Please try again.", cid)
  154.                                 return true
  155.                         end
  156.  
  157.                         selfSay("You have been successfully logged in.", cid)
  158.                 elseif(talkState[cid] == "new-pin") then
  159.                         talkState[cid] = 0
  160.  
  161.                         if(bank_pin.get(cid) ~= -1 and not bank_pin.logged(cid)) then
  162.                                 selfSay("Please login before attempting to change your pin code.", cid)
  163.                                 talkState[cid] = "verify-pin"
  164.                                 return true
  165.                         end
  166.  
  167.                         if(msgcontains(msg, 'reset') or msgcontains(msg, 'remove') or msgcontains(msg, 'clear')) then
  168.                                 selfSay("Pin code has been removed.", cid)
  169.                                 pin[cid] = nil
  170.                                 bank_pin.set(cid, -1)
  171.                                 return true
  172.                         end
  173.  
  174.                         pin[cid] = getCount(msg)
  175.                         if(bank_pin.validate(pin[cid])) then
  176.                                 selfSay("Pin code successfully changed.", cid)
  177.                                 bank_pin.set(cid, pin[cid])
  178.                         else
  179.                                 local str = ""
  180.                                 if(config.pinMinLength ~= config.pinMaxLength) then
  181.                                         str = config.pinMinLength .. " - " .. config.pinMaxLength
  182.                                 else
  183.                                         str = config.pinMinLength
  184.                                 end
  185.  
  186.                                 selfSay("Invalid pin code entered. Your pin should contain " .. str .. " digits", cid)
  187.                         end
  188.  
  189.                         return true
  190.                 elseif(msgcontains(msg, 'balance') or
  191.                         msgcontains(msg, 'deposit') or
  192.                         msgcontains(msg, 'withdraw') or
  193.                         msgcontains(msg, 'transfer')) then
  194.                                 if(bank_pin.get(cid) ~= -1 and not bank_pin.logged(cid)) then
  195.                                         selfSay("Please tell me your bank pin code before making any transactions.", cid)
  196.                                         talkState[cid] = "verify-pin"
  197.                                         return true
  198.                                 end
  199.  
  200.                                 talkState[cid] = 0
  201.                 elseif(msgcontains(msg, 'login')) then
  202.                         talkState[cid] = "verify-pin"
  203.                         return true
  204.                 elseif(msgcontains(msg, 'pin')) then
  205.                         selfSay("Please tell me your new pin code.", cid)
  206.                         talkState[cid] = "new-pin"
  207.                         return true
  208.                 end
  209.         end
  210. ---------------------------- help ------------------------
  211.         if msgcontains(msg, 'advanced') then
  212.                 if isInArray(config.transferDisabledVocations, getPlayerVocation(cid)) then
  213.                         selfSay("Once you are on the Tibian mainland, you can access new functions of your bank account, such as transferring money to other players safely or taking part in house auctions.", cid)
  214.                 else
  215.                         selfSay("Renting a house has never been this easy. Simply make a bid for an auction. We will check immediately if you have enough money.", cid)
  216.                 end
  217.                 talkState[cid] = 0
  218.         elseif msgcontains(msg, 'help') or msgcontains(msg, 'functions') then
  219.                 selfSay("You can check the {balance} of your bank account, {deposit} money or {withdraw} it. You can also {transfer} money to other characters, provided that they have a vocation.", cid)
  220.                 talkState[cid] = 0
  221.         elseif msgcontains(msg, 'bank') then
  222.                 npcHandler:say("We can change money for you. You can also access your bank account.", cid)
  223.                 talkState[cid] = 0
  224.         elseif msgcontains(msg, 'job') then
  225.                 npcHandler:say("I work in this bank. I can change money for you and help you with your bank account.", cid)
  226.                 talkState[cid] = 0
  227. ---------------------------- balance ---------------------
  228.         elseif msgcontains(msg, 'balance') then
  229.                 selfSay("Your account balance is " .. getPlayerBalance(cid) .. " gold.", cid)
  230.                 talkState[cid] = 0
  231. ---------------------------- deposit ---------------------
  232.         elseif msgcontains(msg, 'deposit all') and getPlayerMoney(cid) > 0 then
  233.                 count[cid] = getPlayerMoney(cid)
  234.                 if not isValidMoney(count[cid]) then
  235.                         selfSay("Sorry, but you can't deposit that much.", cid)
  236.                         talkState[cid] = 0
  237.                         return false
  238.                 end
  239.  
  240.                 if count[cid] < 1 then
  241.                         selfSay("You don't have any money to deposit in you inventory..", cid)
  242.                         talkState[cid] = 0
  243.                 else
  244.                         selfSay("Would you really like to deposit " .. count[cid] .. " gold?", cid)
  245.                         talkState[cid] = 2
  246.                 end
  247.         elseif msgcontains(msg, 'deposit') then
  248.                 selfSay("Please tell me how much gold it is you would like to deposit.", cid)
  249.                 talkState[cid] = 1
  250.         elseif talkState[cid] == 1 then
  251.                 count[cid] = getCount(msg)
  252.                 if isValidMoney(count[cid]) then
  253.                         selfSay("Would you really like to deposit " .. count[cid] .. " gold?", cid)
  254.                         talkState[cid] = 2
  255.                 else
  256.                         selfSay("Is isnt valid amount of gold to deposit.", cid)
  257.                         talkState[cid] = 0
  258.                 end
  259.         elseif talkState[cid] == 2 then
  260.                 if msgcontains(msg, 'yes') then
  261.                         if not doPlayerDepositMoney(cid, count[cid]) then
  262.                                 selfSay("You don\'t have enough gold.", cid)
  263.                         else
  264.                                 selfSay("Alright, we have added the amount of " .. count[cid] .. " gold to your balance. You can withdraw your money anytime you want to. Your account balance is " .. getPlayerBalance(cid) .. ".", cid)
  265.                                 doPlayerSave(cid)
  266.                         end
  267.                 elseif msgcontains(msg, 'no') then
  268.                         selfSay("As you wish. Is there something else I can do for you?", cid)
  269.                 end
  270.                 talkState[cid] = 0
  271. ---------------------------- withdraw --------------------
  272.         elseif msgcontains(msg, 'withdraw') then
  273.                 selfSay("Please tell me how much gold you would like to withdraw.", cid)
  274.                 talkState[cid] = 6
  275.         elseif talkState[cid] == 6 then
  276.                 count[cid] = getCount(msg)
  277.                 if isValidMoney(count[cid]) then
  278.                         selfSay("Are you sure you wish to withdraw " .. count[cid] .. " gold from your bank account?", cid)
  279.                         talkState[cid] = 7
  280.                 else
  281.                         selfSay("Is isnt valid amount of gold to withdraw.", cid)
  282.                         talkState[cid] = 0
  283.                 end
  284.         elseif talkState[cid] == 7 then
  285.                 if msgcontains(msg, 'yes') then
  286.                         if not doPlayerWithdrawMoney(cid, count[cid]) then
  287.                                 selfSay("There is not enough gold on your account. Your account balance is " .. getPlayerBalance(cid) .. ". Please tell me the amount of gold coins you would like to withdraw.", cid)
  288.                                 talkState[cid] = 0
  289.                         else
  290.                                 selfSay("Here you are, " .. count[cid] .. " gold. Please let me know if there is something else I can do for you.", cid)
  291.                                 talkState[cid] = 0
  292.                                 doPlayerSave(cid)
  293.                         end
  294.                 elseif msgcontains(msg, 'no') then
  295.                         selfSay("As you wish. Is there something else I can do for you?", cid)
  296.                         talkState[cid] = 0
  297.                 end
  298. ---------------------------- transfer --------------------
  299.         elseif msgcontains(msg, 'transfer') then
  300.                 selfSay("Please tell me the amount of gold you would like to transfer.", cid)
  301.                 talkState[cid] = 11
  302.         elseif talkState[cid] == 11 then
  303.                 count[cid] = getCount(msg)
  304.                 if getPlayerBalance(cid) < count[cid] then
  305.                         selfSay("You dont have enough money on your bank account.", cid)
  306.                         talkState[cid] = 0
  307.                         return true
  308.                 end
  309.  
  310.                 if isValidMoney(count[cid]) then
  311.                         selfSay("Who would you like transfer " .. count[cid] .. " gold to?", cid)
  312.                         talkState[cid] = 12
  313.                 else
  314.                         selfSay("Is isnt valid amount of gold to transfer.", cid)
  315.                         talkState[cid] = 0
  316.                 end
  317.         elseif talkState[cid] == 12 then
  318.                 transfer[cid] = msg
  319.  
  320.                 if getCreatureName(cid) == transfer[cid] then
  321.                         selfSay("Ekhm, You want transfer money to yourself? Its impossible!", cid)
  322.                         talkState[cid] = 0
  323.                         return true
  324.                 end
  325.  
  326.                 if isInArray(config.transferDisabledVocations, getPlayerVocation(cid)) then
  327.                         selfSay("Your vocation cannot transfer money.", cid)
  328.                         talkState[cid] = 0
  329.                 end
  330.  
  331.                 if playerExists(transfer[cid]) then
  332.                         selfSay("So you would like to transfer " .. count[cid] .. " gold to \"" .. transfer[cid] .. "\" ?", cid)
  333.                         talkState[cid] = 13
  334.                 else
  335.                         selfSay("Player with name \"" .. transfer[cid] .. "\" doesnt exist.", cid)
  336.                         talkState[cid] = 0
  337.                 end
  338.         elseif talkState[cid] == 13 then
  339.                 if msgcontains(msg, 'yes') then
  340.                         local targetVocation = getPlayerVocationByName(transfer[cid])
  341.                         if not targetVocation or isInArray(config.transferDisabledVocations, targetVocation) or not doPlayerTransferMoneyTo(cid, transfer[cid], count[cid]) then
  342.                                 selfSay("This player does not exist on this world or have no vocation.", cid)
  343.                         else
  344.                                 selfSay("You have transferred " .. count[cid] .. " gold to \"" .. transfer[cid] .."\".", cid)
  345.                                 transfer[cid] = nil
  346.                                 doPlayerSave(cid)
  347.                         end
  348.                 elseif msgcontains(msg, 'no') then
  349.                         selfSay("As you wish. Is there something else I can do for you?", cid)
  350.                 end
  351.                 talkState[cid] = 0
  352. ---------------------------- money exchange --------------
  353.         elseif msgcontains(msg, 'change gold') then
  354.                 npcHandler:say("How many platinum coins would you like to get?", cid)
  355.                 talkState[cid] = 14
  356.         elseif talkState[cid] == 14 then
  357.                 if getCount(msg) == -1 or getCount(msg) == 0 then
  358.                         npcHandler:say("Hmm, can I help you with something else?", cid)
  359.                         talkState[cid] = 0
  360.                 else
  361.                         count[cid] = getCount(msg)
  362.                         npcHandler:say("So you would like me to change " .. count[cid] * 100 .. " of your gold coins into " .. count[cid] .. " platinum coins?", cid)
  363.                         talkState[cid] = 15
  364.                 end
  365.         elseif talkState[cid] == 15 then
  366.                 if msgcontains(msg, 'yes') then
  367.                         if doPlayerRemoveItem(cid, 2148, count[cid] * 100) then
  368.                                 doPlayerAddItem(cid, 2152, count[cid])
  369.                                 npcHandler:say("Here you are.", cid)
  370.                         else
  371.                                 npcHandler:say("Sorry, you do not have enough gold coins.", cid)
  372.                         end
  373.                 else
  374.                         npcHandler:say("Well, can I help you with something else?", cid)
  375.                 end
  376.                 talkState[cid] = 0
  377.         elseif msgcontains(msg, 'change platinum') then
  378.                 npcHandler:say("Would you like to change your platinum coins into gold or crystal?", cid)
  379.                 talkState[cid] = 16
  380.         elseif talkState[cid] == 16 then
  381.                 if msgcontains(msg, 'gold') then
  382.                         npcHandler:say("How many platinum coins would you like to change into gold?", cid)
  383.                         talkState[cid] = 17
  384.                 elseif msgcontains(msg, 'crystal') then
  385.                         npcHandler:say("How many crystal coins would you like to get?", cid)
  386.                         talkState[cid] = 19
  387.                 else
  388.                         npcHandler:say("Well, can I help you with something else?", cid)
  389.                         talkState[cid] = 0
  390.                 end
  391.         elseif talkState[cid] == 17 then
  392.                 if getCount(msg) == -1 or getCount(msg) == 0 then
  393.                         npcHandler:say("Hmm, can I help you with something else?", cid)
  394.                         talkState[cid] = 0
  395.                 else
  396.                         count[cid] = getCount(msg)
  397.                         npcHandler:say("So you would like me to change " .. count[cid] .. " of your platinum coins into " .. count[cid] * 100 .. " gold coins for you?", cid)
  398.                         talkState[cid] = 18
  399.                 end
  400.         elseif talkState[cid] == 18 then
  401.                 if msgcontains(msg, 'yes') then
  402.                         if doPlayerRemoveItem(cid, 2152, count[cid]) then
  403.                                 npcHandler:say("Here you are.", cid)
  404.                                 doPlayerAddItem(cid, 2148, count[cid] * 100)
  405.                         else
  406.                                 npcHandler:say("Sorry, you do not have enough platinum coins.", cid)
  407.                         end
  408.                 else
  409.                         npcHandler:say("Well, can I help you with something else?", cid)
  410.                 end
  411.                 talkState[cid] = 0
  412.         elseif talkState[cid] == 19 then
  413.                 if getCount(msg) == -1 or getCount(msg) == 0 then
  414.                         npcHandler:say("Hmm, can I help you with something else?", cid)
  415.                         talkState[cid] = 0
  416.                 else
  417.                         count[cid] = getCount(msg)
  418.                         npcHandler:say("So you would like me to change " .. count[cid] * 100 .. " of your platinum coins into " .. count[cid] .. " crystal coins for you?", cid)
  419.                         talkState[cid] = 20
  420.                 end
  421.         elseif talkState[cid] == 20 then
  422.                 if msgcontains(msg, 'yes') then
  423.                         if doPlayerRemoveItem(cid, 2152, count[cid] * 100) then
  424.                                 npcHandler:say("Here you are.", cid)
  425.                                 doPlayerAddItem(cid, 2160, count[cid])
  426.                         else
  427.                                 npcHandler:say("Sorry, you do not have enough platinum coins.", cid)
  428.                         end
  429.                 else
  430.                         npcHandler:say("Well, can I help you with something else?", cid)
  431.                 end
  432.                 talkState[cid] = 0
  433.         elseif msgcontains(msg, 'change crystal') then
  434.                 npcHandler:say("How many crystal coins would you like to change into platinum?", cid)
  435.                 talkState[cid] = 21
  436.         elseif talkState[cid] == 21 then
  437.                 if getCount(msg) == -1 or getCount(msg) == 0 then
  438.                         npcHandler:say("Hmm, can I help you with something else?", cid)
  439.                         talkState[cid] = 0
  440.                 else
  441.                         count[cid] = getCount(msg)
  442.                         npcHandler:say("So you would like me to change " .. count[cid] .. " of your crystal coins into " .. count[cid] * 100 .. " platinum coins for you?", cid)
  443.                         talkState[cid] = 22
  444.                 end
  445.         elseif talkState[cid] == 22 then
  446.                 if msgcontains(msg, 'yes') then
  447.                         if doPlayerRemoveItem(cid, 2160, count[cid])  then
  448.                                 npcHandler:say("Here you are.", cid)
  449.                                 doPlayerAddItem(cid, 2152, count[cid] * 100)
  450.                         else
  451.                                 npcHandler:say("Sorry, you do not have enough crystal coins.", cid)
  452.                         end
  453.                 else
  454.                         npcHandler:say("Well, can I help you with something else?", cid)
  455.                 end
  456.                 talkState[cid] = 0
  457.         elseif msgcontains(msg, 'change') then
  458.                 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)
  459.                 talkState[cid] = 0
  460.         end
  461.  
  462.         return true
  463. end
  464.  
  465. npcHandler:setCallback(CALLBACK_GREET, greetCallback)
  466. npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
  467. npcHandler:addModule(FocusModule:new())
Add Comment
Please, Sign In to add comment