CCCPaste

Versamenti Qtz Cannery

-- Versamenti FRACASSO
-- Variabili config
local uri = "http://172.16.20.220/luanet/servlets/vendite/cannery"
local pb = "5t0sDU8W"

local maxw, maxh = term.getSize()

ben = peripheral.wrap("back")

-- funzioni helper di base

function tablelength(T)
    local count = 0
    for _ in pairs(T) do
        count = count + 1
    end
    return count
end

function clear()
    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

-- 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 titolo(testo)
    drawFilledBox(1, 1, maxw, 1, colors.yellow)
    term.setCursorPos((maxw - #testo) / 2, 1)
    colore(colors.black)
    term.write(testo)
    sfondo(colors.lightBlue)
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

-- verifica servizi online e recupero prezzario

prezzario_raw = http.get(uri .. "/?richiesta=prezzario_lua").readAll()
prezzario = textutils.unserialize(prezzario_raw)

if type(prezzario) ~= "table" then
    errore("Impossibile comunicare con il server interbancario")
end

-- variabili quantità

counted_items = {}
id_items = {}

-- enumerazione prezzario

acceptedItems = ""

for k, v in pairs(prezzario) do
    id_items[ben.getItemIdentifierIDFor(v.id, v.meta)] = k
    acceptedItems = acceptedItems .. v.nome .. ", "
end

-- PER DOPO print("Item: " .. id_items[v[1]] .. " qty: " .. v[2] .. " price: " .. prezzario[id_items[v[1]]]["prezzo"])

-- vuota il bancomat
ben.getAvailableItems()
i, j = os.pullEvent("available_items_return")

for k, v in pairs(j) do
    counted_items[id_items[v[1]]] = v[2]
    ben.makeRequest(v[1], v[2])
    sleep(0.1)
end

-- resetta counted_items e controlla se è vuoto

counted_items = {}

ben.getAvailableItems()
i, j = os.pullEvent("available_items_return")
for k, v in pairs(j) do
    counted_items[id_items[v[1]]] = v[2]
    sleep(0.1)
end

if tablelength(counted_items) ~= 0 then
    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("Svuotamento non possibile, sportello fuori servizio")
    print("\n\n\nContattare il Banco di Nebraska")
    os.sleep(60)
    os.reboot()
end

sfondo(colors.lightBlue)
clear()
titolo("Banco di Nebraska - Sistema sportelli vendite")
sfondo(colors.lightBlue)
print("\n\n")
print("Questo sportello accetta: " .. acceptedItems:sub(1, -3))

-- pulsante vendi
drawFilledBox(16, 15, 33, 17, colors.white)
term.setCursorPos(17, 16)
term.write("Inizia a vendere")

event = os.pullEvent()

sfondo(colours.lightBlue)
term.clear()
term.setCursorPos(1, 1)
titolo("Accredita su Nebraska Pay")
sfondo(colours.lightBlue)

term.setCursorPos(1, 3)
print("Inserire i propri dati.")
colore(colors.gray)
print(
    "Premere Enter per confermare i dati inseriti.\nSe i dati sono corretti, il campo diventerà verde.\nSe sono errati, il programma chiederà di reinserirli nuovamente.")

-- nomeutente
colore(colors.black)
term.setCursorPos(1, 9)
term.write("Nome utente:")
term.setCursorPos(15, 9)
fineColore()
utenteAccettato = false
conteggioErrori = 0
while utenteAccettato == false do
    utente = read()
    utente = string.gsub(utente, "%s", "")
    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.black)
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

credito = tonumber(risposta.saldo)
clear()

while true do

    titolo("Pannello utente nPay")
    term.setCursorPos(1, 3)
    term.write("Benvenuto, ")
    colore(colors.white)
    term.write(utente)
    colore(colors.black)
    term.write("!         Saldo: ")
    colore(colors.white)
    term.write(risposta.saldo)
    colore(colors.black)
    term.write(" IC")
    print("\n\nOperazioni disponibili:\n")
    sfondo(colors.white)
    print(" Vendi oggetti          \n")
    colore(colors.red)
    print(" Annulla operazione     \n")
    colore(colors.black)

    antitonto = os.startTimer(60)

    event, key, x, y = os.pullEventRaw()
    if event == "mouse_click" and y == 7 and x <= 24 then
      sfondo(colors.lightBlue)
      clear()
        titolo("Operazione di vendita - Passo 1")

        redstone.setOutput("left", true)
        print("\n\nInserire gli oggetti nello sportello a destra oppure collocare il Deep Storage di fronte al cubo rosso.")
        print("")
        print("Eventuali oggetti non accettati non verranno ritirati dal meccanismo.\n")

        sfondo(colors.red)
        print("ATTENZIONE: NON RIAVVIARE IL COMPUTER O SI PERDERA' IL VERSAMENTO")
        sfondo(colors.lightBlue)

        print("")
        print("Quando si è pronti per continuare, premere un tasto qualsiasi.")
        os.pullEvent("key")

        clear()

        redstone.setOutput("left", false)
        titolo("Operazione di vendita - Passo 2")

        print("\n\nConteggio oggetti venduti in corso...")

        term.setCursorPos(3, 6)
        term.write("*---")
        sleep(0.5)
        term.setCursorPos(3, 6)
        term.write("-*--")
        sleep(0.5)
        term.setCursorPos(3, 6)
        term.write("--*-")
        sleep(0.5)
        term.setCursorPos(3, 6)
        term.write("---*")
        sleep(0.5)
        term.setCursorPos(3, 6)
        term.write("--*-")
        sleep(0.5)
        term.setCursorPos(3, 6)
        term.write("-*--")
        sleep(0.5)
        term.setCursorPos(3, 6)
        term.write("*---")
        sleep(0.5)

        ben = peripheral.wrap("back")
        ben.getAvailableItems()
        i, j = os.pullEvent("available_items_return")

        clear()

        -- vuota il bancomat
        ben.getAvailableItems()
        i, j = os.pullEvent("available_items_return")

        for k, v in pairs(j) do
            counted_items[id_items[v[1]]] = v[2]
            ben.makeRequest(v[1], v[2])
            sleep(0.1)
        end

        if tablelength(counted_items) == 0 then
            errore("Nessun oggetto risulta essere stato introdotto")
        end

        titolo("Operazione di vendita - Passo 3")
        print("\n\nGli oggetti sono stati conteggiati correttamente.\nComunicazione con i servizi interbancari in corso...")
        print("")
        items_uri = ""

        for k, v in pairs(counted_items) do
            items_uri = items_uri .. "&" .. k .. "=" .. v
        end

        inviaDenaro = http.get(uri .. "/?richiesta=vendita&auth=" .. password .. "&utente=" .. utente ..
                                   "&richiesta=vendita" .. items_uri).readAll()
        risultatoInvio = textutils.unserialize(inviaDenaro)
        if risultatoInvio.stato == "OK" then
            colore(colors.white)
        else
            clear()
            errore("Errore durante il pagamento")
            sleep(5)
            os.reboot()
        end
        sleep(1)
        print("Comunicazione con i servizi fiscali in corso...")
        sleep(1)
        clear()
        titolo("Operazione di vendita conclusa")
        print("\n\nDenaro versato: " .. math.abs(tonumber(risultatoInvio.credito) - tonumber(risultatoInvio.saldo)) .. " IC")
        print("")
        print("Il totale è stato comunicato all'Agenzia dei Tributi e comparirà nel proprio cassetto fiscale")
        print("")
        print("Grazie!")
        print("")
        print("Tra poco sarà possibile eseguire una nuova operazione.")
        sleep(10)
        term.setTextColor(colors.lightBlue)
        shell.run("pastebin get " .. pb ..  " startup2")
        if fs.exists("startup2") then
            shell.run("rm startup")
            shell.run("mv startup2 startup")
        end
        os.reboot()
      elseif event == "mouse_click" and y == 9 and x <= 24 then -- Pulsante disconnetti da nPay
        clear()
        break
    elseif event == "timer" and key == antitonto then -- Butta fuori l'utente se non fa niente
        disconnetti()
        break
    end
end
sfondo(colors.lightBlue)
clear()
print("Attendere")
term.setTextColor(colors.lightBlue)
sfondo(colors.lightBlue)
shell.run("pastebin get " .. pb .. " startup2")
if fs.exists("startup2") then
    shell.run("rm startup")
    shell.run("mv startup2 startup")
end
os.reboot()