Advertisement
fatboychummy

PocketWriter

Jul 18th, 2021
1,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | None | 0 0
  1. local strings = require "cc.strings"
  2. local expect = require "cc.expect".expect
  3.  
  4. local pocketNames = {
  5.     "computercraft:pocket_computer_advanced",
  6.     "computercraft:pocket_computer_normal"
  7. }
  8.  
  9. local function readFile(file)
  10.     local h = io.open(file, 'r')
  11.     if not h then error("Failed to read file " .. tostring(file) .. ".", 2) end
  12.     local data = h:read("*a")
  13.     h:close()
  14.    
  15.     return data
  16. end
  17.  
  18. local function readDatabase()
  19.     return textutils.unserialise(readFile("disk/PocketDatabase.dat")) or error("Failed to read database.")
  20. end
  21.  
  22. local det = peripheral.wrap(readFile("detector"))
  23. local mon = peripheral.wrap(readFile("monitor"))
  24. local hop = peripheral.wrap(readFile("hopper"))
  25. local dis = peripheral.wrap(readFile("dispenser"))
  26. local dri = peripheral.wrap(readFile("drive"))
  27. local repData = readFile("REPLICATE")
  28.  
  29. local sizes = {}
  30. for i = 0.5, 4, 0.5 do
  31.     mon.setTextScale(i)
  32.     sizes[i] = {mon.getSize()}
  33. end
  34.  
  35. local function SetBestSize(message)
  36.     local last = false
  37.     for i = 4, 0.5, -0.5 do
  38.         local x, y = sizes[i][1], sizes[i][2]
  39.         local wrapped = strings.wrap(message, x)
  40.         if #wrapped == y then
  41.             return mon.setTextScale(i)
  42.         elseif #wrapped <= y then
  43.             if last then
  44.                 return mon.setTextScale(i + 0.5)
  45.             else
  46.                 return mon.setTextScale(i)
  47.             end
  48.         end
  49.         last = #wrapped == y
  50.     end
  51. end
  52.  
  53. local function Write(message)
  54.     mon.clear()
  55.     SetBestSize(message)
  56.     local w = mon.getSize()
  57.     local wrapped = strings.wrap(message, w)
  58.    
  59.     for i = 1, #wrapped do
  60.         mon.setCursorPos(1, i)
  61.         mon.write(wrapped[i])
  62.     end
  63. end
  64.  
  65. local function CheckLeave(player)
  66.     while true do
  67.         os.sleep(1)
  68.        
  69.         if not det.isPlayerInRange(3, player) then
  70.             return
  71.         end
  72.     end
  73. end
  74.  
  75. local written = false
  76. local function TellPlayer(player)
  77.     written = false
  78.     Write(string.format("Hello, %s!", player))
  79.     os.sleep(2)
  80.    
  81.     while true do
  82.         if written then
  83.             break
  84.         else
  85.             Write("Drop a pocket computer into the hopper to continue!")
  86.             os.sleep(5)
  87.             if written then break end
  88.             Write("I will add a file to it so you can play!")
  89.             os.sleep(4)
  90.             if written then break end
  91.             Write("It will be saved as 'startup.lua'. Or 'GAME.lua' if 'startup.lua' is taken.")
  92.             os.sleep(6)
  93.             if written then break end
  94.         end
  95.     end
  96.    
  97.     os.sleep(3)
  98. end
  99. local function main()
  100.     while true do
  101.         mon.clear()
  102.         local players = det.getPlayersInRange(3)
  103.         if next(players) then
  104.             parallel.waitForAny(
  105.                 function()
  106.                     TellPlayer(players[1])
  107.                 end,
  108.                 function()
  109.                     CheckLeave(players[1])
  110.                 end
  111.             )
  112.             Write("Goodbye.")
  113.         end
  114.         os.sleep(1)
  115.     end
  116. end
  117.  
  118. local function Reject(idx)
  119.     if idx then
  120.         hop.pushItems(peripheral.getName(dis), idx, 64, 1)
  121.     end
  122.     while next(dis.list()) do
  123.         rs.setOutput("back", true)
  124.         os.sleep(0.1)
  125.         rs.setOutput("back", false)
  126.         os.sleep(0.1)
  127.     end
  128. end
  129.  
  130. local function Accept(idx)
  131.     expect(1, idx, "number")
  132.    
  133.     hop.pushItems(peripheral.getName(dri), idx, 1)
  134.    
  135.    
  136.     local mp = dri.getMountPath()
  137.     local writeTo = false
  138.     if fs.exists(fs.combine(mp, "startup.lua")) then
  139.         if not fs.exists(fs.combine(mp, "GAME.lua")) then
  140.             -- write
  141.             writeTo = fs.combine(mp, "GAME.lua")
  142.         end
  143.     else
  144.         -- write
  145.         writeTo = fs.combine(mp, "startup.lua")
  146.     end
  147.    
  148.     if writeTo then
  149.         local h = io.open(writeTo, 'w')
  150.         if not h then
  151.             dis.pullItems(peripheral.getName(dri), 1, 1)
  152.             Reject()
  153.             Write("Failed to open file for writing.")
  154.             written = true
  155.             return
  156.         end
  157.        
  158.         h:write(repData):close()
  159.         Write(string.format("Wrote to %s.", fs.getName(writeTo)))
  160.         dis.pullItems(peripheral.getName(dri), 1, 1)
  161.         Reject()
  162.     else
  163.         dis.pullItems(peripheral.getName(dri), 1, 1)
  164.         Reject()
  165.         Write("startup.lua and GAME.lua already exist!")
  166.     end
  167.    
  168.     written = true
  169. end
  170.  
  171. local function GrabPocket()
  172.     local list = hop.list()
  173.     for i = 1, hop.size() do
  174.         if list[i] then
  175.             for j = 1, #pocketNames do
  176.                 if list[i].name == pocketNames[j] then
  177.                     Accept(i)
  178.                     break
  179.                 else
  180.                     Reject(i)
  181.                     break
  182.                 end
  183.             end
  184.         end
  185.     end
  186. end
  187.  
  188. local function HandlePocket()
  189.     while true do
  190.         os.sleep(2)
  191.         GrabPocket()
  192.     end
  193. end
  194.  
  195. parallel.waitForAny(main, HandlePocket)
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement