Skip to content

Instantly share code, notes, and snippets.

Created December 14, 2013 03:40
Show Gist options
  • Save anonymous/7955418 to your computer and use it in GitHub Desktop.
Save anonymous/7955418 to your computer and use it in GitHub Desktop.
function main()
--[[
Program: Config API
Version: 1.0
Author: ben657
Support:
--]]
loaded = false
dir = ""
file = ""
path = ""
internal = {}
function create()
local file = fs.open(path,"a")
file.close()
end
function load(directory,fileName)
dir = directory
file = fileName
if dir == nil then
path = file
else
path = dir.."/"..file
end
if path:find("/") then
fs.makeDir(dir)
end
create(path)
local file = fs.open(path, "r")
repeat
line = file.readLine()
if(line ~= nil) then
local asWords = line:gsub(":","")
local parts = {}
for word in asWords:gmatch("%w+") do table.insert(parts,word) end
internal[parts[1]] = parts[2]
end
until line == nil
loaded = true
file.close()
end
function writeVal(key,value)
if(loaded == false) then
return false
else
local toWrite = value
if(value == true) then toWrite = "true"
elseif(value == false) then toWrite = "false" end
internal[key] = toWrite
return true
end
end
function readVal(key)
if(loaded == false) then
return nil
end
toReturn = internal[key]
if(toReturn == "true") then return true
elseif(toReturn == "false") then return false
else return internal[key] end
end
function save()
if(loaded == true) then
local file = fs.open(path,"w")
for i,v in pairs(internal) do
file.writeLine(i.." = "..v)
end
file.close()
internal = {}
loaded = false
return true
else
return false
end
end
function isEmpty()
if(loaded == false) then
return nil
end
if(fs.getSize(path) == 0)then
return true
else return false end
end
local function drawScreen()
term.clear()
term.setCursorPos(1,1)
print("Christmas Present Machine")
--print("Press 'y' to edit the naughty/nice list")
end
term.clear()
term.setCursorPos(1,1)
for _,side in pairs(rs.getSides()) do
if peripheral.getType(side) == "playerDetector" then
hPlayer = peripheral.wrap(side)
end
end
if not hPlayer then
error("No player detector found!")
end
if not fs.exists("config") then
while true do
term.clear()
term.setCursorPos(1,1)
write("VALID INPUTS: ")
for index,side in pairs(rs.getSides()) do
if index < 6 then
write(side..", ")
else
write(side)
end
end
term.setCursorPos(1,3)
write("Please enter the side on which to output redstone: ")
term.setCursorPos(1,4)
iSide = read():lower()
for _,side in pairs(rs.getSides()) do
if iSide == side then
load(nil,"config")
writeVal("redstone",iSide)
save()
end
end
if fs.exists("config") then
term.clear()
term.setCursorPos(1,1)
break
end
end
else
load(nil,"config")
rsSide = readVal("redstone")
save()
end
if fs.exists("internal") then
hFile = fs.open("internal","r")
presentList = textutils.unserialize(hFile.readAll())
hFile.close()
if not presentList then
presentList = {}
end
end
drawScreen()
while true do
local ev = {os.pullEventRaw("player")}
if ev[1] == "player" then
bGive = true
for _,name in pairs(presentList) do
if name == ev[2] then
bGive = false
end
end
if bGive then
table.insert(presentList,ev[2])
hFile = fs.open("internal","w")
hFile.write(textutils.serialize(presentList))
hFile.close()
hFile = fs.open("Christmas List","w")
for _,name in pairs(presentList) do
hFile.write(name.."\n")
end
hFile.close()
rs.setOutput(rsSide,bGive)
sleep(0.2)
rs.setOutput(rsSide,false)
end
end
end
end
local ok,err = pcall(main())
if not ok then
hFile = fs.open("errorLog","w")
hFile.write(tostring(err))
hFile.close()
os.reboot()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment