lego11

LotteriaNR

Sep 24th, 2020 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.18 KB | None | 0 0
  1. local maxw, maxh = term.getSize()
  2.  
  3. -- IMPLEMENTAZIONE DEL DRAWFILLEDBOX
  4. local function drawPixelInternal(xPos, yPos)
  5.     term.setCursorPos(xPos, yPos)
  6.     term.write(" ")
  7. end
  8.  
  9. local tColourLookup = {}
  10. for n = 1, 16 do
  11.     tColourLookup[string.byte("0123456789abcdef", n, n)] = 2 ^ (n - 1)
  12. end
  13.  
  14. function drawFilledBox(startX, startY, endX, endY, nColour)
  15.     if type(startX) ~= "number" or type(startX) ~= "number" or type(endX) ~=
  16.         "number" or type(endY) ~= "number" or
  17.         (nColour ~= nil and type(nColour) ~= "number") then
  18.         error("Expected startX, startY, endX, endY, colour", 2)
  19.     end
  20.  
  21.     startX = math.floor(startX)
  22.     startY = math.floor(startY)
  23.     endX = math.floor(endX)
  24.     endY = math.floor(endY)
  25.  
  26.     if nColour then term.setBackgroundColor(nColour) end
  27.     if startX == endX and startY == endY then
  28.         drawPixelInternal(startX, startY)
  29.         return
  30.     end
  31.  
  32.     local minX = math.min(startX, endX)
  33.     if minX == startX then
  34.         minY = startY
  35.         maxX = endX
  36.         maxY = endY
  37.     else
  38.         minY = endY
  39.         maxX = startX
  40.         maxY = startY
  41.     end
  42.  
  43.     for x = minX, maxX do for y = minY, maxY do drawPixelInternal(x, y) end end
  44. end
  45.  
  46. function clear()
  47.     sfondo(colors.blue)
  48.     term.clear()
  49.     term.setCursorPos(1, 1)
  50. end
  51.  
  52. function fineColore() term.setTextColour(colours.white) end
  53.  
  54. function fineSfondo() term.setBackgroundColour(colours.black) end
  55.  
  56. function colore(sfumatura) term.setTextColour(sfumatura) end
  57.  
  58. function sfondo(sfumaturaSfondo) term.setBackgroundColour(sfumaturaSfondo) end
  59.  
  60. function titolo(testo)
  61.     drawFilledBox(1, 1, maxw, 1, colors.yellow)
  62.     term.setCursorPos((maxw - #testo) / 2, 1)
  63.     colore(colors.black)
  64.     term.write(testo)
  65.     sfondo(colors.blue)
  66. end
  67.  
  68. function errore(errore)
  69.     sfondo(colors.red)
  70.     colore(colors.white)
  71.     term.clear()
  72.     term.setCursorPos(1, 1)
  73.     titolo("Errore irreversibile")
  74.     term.setCursorPos(1, 3)
  75.     sfondo(colors.red)
  76.     colore(colors.white)
  77.     print(errore)
  78.     print("\n\n\nAttendere qualche secondo...")
  79.     os.sleep(5)
  80.     os.reboot()
  81. end
  82.  
  83. function compra_biglietto()
  84.  
  85.     clear()
  86.     titolo("Lotteria di New Radeon")
  87.  
  88.     drawFilledBox(3, 3, maxw - 3, 5, colors.lime);
  89.     term.setCursorPos(6, 4)
  90.     term.write("Montepremi attuale: " .. verifica_ini.montepremi .. " IC")
  91.  
  92.     term.setCursorPos(1, 10)
  93.  
  94.     term.setBackgroundColor(colors.blue)
  95.     term.setTextColor(colors.white)
  96.     print(
  97.         "Sfiora il sensore sottostante per comprare un biglietto ed aggiungere 20 IC al montepremi!\n\nCosto di un biglietto: 5 IC\nPagamento con Nebraska Pay.")
  98.     __, b = os.pullEvent("player")
  99.  
  100.     sir_biglietto = textutils.unserialize(
  101.                         http.get(
  102.                             "http://172.16.20.220/luanet/servlets/lotterianr/?richiesta=esiste_giocatore&giocatore=" ..
  103.                                 b).readAll())
  104.  
  105.     if sir_biglietto.stato == "KO" then errore(sir_biglietto.errore) end
  106.  
  107.     sfondo(colours.blue)
  108.     term.clear()
  109.     term.setCursorPos(1, 1)
  110.     titolo("Paga con Nebraska Pay")
  111.     sfondo(colours.blue)
  112.     colore(colors.white)
  113.     print(
  114.         "\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.")
  115.  
  116.     -- nomeutente
  117.     term.setCursorPos(1, 9)
  118.     term.write("Nome utente:")
  119.     term.setCursorPos(15, 9)
  120.     fineColore()
  121.     utenteAccettato = false
  122.     conteggioErrori = 0
  123.     while utenteAccettato == false do
  124.         utente = read()
  125.         checkuser = http.get(
  126.                         "http://172.16.20.220/luanet/servlets/nebraskapay.php?auth=no&richiesta=verifica&utente=" ..
  127.                             utente).readAll()
  128.         tabcheckuser = textutils.unserialize(checkuser)
  129.         if tabcheckuser.stato == "OK" then
  130.             term.setCursorPos(15, 9)
  131.             colore(colours.lime)
  132.             term.write(utente)
  133.             fineColore()
  134.             utenteAccettato = true
  135.         else
  136.             term.setCursorPos(15, 9)
  137.             colore(colours.red)
  138.             term.write("Utente errato!          ")
  139.             sleep(2)
  140.             term.setCursorPos(15, 9)
  141.             fineColore()
  142.             term.write("                       ")
  143.             term.setCursorPos(15, 9)
  144.             conteggioErrori = conteggioErrori + 1
  145.             if conteggioErrori > 2 then
  146.                 term.setCursorPos(15, 9)
  147.                 colore(colours.red)
  148.                 errore("Troppi tentativi di immissione errati    ")
  149.             end
  150.         end
  151.     end
  152.     colore(colours.white)
  153.     term.setCursorPos(1, 11)
  154.     term.write("Password:")
  155.     term.setCursorPos(15, 11)
  156.  
  157.     conteggioErrori = 0
  158.     passAccettata = false
  159.     while passAccettata == false do
  160.         fineColore()
  161.         password = read("#")
  162.         colore(colours.black)
  163.         checkuser = http.get(
  164.                         "http://172.16.20.220/luanet/servlets/nebraskapay.php?auth=" ..
  165.                             password .. "&richiesta=addebito&valore=0&utente=" ..
  166.                             utente).readAll()
  167.         risposta = textutils.unserialize(checkuser)
  168.         if risposta.stato == "OK" then
  169.             term.setCursorPos(15, 11)
  170.             colore(colours.lime)
  171.             term.write("##############               ")
  172.             fineColore()
  173.             passAccettata = true
  174.         else
  175.             term.setCursorPos(15, 11)
  176.             colore(colours.red)
  177.             term.write("Password errata!            ")
  178.             conteggioErrori = conteggioErrori + 1
  179.             sleep(2)
  180.             term.setCursorPos(15, 11)
  181.             term.write("                               ")
  182.             term.setCursorPos(15, 11)
  183.             if conteggioErrori > 2 then
  184.                 term.setCursorPos(15, 11)
  185.                 colore(colours.red)
  186.                 errore("Troppi tentativi di immissione errati")
  187.             end
  188.         end
  189.     end
  190.  
  191.     -- Mostra saldo
  192.     term.clear()
  193.  
  194.     term.clear()
  195.     term.setCursorPos(1, 1)
  196.     titolo("Paga con Nebraska Pay")
  197.     sfondo(colours.blue)
  198.     colore(colors.white)
  199.     print("\n\nSaldo disponibile: " .. risposta.saldo .. " IC")
  200.     importo = 5
  201.     if tonumber(risposta.saldo) >= tonumber(importo) then
  202.         importoAccettato = true
  203.     else
  204.         errore("Disponibilità insufficiente!")
  205.     end
  206.     term.setCursorPos(1, 3)
  207.     term.write("Invio di ")
  208.     term.write(importo)
  209.     term.write(" IC a favore di ")
  210.     term.write("Comune di New Radeon")
  211.     print("\n\nConfermare l'operazione?")
  212.     colore(colors.yellow)
  213.     print("\n\nInvia il denaro           Annulla")
  214.     while true do
  215.         event, key, x, y = os.pullEvent()
  216.         if event == "mouse_click" and x < 16 and y == 8 then
  217.             inviaDenaro = http.get(
  218.                               "http://172.16.20.220/luanet/servlets/nebraskapay.php?auth=" ..
  219.                                   password .. "&utente=" .. utente ..
  220.                                   "&richiesta=trasferimento&valore=" .. importo ..
  221.                                   "&beneficiario=ComuneNR").readAll()
  222.             risultatoInvio = textutils.unserialize(inviaDenaro)
  223.             if risultatoInvio.stato == "OK" then
  224.                 colore(colors.white)
  225.                 break
  226.             else
  227.                 clear()
  228.                 errore("Errore durante il pagamento")
  229.             end
  230.         elseif event == "mouse_click" and x > 18 and y == 8 then
  231.             errore("Pagamento annullato dall'utente.")
  232.         end
  233.     end
  234.  
  235.     sir_biglietto = textutils.unserialize(
  236.                         http.get(
  237.                             "http://172.16.20.220/luanet/servlets/lotterianr/?richiesta=nuovo_biglietto&giocatore=" ..
  238.                                 b).readAll())
  239.  
  240.     if sir_biglietto.stato == "OK" then
  241.         clear()
  242.         titolo("Lotteria di New Radeon")
  243.         term.setCursorPos(1, 3)
  244.         colore(colors.white)
  245.         print("Grazie per l'acquisto!\n\n")
  246.         print("Questo è il numero del tuo biglietto: " .. sir_biglietto.numero)
  247.         print(
  248.             "\nNon temere di dimenticarlo, se vincerai riceverai un'email!")
  249.         print("\nBuona fortuna!")
  250.         os.sleep(5)
  251.         os.reboot()
  252.     else
  253.         errore(sir_biglietto.errore)
  254.     end
  255. end
  256.  
  257. function prendi_premio()
  258.     winner = textutils.unserialize(http.get(
  259.                                        "http://172.16.20.220/luanet/servlets/lotterianr/?richiesta=verifica_vincita")
  260.                                        .readAll())
  261.     clear()
  262.     titolo("Lotteria di New Radeon")
  263.  
  264.     drawFilledBox(3, 3, maxw - 3, 5, colors.lime);
  265.     term.setCursorPos(6, 4)
  266.     term.write("Vincitore: " .. winner.numero .. " - " .. winner.giocatore .. " - " .. verifica_ini.montepremi .. " IC")
  267.  
  268.     term.setCursorPos(1, 7)
  269.  
  270.     term.setBackgroundColor(colors.blue)
  271.     term.setTextColor(colors.white)
  272.     print(
  273.         "Sfiora il sensore sottostante per ritirare la tua vincita!")
  274.     __, b = os.pullEvent("player")
  275.     if b == winner.giocatore then
  276.         sfondo(colours.blue)
  277.         term.clear()
  278.         term.setCursorPos(1, 1)
  279.         titolo("Ritira la vincita con Nebraska Pay")
  280.         sfondo(colours.blue)
  281.         colore(colors.white)
  282.         print(
  283.             "\n\nInserire l'utente nPay per ritirare la vincita. 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.")
  284.    
  285.         -- nomeutente
  286.         term.setCursorPos(1, 9)
  287.         term.write("Nome utente:")
  288.         term.setCursorPos(15, 9)
  289.         fineColore()
  290.         utenteAccettato = false
  291.         conteggioErrori = 0
  292.         while utenteAccettato == false do
  293.             utente = read()
  294.             checkuser = http.get(
  295.                             "http://172.16.20.220/luanet/servlets/nebraskapay.php?auth=no&richiesta=verifica&utente=" ..
  296.                                 utente).readAll()
  297.             tabcheckuser = textutils.unserialize(checkuser)
  298.             if tabcheckuser.stato == "OK" then
  299.                 term.setCursorPos(15, 9)
  300.                 colore(colours.lime)
  301.                 term.write(utente)
  302.                 fineColore()
  303.                 utenteAccettato = true
  304.             else
  305.                 term.setCursorPos(15, 9)
  306.                 colore(colours.red)
  307.                 term.write("Utente errato!          ")
  308.                 sleep(2)
  309.                 term.setCursorPos(15, 9)
  310.                 fineColore()
  311.                 term.write("                       ")
  312.                 term.setCursorPos(15, 9)
  313.                 conteggioErrori = conteggioErrori + 1
  314.                 if conteggioErrori > 2 then
  315.                     term.setCursorPos(15, 9)
  316.                     colore(colours.red)
  317.                     errore("Troppi tentativi di immissione errati    ")
  318.                 end
  319.             end
  320.         end
  321.         sir_biglietto = textutils.unserialize(
  322.             http.get(
  323.                 "http://172.16.20.220/luanet/servlets/lotterianr/?richiesta=paga_vincitore&npay=".. utente .."&giocatore=" .. winner.giocatore).readAll())
  324.         if sir_biglietto.stato == "KO" then errore(sir_biglietto.errore) end
  325.         sfondo(colours.blue)
  326.         term.clear()
  327.         term.setCursorPos(1, 1)
  328.         titolo("Ritira la vincita con Nebraska Pay")
  329.         sfondo(colours.blue)
  330.         colore(colors.white)
  331.         print("\n\n\nIl montepremi di ".. verifica_ini.montepremi .." IC è stato accreditato con successo sul conto '" .. utente .. "'.\n\nBuona giornata!")
  332.         os.sleep(5)
  333.         os.reboot()
  334.     else
  335.         errore("Non sei il vincitore - non fare il furbo.")
  336.         os.sleep(5)
  337.         os.reboot()
  338.     end
  339.  
  340. end
  341.  
  342. verifica_ini = textutils.unserialize(http.get(
  343.                                          "http://172.16.20.220/luanet/servlets/lotterianr/?richiesta=verifica_stato")
  344.                                          .readAll())
  345. if verifica_ini.stato == "NA" then
  346.     compra_biglietto()
  347. elseif verifica_ini.stato == "EA" then
  348.     prendi_premio()
  349. else
  350.     errore("Errore fatale del server di gioco")
  351.     os.sleep(5)
  352.     os.reboot()
  353. end
  354.  
Add Comment
Please, Sign In to add comment