Advertisement
Zantag

pimPayment

Sep 13th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. pIM = peripheral.wrap("pim_7")
  2. pIMstorage = peripheral.wrap("ender_chest_5")
  3. pIMstorageDir = "south"
  4. pimDir = "north"
  5. function init_pIMstorage()
  6. while pIMstorage == nil do
  7. pIMstorage = peripheral.wrap("ender_chest_5")
  8. sleep(0.1)
  9. end
  10. end
  11. function init_pIM()
  12. while pIM == nil do
  13. pIM = peripheral.wrap("pim_7")
  14. end
  15. end
  16. function giveItemToPIM(name, qty)
  17. if qty == nil then
  18. qty = 1
  19. end
  20. init_pIMstorage()
  21. size = pIMstorage.getSizeInventory()
  22. for i = 0, size - 1 do
  23. local tableInfo = pIMstorage.getStackInSlot(i)
  24. if tableInfo ~= nil then
  25. if tableInfo["name"] == name then
  26. pIM.pull(pimDir, i, qty)
  27. end
  28. end
  29. end
  30. end
  31. function deductFunds(fee)
  32. local ret = {0,0,0,0,0}
  33. init_pIM()
  34. size = pIM.getSizeInventory()
  35.  
  36. for i = 0, size-1 do
  37. local tableInfo = pIM.getStackInSlot(i)
  38. if tableInfo ~= nil then
  39. if tableInfo["id"] == 22256 then --We have a credit
  40. --print("----" .. tostring(tableInfo["name"]))
  41. if tableInfo["dmg"] > 0 then
  42. ret[tableInfo["dmg"]+1] = ret[tableInfo["dmg"]+1] + tableInfo["qty"]
  43. else
  44. ret[1] = ret[1] + tableInfo["qty"]
  45. end
  46.  
  47. elseif tableInfo["id"] == 30186 then
  48. -- print("----" .. tostring(tableInfo["name"]))
  49. ret[2] = ret[2] + tableInfo["qty"]
  50. end
  51. end
  52. end
  53. tFee = fee
  54. coinV = {0.125, 1, 8, 64, 512}
  55. temp = {0,0,0,0,0}
  56. for i = 1, 5 do
  57. while tFee >= coinV[6-i] and ret[6-i] > 0 do
  58. if tFee >= coinV[6-i] then
  59. ret[6-i] = ret[6-i] - 1
  60. tFee = tFee - coinV[6 - i]
  61. temp[6-i] = temp[6-i] + 1
  62. end
  63. end
  64. end
  65. if tFee > 0 then
  66. print("Not enough funds!")
  67. return false
  68. else
  69. for i = 0, size -1 do
  70. local tableInfo = pIM.getStackInSlot(i)
  71. if tableInfo ~= nil then
  72. dmg = tonumber(tableInfo["dmg"])
  73. qty = tonumber(tableInfo["qty"])
  74. if tableInfo["id"] == 22256 then --We have a credit
  75. if dmg > 0 then
  76. m = math.min(qty, temp[dmg+1])
  77. else
  78. m = math.min(qty, temp[1])
  79. end
  80. if pIM.push(pimDir, i, m) then
  81. if dmg > 0 then
  82. temp[dmg+1] = temp[dmg+1] - m
  83. else
  84. temp[1] = temp[1] - m
  85. end
  86. end
  87. elseif tableInfo["id"] == 30186 then
  88. m = math.min(qty, temp[2])
  89. if pIM.push(pimDir, i, m) then
  90. temp[2] = temp[2] - m
  91. end
  92. end
  93. end
  94. end
  95. for i = 1, 5 do
  96. if temp[i] > 0 then
  97. print("Failing b/c t: " .. temp[i])
  98. return false
  99. end
  100. end
  101. return true
  102. end
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement