CCCPaste

Distributore gasolio PECTEN

costante = 1
local maxw, maxh = term.getSize()
m = peripheral.wrap("top")
m.write(" PECTEN")
m.setCursorPos(1,3)
m.write("Diesel:")
m.setCursorPos(1,4)
m.write(costante)
m.setCursorPos(1,5)
m.write("IC/s.")


function creditoInsufficiente()
    clearRed()
    print("Credito insufficiente.")
    print("")
    print("Premere un tasto per uscire")
    os.pullEvent("key")
    os.reboot()
end

function gasolioNonDisponibile()
    clearRed()
    print("Gasolio esaurito.")
    print("")
    print("Premere un tasto per uscire")
    os.pullEvent("key")
    os.reboot()
end

-- IMPLEMENTAZIONE DEL DRAWFILLEDBOX
local function drawPixelInternal(xPos, yPos)
    term.setCursorPos(xPos, yPos)
    term.write(" ")
end

local tColourLookup = {}
for n = 1, 16 do
    tColourLookup[string.byte("0123456789abcdef", n, n)] = 2 ^ (n - 1)
end

function drawFilledBox(startX, startY, endX, endY, nColour)
    if type(startX) ~= "number" or type(startX) ~= "number" or type(endX) ~=
        "number" or type(endY) ~= "number" or
        (nColour ~= nil and type(nColour) ~= "number") then
        error("Expected startX, startY, endX, endY, colour", 2)
    end

    startX = math.floor(startX)
    startY = math.floor(startY)
    endX = math.floor(endX)
    endY = math.floor(endY)

    if nColour then term.setBackgroundColor(nColour) end
    if startX == endX and startY == endY then
        drawPixelInternal(startX, startY)
        return
    end

    local minX = math.min(startX, endX)
    if minX == startX then
        minY = startY
        maxX = endX
        maxY = endY
    else
        minY = endY
        maxX = startX
        maxY = startY
    end

    for x = minX, maxX do for y = minY, maxY do drawPixelInternal(x, y) end end
end

function clear()
    sfondo(colors.yellow)
    term.clear()
    term.setCursorPos(1, 1)
end

function clearRed()
    sfondo(colors.red)
    term.clear()
    term.setCursorPos(1, 1)
end

function colore(sfumatura) term.setTextColour(sfumatura) end

function sfondo(sfumaturaSfondo) term.setBackgroundColour(sfumaturaSfondo) end

function fineColore() term.setTextColour(colours.white) end
 
function fineSfondo() term.setBackgroundColour(colours.black) end

