MrCyberdragon

Ipeni ATM Turtle for 1.16

Jun 17th, 2021 (edited)
897
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  
  3. CC BY-SA License
  4.  
  5. Copyright (c) 2020 ipeni
  6.  
  7. reusers may distribute, remix, adapt, and build upon the material in any medium or format, so long as attribution is given to the creator. This license allows for commercial use. If you remix, adapt, or build upon the material, you must license the modified material under identical terms.
  8. --]]
  9.  
  10.  
  11. modem = nil
  12. modemBlock = nil
  13. monitor = nil
  14. dr = nil
  15. user = nil
  16. userBalance = 0
  17.  
  18. function init()
  19.     if not fs.exists("prices.txt") then
  20.         file = fs.open("prices.txt", "w")
  21.         file.writeLine("{}")
  22.         file.close()
  23.     end
  24.  
  25.     while true do
  26.         if (dr ~= nil) then
  27.         drawer = dr.list()
  28.         if (drawer[2]~=nil) then
  29.         name = drawer[2].name
  30.         amount = drawer[2].count
  31.         if name ~= nil and user ~= nil then
  32.             pricesFile = fs.open("prices.txt", "r")
  33.             prices = textutils.unserialise(pricesFile.readAll())
  34.             pricesFile.close()
  35.             if prices[name] == nil then
  36.                 value = 1
  37.                 if name:match("block", 1, true) then
  38.                     value = 9
  39.                 end
  40.                 prices[name] = value
  41.                 balance = prices[name] * amount
  42.                 newPrices = textutils.serialise(prices)
  43.                 pricesFile = fs.open("prices.txt", "w")
  44.                 pricesFile.write(newPrices)
  45.                 pricesFile.close()
  46.                 print("Credit: "..tonumber(amount) * tonumber(value))
  47.                 userBalance = userBalance + balance
  48.                 modem.transmit(404,6969,{request = "balance_update", user = user, balance = balance})
  49.                 updateMonitor()
  50.             else
  51.                 value = prices[name]
  52.                 balance = value * amount
  53.                 turtle.select(1)
  54.                 print("Credit: "..tonumber(amount) * tonumber(value))
  55.                 userBalance = userBalance + balance
  56.                 modem.transmit(404,6969, {request = "balance_update", user = user, balance = balance})
  57.                 updateMonitor()
  58.             end
  59.             for i = 1,(tonumber(amount)/64)+1 do
  60.                 turtle.suck(64)
  61.                 turtle.dropDown()
  62.                 turtle.select(i)
  63.             end
  64.         end
  65.         else
  66.             amount=0;
  67.         end
  68.         end
  69.         sleep(2)
  70.     end
  71. end
  72.  
  73. function listenForUserChange()
  74.     while true do
  75.         local e, s, sC, rC, m, dist = os.pullEvent("modem_message")
  76.         if m["request"] == "disk_eject" then
  77.             user = nil
  78.             userBalance = 0
  79.    monitor.setTextScale(0.5)
  80.             monitor.clear()
  81.             monitor.setCursorPos(1,1)
  82.             monitor.write("please insert user card")
  83.             print("User cleared")
  84.         elseif(m["request"] == "user_change") then
  85.             print("User "..m["user"].." logged in")
  86.              waitingForResponse = true
  87.              user = m["user"]
  88.     while waitingForResponse do
  89.                 modem.transmit(404, 6969, { request = "balance_request", user = m["user"]})
  90.                 local e, s, sC, rc, m, dist = os.pullEvent("modem_message")
  91.                 if m["request"] == "balance_transmission" then
  92.                     userBalance = m["balance"]
  93.                     updateMonitor()
  94.                     waitingForResponse = false
  95.                 end
  96.             end
  97.         end
  98.     end
  99. end
  100.  
  101. function updateMonitor()
  102.     monitor.setTextScale(0.5)
  103.     monitor.clear()
  104.     monitor.setCursorPos(1,1)
  105.     monitor.write("Welcome")
  106.     monitor.setCursorPos(1,2)
  107.     monitor.write("Your balance is")
  108.     monitor.setCursorPos(1,3)
  109.     monitor.write(userBalance)
  110. end
  111.  
  112. function initializePeripherals()
  113.     print("Initializing...")
  114.     for k,v in ipairs(peripheral.getNames()) do
  115.         type = peripheral.getType(v)
  116.         if type == "modem" then
  117.             tempModem = peripheral.wrap(v)
  118.             if tempModem.isWireless() then
  119.                 modem = peripheral.wrap(v)
  120.                 modem.open(6969)
  121.             else
  122.                 modemBlock = peripheral.wrap(v)
  123.                 modemBlock.open(0)
  124.                 modemNames = modemBlock.getNamesRemote()
  125.                 for x,y in ipairs(modemNames) do
  126.                     if y:match("monitor",1,true) then
  127.                         monitor = peripheral.wrap(y)
  128.                         break
  129.                     end
  130.                 end
  131.             end
  132.         elseif type == "storagedrawers:standard_drawers_1" then
  133.             dr = peripheral.wrap(v)
  134.         end
  135.     end
  136.     if modem == nil or monitor == nil or modemBlock == nil then
  137.         print("please make sure a modem, modemBlock, and monitor are connecting to this turtle, monitor must be connected to the modem block")
  138.         sleep(3)
  139.         initializePeripherals()
  140.     elseif dr == nil then
  141.         print("please place a drawer adjacent to this turtle")
  142.         sleep(3)
  143.         initializePeripherals()
  144.     elseif modem ~= nil and dr ~= nil and modemBlock ~= nil and monitor ~= nil then
  145.         parallel.waitForAny(init, listenForUserChange)
  146.     end
  147. end
  148.  
  149. initializePeripherals()
RAW Paste Data