Merry Christmas Presents!

Discussion in 'ComputerCraft Programming' started by gknova61, 14 December 2013.

  1. gknova61

    gknova61 Farbes Lover

    Joined:
    17 March 2012
    Messages:
    1.238
    Likes Received:
    350
    Who needs a ChristmasCraft tree when we have ComputerCraft! This program is made for those out there feeling a little generous this holiday :D.

    Using a computer, a player detector, some redwire, and a dispenser, you can easily setup your own Christmas gift-giving stand where players just need to right click
    on a player detector to get their present! Once they interact with the detector, their name is logged into a database (including a human-readable 'Christmas List' file) so
    they cannot get multiple gifts.

    Tutorial:
    Step 1 - Setup your computer (usually want this hidden), player detector, and your dispenser. You can do this anyway you want as long as the player detector is touching
    the computer, and the redwire is like so:
    [​IMG]
    [​IMG]
    Note: You can setup the redwire, and player detector on any side of the computer as shown here:
    [​IMG]

    Step 2 - Load the program onto the computer and name it 'startup' then restart. Enter the side that your redwire is on:
    [​IMG]
    Step 3 - Have your people interact with the player detector to get their presents! I would suggest a bag of holding or similar.
    [​IMG]
    [​IMG]

    Question: Would there be any interest for a naughty/nice list where you can specify who doesn't get a present or where you specify
    the only people who can?

    Program:
    Code:
    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
    
    Happy Holidays, everyone!

    Released on CC forums: http://www.computercraft.info/forums2/index.php?/topic/16317-merry-christmas-presents/

    Credits: Config API by ben657
     
  2. fxstriker

    fxstriker Good Bye skcraft.

    Joined:
    14 January 2012
    Messages:
    259
    Likes Received:
    35

    Okay.