Advertisement
9551

backup

Sep 4th, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.66 KB | None | 0 0
  1. --sender
  2. local main = function()
  3.     local modems = {peripheral.find("modem")}
  4.     local modem
  5.     for k,v in pairs(modems) do
  6.         if v.isWireless() then
  7.             modem = v
  8.         end
  9.     end
  10.     if not modem then
  11.         error("no modem!",0)
  12.     end
  13.    
  14.     if not fs.exists("reactor.data") then
  15.         fs.open("reactor.data","w").close()
  16.     end
  17.     if not fs.exists("buttonH.lua") then
  18.         shell.run("pastebin get LTDZZZEJ buttonH.lua")
  19.     end
  20.     local b = require("buttonH").terminal
  21.     local file = fs.open("reactor.data","r")
  22.     local lenRead = function(maxlen,bcol)
  23.         local str = ""
  24.         local xs, ys = term.getCursorPos()
  25.         while true do
  26.             local e = {os.pullEvent()}
  27.             if e[1] == "char" and #str < maxlen then
  28.                 str = str..e[2]
  29.             elseif e[1] == "key" and e[2] == keys.backspace and #str >= 1 then
  30.                 str = str:match("(.*).$")
  31.             elseif e[1] == "key" and e[2] == keys.enter then
  32.                 return str
  33.             end
  34.             term.setCursorPos(xs, ys)
  35.             local obc = term.getBackgroundColor()
  36.             term.setBackgroundColor(bcol)
  37.             term.write((" "):rep(maxlen))
  38.             term.setBackgroundColor(obc)
  39.             term.setCursorPos(xs, ys)
  40.             term.write(str)
  41.         end
  42.     end
  43.     local datas = {}
  44.     if file.readAll() == "" then
  45.         file.close()
  46.         local ID
  47.         local name
  48.         local identifier = math.random(1,100000000)
  49.         local rin,rout = peripheral.find("bigreactors:tileentityreactoraccessport")
  50.         if not rin then error("no reactor input!",0) end
  51.         if not rout then error("no reactor output!",0) end
  52.         local reactorM = peripheral.find("BigReactors-Reactor")
  53.         if not reactorM then error("no reactor!",0) end
  54.         local rin,rout = peripheral.getName(rin),peripheral.getName(rout)
  55.         local reactorM = peripheral.getName(reactorM)
  56.         local chest
  57.         local getData = function()
  58.             term.setBackgroundColor(colors.lightGray)
  59.             term.clear()
  60.             term.setCursorPos(24,5)
  61.             term.write("SETUP")
  62.             b.frame(13,11,28,5,"gray","black",false)
  63.             term.setBackgroundColor(colors.gray)
  64.             term.setTextColor(colors.black)
  65.             term.setCursorPos(13,9)
  66.             print(("\143"):rep(27))
  67.             term.setCursorPos(13,12)
  68.             print(("\143"):rep(27))
  69.             term.setTextColor(colors.white)
  70.             term.setBackgroundColor(colors.blue)
  71.             term.setCursorPos(13,7)
  72.             print("Pocket pc ID (number)")
  73.             term.setCursorPos(13,10)
  74.             print("Reactor Name (text)")
  75.             term.setCursorPos(13,13)
  76.             print("Chest Type (ex: mc:chest)")
  77.             term.setCursorPos(13,8)
  78.             term.setBackgroundColor(colors.gray)
  79.             ID = lenRead(27,colors.black)
  80.             term.setCursorPos(13,11)
  81.             name = lenRead(27,colors.black)
  82.             term.setCursorPos(13,14)
  83.             chest = peripheral.find(lenRead(27,colors.black))
  84.             if not chest then return "chestERR" end
  85.             chest = peripheral.getName(chest)
  86.         end
  87.         while getData() do
  88.             term.setTextColor(colors.white)
  89.             term.setBackgroundColor(colors.red)
  90.             term.clear()
  91.             b.frame(10,10,34,4,"black","gray")
  92.             term.setCursorPos(18,10)
  93.             term.setBackgroundColor(colors.lightGray)
  94.             term.write("Not a valid chest")
  95.             sleep(2)
  96.         end
  97.         while true do
  98.             local click = b.timetouch(0)
  99.             if b.boxButton(1,click,17,18,"red","black","discard",1,1) then
  100.                 while getData() do
  101.                     term.setTextColor(colors.white)
  102.                     term.setBackgroundColor(colors.red)
  103.                     term.clear()
  104.                     b.frame(10,10,34,4,"black","gray")
  105.                     term.setCursorPos(18,10)
  106.                     term.setBackgroundColor(colors.lightGray)
  107.                     term.write("Not a valid chest")
  108.                     sleep(2)
  109.                 end
  110.             end
  111.             if b.boxButton(1,click,27,18,"green","black","confirm",1,1) then
  112.                 datas = {
  113.                     metadata = {
  114.                         stamp = "POCKETn@P#C3Y5XnNTj79gyj__GD=Zg92&Mn,e#2wE,Nyuc2sS/D>V>Hg_)3C*Q_%b*N5E",
  115.                         sendID = ID,
  116.                         reply = os.getComputerID(),
  117.                         identifier = identifier,
  118.                         name = name,
  119.                         save = false
  120.                     },
  121.                     reactor = {
  122.                         state = false,
  123.                         rods = 0,
  124.                         input = rin,
  125.                         output = rout,
  126.                         chest = chest,
  127.                         self = reactorM,
  128.                         isSteam = false,
  129.                         energyData = {
  130.                             perT = 0,
  131.                             perSec = 0,
  132.                             perMin = 0,
  133.                             stored = 0,
  134.                             maxstored = 0
  135.                         },
  136.                         tempInfo = {
  137.                             coreTemp = 0,
  138.                             caseTemp = 0,
  139.                             tempDiff = 0
  140.                         },
  141.                         fuelInfo = {
  142.                             fuel = 0,
  143.                             waste = 0,
  144.                             fuelUsePerT = 0,
  145.                             reactivity = 0
  146.                         },
  147.                         peripherals = {
  148.                             list = {}
  149.                         }
  150.                     },
  151.                     chestEvents = {
  152.                         items = {},
  153.                         push = false,
  154.                         toInput = false,
  155.                         fromInput = false,
  156.                         toOutput = false,
  157.                         slot = 0
  158.                     }
  159.                 }
  160.                 local file = fs.open("reactor.data","w")
  161.                 file.write(textutils.serialize(datas))
  162.                 file.close()
  163.                 break
  164.             end
  165.         end
  166.     else
  167.         file.close()
  168.     end
  169.     local file = fs.open("reactor.data","r")
  170.     local default = textutils.unserialise(file.readAll())
  171.     local current = default
  172.     term.setTextColor(colors.white)
  173.     term.setBackgroundColor(colors.blue)
  174.     term.clear()
  175.     term.setCursorPos(19,5)
  176.     term.write("pocket running")
  177.     b.frame(8,10,38,4,"black","gray")
  178.     term.setBackgroundColor(colors.black)
  179.     term.setCursorPos(8,7)
  180.     term.write("STATUS")
  181.     term.setTextColor(colors.white)
  182.     term.setBackgroundColor(colors.gray)
  183.     rednet.open(peripheral.getName(modem))
  184.     local writes = function(state,tcol,bcol)
  185.         local obc = term.getBackgroundColor()
  186.         local otc = term.getTextColor()
  187.         term.setCursorPos(9,10)
  188.         term.setTextColor(tcol or otc)
  189.         term.setBackgroundColor(bcol or obc)
  190.         term.write(state)
  191.         term.setTextColor(otc)
  192.         term.setBackgroundColor(obc)
  193.     end
  194.     local gdt
  195.     local tempData
  196.     while true do
  197.         local click = b.timetouch(0.1)
  198.         term.setBackgroundColor(colors.blue)
  199.         b.switch(1,1,click,17,18,"red","green","black","PAUSE",1,1)
  200.         if b.boxButton(1,click,25,18,"red","black","restart config",1,1) then
  201.             fs.open("reactor.data","w").close()
  202.             term.setBackgroundColor(colors.black)
  203.             term.setTextColor(colors.white)
  204.             term.clear()
  205.             term.setCursorPos(1,1)
  206.             break
  207.         end
  208.         term.setCursorPos(11,10)
  209.         if not b.switch("db",1) then
  210.             gdt = function()
  211.                 while true do
  212.                     rednet.send(tonumber(default.metadata.sendID),current,"POCKETn@P#C3Y5XnNTj79gyj__GD=Zg92&Mn,e#2wE,Nyuc2sS/D>V>Hg_)3C*Q_%b*N5E")
  213.                     local _,recdata = rednet.receive("POCKETn@P#C3Y5XnNTj79gyj__GD=Zg92&Mn,e#2wE,Nyuc2sS/D>V>Hg_)3C*Q_%b*N5E",1)
  214.                     if not recdata then
  215.                         writes("TIMED OUT. trying again...         ",colors.white,colors.red)
  216.                         sleep(1)
  217.                     else
  218.                         writes("              WORKING              ",colors.white,colors.green)
  219.                         return recdata
  220.                     end
  221.                 end
  222.             end
  223.             current = gdt()
  224.             if current.metadata.save then
  225.                 writes("saving new received config")
  226.                 current.metadata.save = false
  227.                 local file = fs.open("reactor.data","w")
  228.                 file.write(textutils.serialize(current))
  229.                 file.close()
  230.                 sleep(1)
  231.             end
  232.             local reactor = peripheral.wrap(current.reactor.self)
  233.             local rin = peripheral.wrap(current.reactor.input)
  234.             local rout = peripheral.wrap(current.reactor.output)
  235.             local chest = peripheral.wrap(current.reactor.chest)
  236.             local updatedata = {}
  237.             parallel.waitForAll(
  238.                 function() updatedata.energyPerT = reactor.getEnergyProducedLastTick() end,
  239.                 function() updatedata.stored = reactor.getEnergyStored() end,
  240.                 function() updatedata.storedMax = reactor.getEnergyCapacity() end,
  241.                 function() updatedata.coreTemp = reactor.getFuelTemperature() end,
  242.                 function() updatedata.caseTemp = reactor.getCasingTemperature() end,
  243.                 function() updatedata.fuel = reactor.getFuelAmount() end,
  244.                 function() updatedata.waste = reactor.getWasteAmount() end,
  245.                 function() updatedata.fuelUsePerT = reactor.getWasteAmount() end,
  246.                 function() updatedata.reactivity = reactor.getFuelReactivity() end,
  247.                 function() updatedata.items = chest.list() end
  248.             )
  249.             updatedata.energyPerS = updatedata.energyPerT*20
  250.             updatedata.energyPerMin = updatedata.energyPerS*6
  251.             updatedata.tempDiff = updatedata.coreTemp - updatedata.caseTemp
  252.             current.reactor.energyData.perT = updatedata.energyPerT
  253.             current.reactor.energyData.perSec = updatedata.energyPerSec
  254.             current.reactor.energyData.perMin = updatedata.energyPerMin
  255.             current.reactor.energyData.stored = updatedata.stored
  256.             current.reactor.energyData.maxstored = updatedata.storedMax
  257.             current.reactor.tempInfo.coreTemp = updatedata.coreTemp
  258.             current.reactor.tempInfo.caseTemp = updatedata.caseTemp
  259.             current.reactor.tempInfo.tempDiff = updatedata.tempDiff
  260.             current.reactor.fuelInfo.fuel = updatedata.fuel
  261.             current.reactor.fuelInfo.waste = updatedata.waste
  262.             current.reactor.fuelInfo.fuelUsePerT = updatedata.fuelUsePerT
  263.             current.reactor.fuelInfo.reactivity = updatedata.reactivity
  264.             current.chestEvents.items = updatedata.items
  265.             reactor.setAllControlRodLevels(current.reactor.rods)
  266.             reactor.setActive(current.reactor.state or false)
  267.         else
  268.             term.setBackgroundColor(colors.blue)
  269.             b.frame(8,10,38,4,"black","gray")
  270.             term.setBackgroundColor(colors.black)
  271.             term.setCursorPos(8,7)
  272.             term.write("STATUS")
  273.             writes("Paused...",colors.black,colors.yellow)
  274.         end
  275.     end
  276. end
  277. local ok, err = pcall(main)
  278. term.setBackgroundColor(colors.black)
  279. term.setTextColor(colors.white)
  280. term.clear()
  281. term.setCursorPos(1,1)
  282. if not ok then
  283.     print("pocket crashed. reason:\n"..err)
  284. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement