Advertisement
CelticCoder

turtleATM

May 9th, 2024 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. os.loadAPI("turtleChest.lua")
  2. --known problems: Need a way to update the itemlist 24/7 but also time sensitive
  3. username = nil
  4. password = nil
  5. serverID = 4
  6. atmID = 1
  7.  
  8. function twoInputStrip(str)
  9.     first, second = str:match("(%S+)%s+(%S+)")
  10.     return first, second
  11. end
  12.  
  13. function threeInputStrip(str)
  14.     first, second, third = str:match("(%S+)%s+(%S+)%s+(%S+)")
  15.     return first, second, third
  16. end
  17.  
  18. function setItemlist(filename, itemlist)
  19.     local file = fs.open(filename, "w")
  20.     for _, line in pairs(itemlist) do
  21.         file.write(line .. "\n")
  22.     end
  23.     file.close()
  24. end
  25.  
  26. function send(id, message)
  27.     rednet.open("left")
  28.     rednet.send(id, message)
  29.     print("send: " .. message)
  30. end
  31.  
  32. function recieve(serverID)
  33.     rednet.open("left")
  34.     senderID, message, protocol = rednet.receive()
  35.     print("ServerID: " .. serverID)
  36.     print("senderID: " .. senderID)
  37.     print(message)
  38.     print(tonumber(serverID) == tonumber(senderID))
  39.     if tonumber(serverID) == tonumber(senderID) then
  40.         return message
  41.     else
  42.         return nil
  43.     end
  44. end
  45.  
  46. function anyRecieve()
  47.     rednet.open("left")
  48.     senderID, message, protocol = rednet.receive()
  49.     return message
  50. end
  51.  
  52. function commands(serverID, filename)
  53.     while true do
  54.         input = anyRecieve()
  55.         print(input)
  56.         command = twoInputStrip(input)
  57.         print("Recieved: " .. command)
  58.         if command ~= nil then
  59.             if command == "start" then
  60.                 command, username, password = threeInputStrip(input)
  61.                 print("START")
  62.                 start = true
  63.             elseif command == "stop" then
  64.                 print("STOP")
  65.                 start = false
  66.             elseif command == "itemlist" then
  67.                 itemlist = recieve(serverID)
  68.                 setItemlist(filename, itemlist)
  69.             end
  70.         end
  71.         os.sleep(0.1)
  72.     end
  73. end
  74.  
  75. function getItem(item, filename)
  76.     local file = fs.open(filename, "r")
  77.     local line = file.readLine()
  78.     while line do
  79.         if twoInputStrip(line) == item then
  80.             return line
  81.         end
  82.         line = file.readLine()
  83.     end
  84.     file.close()
  85.     return nil
  86. end
  87.  
  88. function deposit(itemlist)
  89.     depositamount = 0
  90.     success = true
  91.     while success do
  92.         itemname, amount = turtleChest.itemSuckUp()
  93.         if itemname ~= nil then
  94.                 item = getItem(itemname, itemlist)
  95.                 if item ~= nil then
  96.                     name, value = twoInputStrip(item)
  97.                     depositamount = depositamount + (value * amount)
  98.                     turtle.drop()
  99.                 else
  100.                     turtle.dropDown()
  101.                 end
  102.         else
  103.             success = false
  104.         end
  105.     end
  106.     return depositamount
  107. end
  108.  
  109. function atm(filename, serverID, atmID)
  110.     while true do
  111.         balancechange = 0
  112.         while start do
  113.             balancechange = balancechange + deposit(filename)
  114.             send(atmID, balancechange)
  115.         end
  116.         if balancechange ~= 0 then
  117.             balance = "balance " .. username .. " " .. password .. " " .. balancechange
  118.             send(serverID, balance)
  119.             if recieve(serverID) ~= "false" then
  120.                 print("Sent to ATM")
  121.                 send(atmID, balance)
  122.             else
  123.                 send(atmID, "deposit not recieved")
  124.             end
  125.             username = "temp"
  126.             password = "temp"
  127.         end
  128.         os.sleep(0.1)
  129.     end
  130. end
  131.  
  132. rednet.open("left")
  133. filename = "itemlist.txt"
  134. rednet.send(serverID, "items please")
  135. itemlist = recieve(serverID)
  136. setItemlist(filename, itemlist)
  137. while true do
  138.     parallel.waitForAny(
  139.         function() atm(filename, serverID, atmID) end,
  140.         function() commands(serverID, filename) end
  141.     )
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement