Advertisement
Marum

Mermegold V4 ATM assistant

Mar 25th, 2025 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. -- ATM Assistant
  2.  
  3. local linkedInterfaceID
  4. local clientInterface = peripheral.wrap("front")
  5. local storage = peripheral.wrap("bottom")
  6. local workChest = peripheral.wrap("top")
  7. local serverData
  8.  
  9. print("Looking for modem...")
  10. peripheral.find("modem", rednet.open)
  11. print("Found modem!")
  12.  
  13. local f = fs.open("interface.txt", "r")
  14. if (f ~= nil) then
  15. linkedInterfaceID = f.readLine()
  16. print("Connected to interface #"..linkedInterfaceID)
  17. f.close()
  18. else
  19. print("Finding interface...")
  20. local sender, message = rednet.receive("mermegold_customer")
  21. if (message == "hiring") then
  22. rednet.send(sender, "offering", "mermegold_customer")
  23. linkedInterfaceID = sender
  24. end
  25. print("interface found! #"..linkedInterfaceID)
  26. local f = fs.open("interface.txt", "w")
  27. f.write(tostring(linkedInterfaceID))
  28. f.close()
  29. end
  30.  
  31. local function processRequest(func, sender, data)
  32. serverData = data.serverData
  33. local success, response = func(data)
  34. local message = {
  35. success = success,
  36. response = response
  37. }
  38. rednet.send(sender, message, "mermegold_customer")
  39. end
  40.  
  41. local function calculateValue()
  42. local value = 0
  43. for slot, item in pairs(clientInterface.list()) do
  44. for k, v in ipairs(serverData.currency) do
  45. if (item.name == v.id) then
  46. value = value + v.value*item.count*serverData.valueMultiplier
  47. break
  48. end
  49. end
  50. end
  51. return true, value
  52. end
  53.  
  54. local function countValueInside()
  55. local value = 0
  56. for i=1, 16 do
  57. local item = turtle.getItemDetail(i)
  58. if (item ~= nil) then
  59. local isCurrency = false
  60. for k, v in ipairs(serverData.currency) do
  61. if (item.name == v.id) then
  62. value = value + v.value*item.count*serverData.valueMultiplier
  63. isCurrency = true
  64. break
  65. end
  66. end
  67. if (not isCurrency) then
  68. turtle.select(i)
  69. turtle.drop()
  70. end
  71. end
  72. end
  73. return value
  74. end
  75.  
  76. local function reduceStorage(data)
  77. -- For each currency item
  78. for k, v in ipairs(serverData.currency) do
  79. if(v.uprecipe ~= nil) then
  80. -- Count how much we have stored
  81. local stored = 0
  82. for slot, item in pairs(storage.list()) do
  83. if (item.name == v.id) then
  84. stored = stored + item.count
  85. end
  86. end
  87.  
  88. -- See how many compression recipes we can make
  89. local craftsremaining = math.floor(stored/v.uprecipe.need)
  90.  
  91. -- Drain other items
  92. if (craftsremaining > 0) then
  93. for slot, item in pairs(storage.list()) do
  94. if (item.name ~= v.id) then
  95. storage.pushItems(peripheral.getName(workChest), slot)
  96. end
  97. end
  98. end
  99.  
  100. while (craftsremaining > 0) do
  101. local crafts = math.min(craftsremaining, 64)
  102. craftsremaining = craftsremaining - crafts
  103. -- Place in grid
  104. for i=1, v.uprecipe.need do
  105. local turtleSlot = i
  106. if (i > 3) then turtleSlot = turtleSlot + 1 end
  107. if (i > 6) then turtleSlot = turtleSlot + 1 end
  108. turtle.select(turtleSlot)
  109. local remaining = crafts - turtle.getItemCount(turtleSlot)
  110. while (remaining > 0) do
  111. turtle.suckDown(remaining)
  112. remaining = crafts - turtle.getItemCount(turtleSlot)
  113. end
  114. end
  115.  
  116. -- Craft
  117. turtle.craft()
  118.  
  119. -- Move all items from turtle inventory to the work chest
  120. for i=1, 16 do
  121. if (turtle.getItemCount(i) > 0) then
  122. turtle.select(i)
  123. turtle.dropUp()
  124. end
  125. end
  126. end
  127.  
  128. -- Return all items from workchest to storage
  129. for slot, item in pairs(workChest.list()) do
  130. workChest.pushItems(peripheral.getName(storage), slot)
  131. end
  132. end
  133. end
  134.  
  135. return true, "done"
  136. end
  137.  
  138. local function deposit()
  139. while (turtle.suck()) do end
  140. local message = countValueInside()
  141. for i=1, 16 do
  142. if (turtle.getItemCount(i) > 0) then
  143. turtle.select(i)
  144. turtle.dropDown()
  145. end
  146. end
  147. return true, message
  148. end
  149.  
  150. local function craftFromHigherCurrency(currency, amount)
  151. -- Get higherCurrency
  152. local higherCurrency
  153. local downgradeYield
  154. if (not currency.uprecipe) then return false end
  155. for k, v in ipairs(serverData.currency) do
  156. if (v.id == currency.uprecipe.result) then
  157. higherCurrency = v
  158. downgradeYield = higherCurrency.downrecipe.yield
  159. break
  160. end
  161. end
  162. if (higherCurrency == nil) then return false end
  163.  
  164. -- Is there any of the next currency stored?
  165. local higherCurrencyPresent = 0
  166. local higherCurrencyNeeded = amount/downgradeYield
  167. for slot, item in pairs(storage.list()) do
  168. if (item.name == higherCurrency.id) then
  169. higherCurrencyPresent = higherCurrencyPresent+item.count
  170. if (higherCurrencyPresent >= higherCurrencyNeeded) then
  171. break
  172. end
  173. end
  174. end
  175. -- If not, make some
  176. if (higherCurrencyPresent < higherCurrencyNeeded) then
  177. craftFromHigherCurrency(higherCurrency, math.ceil(higherCurrencyNeeded-higherCurrencyPresent))
  178. end
  179.  
  180. -- Craft
  181. local crafts = math.ceil(amount/downgradeYield)
  182. for slot, item in pairs(storage.list()) do
  183. if (item.name == higherCurrency.id) then
  184. turtle.select(1)
  185. turtle.suckDown(crafts)
  186. turtle.craft()
  187. while(turtle.dropDown()) do end
  188. break
  189. else
  190. storage.pushItems(peripheral.getName(workChest), slot)
  191. end
  192. end
  193.  
  194. -- Return all items from workchest to storage
  195. for slot, item in pairs(workChest.list()) do
  196. workChest.pushItems(peripheral.getName(storage), slot)
  197. end
  198. end
  199.  
  200. local function giveItems(currency, amount)
  201. print(currency.id..": "..amount)
  202.  
  203. -- Count stock
  204. local stock = 0
  205. for slot, item in pairs(storage.list()) do
  206. if (item.name == currency.id) then
  207. stock = stock + item.count
  208. end
  209. end
  210. if (stock < amount) then
  211. craftFromHigherCurrency(currency, amount-stock)
  212. end
  213.  
  214. -- Give items
  215. local remaining = amount
  216. for slot, item in pairs(storage.list()) do
  217. if (item.name == currency.id) then
  218. print("Giving client "..math.min(remaining, item.count).." "..item.name)
  219. storage.pushItems(peripheral.getName(clientInterface), slot, math.min(remaining, item.count))
  220. remaining = remaining - item.count
  221. if (remaining < 1) then break end
  222. end
  223. end
  224. end
  225.  
  226. local function withdraw(data)
  227.  
  228. if (data.amount == "" or data.amount == nil) then
  229. return false, "invalid_amount"
  230. end
  231.  
  232. print("Counting vault...")
  233. local storedValue = 0
  234. for i = #serverData.currency, 1, -1 do
  235. local currency = serverData.currency[i]
  236. if (not currency.ingredient) then
  237. for slot, item in pairs(storage.list()) do
  238. if (item.name == currency.id) then
  239. storedValue = storedValue + currency.value*item.count
  240. end
  241. end
  242. end
  243. end
  244. print("Stored amount: "..storedValue)
  245. if (storedValue < data.amount) then
  246. return false, "not_enough_stored"
  247. end
  248.  
  249. local remainingValue = data.amount
  250. print("Withdrawing: "..remainingValue)
  251.  
  252. for i = #serverData.currency, 1, -1 do
  253. local currency = serverData.currency[i]
  254. if (currency.ingredient == nil or currency.ingredient == false) then
  255. -- Give in terms of $
  256. --local give = math.floor(remainingValue/(currency.value*serverData.valueMultiplier))
  257. --if (give > 0) then
  258. -- remainingValue = remainingValue - give*(currency.value*serverData.valueMultiplier)
  259. -- giveItems(currency, give)
  260. --end
  261.  
  262. -- Give in terms of item count
  263. local give = math.floor(remainingValue/(currency.value))
  264. if (give > 0) then
  265. remainingValue = remainingValue - give*(currency.value)
  266. giveItems(currency, give)
  267. end
  268. end
  269. end
  270.  
  271. return true, "Success"
  272. end
  273.  
  274. while (true) do
  275. local sender, message = rednet.receive("mermegold_customer")
  276. if (tonumber(sender) == tonumber(linkedInterfaceID)) then
  277. if (message.action == "calculateValue") then
  278. processRequest(calculateValue, sender, message)
  279. elseif (message.action == "deposit") then
  280. processRequest(deposit, sender, message)
  281. elseif (message.action == "withdraw") then
  282. processRequest(withdraw, sender, message)
  283. elseif (message.action == "reduce") then
  284. processRequest(reduceStorage, sender, message)
  285. end
  286. end
  287. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement