Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local strings = require "cc.strings"
- local expect = require "cc.expect".expect
- local pocketNames = {
- "computercraft:pocket_computer_advanced",
- "computercraft:pocket_computer_normal"
- }
- local function readFile(file)
- local h = io.open(file, 'r')
- if not h then error("Failed to read file " .. tostring(file) .. ".", 2) end
- local data = h:read("*a")
- h:close()
- return data
- end
- local function readDatabase()
- return textutils.unserialise(readFile("disk/PocketDatabase.dat")) or error("Failed to read database.")
- end
- local det = peripheral.wrap(readFile("detector"))
- local mon = peripheral.wrap(readFile("monitor"))
- local hop = peripheral.wrap(readFile("hopper"))
- local dis = peripheral.wrap(readFile("dispenser"))
- local dri = peripheral.wrap(readFile("drive"))
- local repData = readFile("REPLICATE")
- local sizes = {}
- for i = 0.5, 4, 0.5 do
- mon.setTextScale(i)
- sizes[i] = {mon.getSize()}
- end
- local function SetBestSize(message)
- local last = false
- for i = 4, 0.5, -0.5 do
- local x, y = sizes[i][1], sizes[i][2]
- local wrapped = strings.wrap(message, x)
- if #wrapped == y then
- return mon.setTextScale(i)
- elseif #wrapped <= y then
- if last then
- return mon.setTextScale(i + 0.5)
- else
- return mon.setTextScale(i)
- end
- end
- last = #wrapped == y
- end
- end
- local function Write(message)
- mon.clear()
- SetBestSize(message)
- local w = mon.getSize()
- local wrapped = strings.wrap(message, w)
- for i = 1, #wrapped do
- mon.setCursorPos(1, i)
- mon.write(wrapped[i])
- end
- end
- local function CheckLeave(player)
- while true do
- os.sleep(1)
- if not det.isPlayerInRange(3, player) then
- return
- end
- end
- end
- local written = false
- local function TellPlayer(player)
- written = false
- Write(string.format("Hello, %s!", player))
- os.sleep(2)
- while true do
- if written then
- break
- else
- Write("Drop a pocket computer into the hopper to continue!")
- os.sleep(5)
- if written then break end
- Write("I will add a file to it so you can play!")
- os.sleep(4)
- if written then break end
- Write("It will be saved as 'startup.lua'. Or 'GAME.lua' if 'startup.lua' is taken.")
- os.sleep(6)
- if written then break end
- end
- end
- os.sleep(3)
- end
- local function main()
- while true do
- mon.clear()
- local players = det.getPlayersInRange(3)
- if next(players) then
- parallel.waitForAny(
- function()
- TellPlayer(players[1])
- end,
- function()
- CheckLeave(players[1])
- end
- )
- Write("Goodbye.")
- end
- os.sleep(1)
- end
- end
- local function Reject(idx)
- if idx then
- hop.pushItems(peripheral.getName(dis), idx, 64, 1)
- end
- while next(dis.list()) do
- rs.setOutput("back", true)
- os.sleep(0.1)
- rs.setOutput("back", false)
- os.sleep(0.1)
- end
- end
- local function Accept(idx)
- expect(1, idx, "number")
- hop.pushItems(peripheral.getName(dri), idx, 1)
- local mp = dri.getMountPath()
- local writeTo = false
- if fs.exists(fs.combine(mp, "startup.lua")) then
- if not fs.exists(fs.combine(mp, "GAME.lua")) then
- -- write
- writeTo = fs.combine(mp, "GAME.lua")
- end
- else
- -- write
- writeTo = fs.combine(mp, "startup.lua")
- end
- if writeTo then
- local h = io.open(writeTo, 'w')
- if not h then
- dis.pullItems(peripheral.getName(dri), 1, 1)
- Reject()
- Write("Failed to open file for writing.")
- written = true
- return
- end
- h:write(repData):close()
- Write(string.format("Wrote to %s.", fs.getName(writeTo)))
- dis.pullItems(peripheral.getName(dri), 1, 1)
- Reject()
- else
- dis.pullItems(peripheral.getName(dri), 1, 1)
- Reject()
- Write("startup.lua and GAME.lua already exist!")
- end
- written = true
- end
- local function GrabPocket()
- local list = hop.list()
- for i = 1, hop.size() do
- if list[i] then
- for j = 1, #pocketNames do
- if list[i].name == pocketNames[j] then
- Accept(i)
- break
- else
- Reject(i)
- break
- end
- end
- end
- end
- end
- local function HandlePocket()
- while true do
- os.sleep(2)
- GrabPocket()
- end
- end
- parallel.waitForAny(main, HandlePocket)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement