peonso

Untitled

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