function titolo(testo)
    drawFilledBox(1, 1, maxw, 1, colors.red)
    term.setCursorPos((maxw - #testo) / 2, 1)
    colore(colors.black)
    term.write(testo)
    sfondo(colors.red)
end

function errore(errore)
    sfondo(colors.red)
    colore(colors.white)
    term.clear()
    term.setCursorPos(1, 1)
    titolo("Errore irreversibile")
    term.setCursorPos(1, 3)
    sfondo(colors.red)
    colore(colors.white)
    print(errore)
    print("\n\n\nAttendere qualche secondo...")
    os.sleep(5)
    os.reboot()
end

clear()

titolo("DISTRIBUTORE DI VPower® PECTEN®")
-- startx, starty, endx, endy
drawFilledBox(3, 4, 15, 6, colors.red)
drawFilledBox(3, 8, 15, 10, colors.red)
drawFilledBox(3, 12, 15, 14, colors.red)
drawFilledBox(3, 16, 15, 18, colors.red)
term.setCursorPos(4, 5)
colore(colors.white)
sfondo(colors.red)
term.write("16 secchi")

term.setCursorPos(4, 9)
colore(colors.white)
sfondo(colors.red)
term.write("32 secchi")

term.setCursorPos(4, 13)
colore(colors.white)
sfondo(colors.red)
term.write("Pieno Loco")

term.setCursorPos(4, 17)
colore(colors.white)
sfondo(colors.red)
term.write("Pieno Vagone")

while true do
    drawFilledBox(18, 5, 50, 17, colors.yellow)
    term.setCursorPos(19, 6)
    colore(colors.black)
    sfondo(colors.yellow)
    term.write("ISTRUZIONI - LEGGERE:")
    term.setCursorPos(19, 8)
    term.write("1. Posizionare il vagone/loco")
    term.setCursorPos(19, 9)
    term.write("   sotto l'erogatore appropriato")
    term.setCursorPos(19, 10)
    term.write("2. Controllare che il vagone")
    term.setCursorPos(19, 11)
    term.write("   o loco siano vuoti.")
    term.setCursorPos(19, 12)
    term.write("3. Selezionare su questo PC")
    term.setCursorPos(19, 13)
    term.write("   la quantità desiderata")
    term.setCursorPos(19, 14)
    term.write("4. Attendere l'erogazione")
    term.setCursorPos(19, 15)
    term.write("5. Non appena l'operazione sarà")
    term.setCursorPos(19, 16)
    term.write("   terminata liberare l'area")

    local event, par1, x, y = os.pullEvent("mouse_click")
    if event == "mouse_click" and x >= 3 and x <= 15 and y >= 4 and y <= 6 then
        qta = 16
        materiale = "16 secchi"
        prezzo = qta * costante
        break
    elseif event == "mouse_click" and x >= 3 and x <= 15 and y >= 8 and y <= 10 then
        qta = 32
        materiale = "32 secchi"
        prezzo = qta * costante
        break
    elseif event == "mouse_click" and x >= 3 and x <= 15 and y >= 12 and y <= 14 then
        qta = 160
        materiale = "Pieno Loco"
        prezzo = qta * costante
        break
    elseif event == "mouse_click" and x >= 3 and x <= 15 and y >= 16 and y <= 18 then
        qta = 208
        prezzo = qta * costante
        materiale = "Pieno Vagone"
        break
    end
    sleep(1)
end
clear()

if redstone.getInput("back") == true then
    errore("Gasolio esaurito")
end
sfondo(colours.blue)
    term.clear()
    term.setCursorPos(1, 1)
    titolo("Paga con Nebraska Pay")
    sfondo(colours.blue)
    colore(colors.white)
    print(
        "\n\nInserire i propri dati. Premere Enter per confermare i dati inseriti. \nSe i dati sono corretti, il campo diventerà verde. Se sono errati, il programma chiederà di reinserirli nuovamente.")
 
    -- nomeutente
    term.setCursorPos(1, 9)
    term.write("Nome utente:")
    term.setCursorPos(15, 9)
    fineColore()
    utenteAccettato = false
    conteggioErrori = 0
    while utenteAccettato == false do
        utente = read()
        checkuser = http.get(
                        "http://172.16.20.220/luanet/servlets/nebraskapay.php?auth=no&richiesta=verifica&utente=" ..
                            utente).readAll()
        tabcheckuser = textutils.unserialize(checkuser)
        if tabcheckuser.stato == "OK" then
            term.setCursorPos(15, 9)
            colore(colours.lime)
            term.write(utente)
            fineColore()
            utenteAccettato = true
        else
            term.setCursorPos(15, 9)
            colore(colours.red)
            term.write("Utente errato!          ")
            sleep(2)
            term.setCursorPos(15, 9)
            fineColore()
            term.write("                       ")
            term.setCursorPos(15, 9)
            conteggioErrori = conteggioErrori + 1
            if conteggioErrori > 2 then
                term.setCursorPos(15, 9)
                colore(colours.red)
                errore("Troppi tentativi di immissione errati    ")
            end
        end
    end
    colore(colours.white)
    term.setCursorPos(1, 11)
    term.write("Password:")
    term.setCursorPos(15, 11)
    conteggioErrori = 0
    passAccettata = false
    while passAccettata == false do
        fineColore()
        password = read("#")
        colore(colours.black)
        checkuser = http.get(
                        "http://172.16.20.220/luanet/servlets/nebraskapay.php?auth=" ..
                            password .. "&richiesta=addebito&valore=0&utente=" ..
                            utente).readAll()
        risposta = textutils.unserialize(checkuser)
        if risposta.stato == "OK" then
            term.setCursorPos(15, 11)
            colore(colours.lime)
            term.write("##############               ")
            fineColore()
            passAccettata = true
        else
            term.setCursorPos(15, 11)
            colore(colours.red)
            term.write("Password errata!            ")
            conteggioErrori = conteggioErrori + 1
            sleep(2)
            term.setCursorPos(15, 11)
            term.write("                               ")
            term.setCursorPos(15, 11)
            if conteggioErrori > 2 then
                term.setCursorPos(15, 11)
                colore(colours.red)
                errore("Troppi tentativi di immissione errati")
            end
        end
    end
 
    -- Mostra saldo
    term.clear()
 
    term.clear()
    term.setCursorPos(1, 1)
    titolo("Paga con Nebraska Pay")
    sfondo(colours.blue)
    colore(colors.white)
    print("\n\nSaldo disponibile: " .. risposta.saldo .. " IC")
    if tonumber(risposta.saldo) >= tonumber(prezzo) then
        prezzoAccettato = true
    else
        errore("Disponibilità insufficiente!")
    end
    term.setCursorPos(1, 3)
    term.write("Invio di ")
    term.write(prezzo)
    term.write(" IC a favore di ")
    term.write("PECTEN")
    print("\n\nConfermare l'operazione?")
    colore(colors.yellow)
    print("\n\nInvia il denaro           Annulla")
    while true do
        event, key, x, y = os.pullEvent()
        if event == "mouse_click" and x < 16 and y == 8 then
            inviaDenaro = http.get(
                              "http://172.16.20.220/luanet/servlets/nebraskapay.php?auth=" ..
                                  password .. "&utente=" .. utente ..
                                  "&richiesta=trasferimento&valore=" .. prezzo ..
                                  "&beneficiario=RGCorp").readAll()
            risultatoInvio = textutils.unserialize(inviaDenaro)
            if risultatoInvio.stato == "OK" then
                colore(colors.white)
                break
            else
                clear()
                errore("Errore durante il pagamento")
            end
        elseif event == "mouse_click" and x > 18 and y == 8 then
            errore("Pagamento annullato dall'utente.")
        end
    end
clear()
titolo("Erogazione in corso")

print("\n\nErogazione di " .. materiale .. " di gasolio in corso...")
ben = peripheral.wrap("back")
ben.makeRequest(ben.getItemIdentifierIDFor(21258, 18), qta)
sleep(10)
term.setTextColor(colors.black)
term.setBackgroundColor(colors.black)
shell.run("rm startup") 
shell.run("pastebin get 5WVAm8cT startup")
os.reboot()