lego11

CARTA Slot Machine

May 15th, 2018 (edited)
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local maxw, maxh = term.getSize()
  2.  
  3. function cartaNonValida()
  4.     clearRed()
  5.     print("Carta non valida o corrotta.")
  6.     print("")
  7.     print("Premere un tasto per uscire")
  8.     os.pullEvent("key")
  9.     os.reboot()
  10. end
  11.  
  12. function cartaVuota()
  13.     clearRed()
  14.     print("Carta smagnetizzata o vuota.")
  15.     print("")
  16.     print("Premere un tasto per uscire")
  17.     os.pullEvent("key")
  18.     os.reboot()
  19. end
  20.  
  21. function creditoInsufficiente()
  22.     clearRed()
  23.     print("Credito insufficiente.")
  24.     print("")
  25.     print("Premere un tasto per uscire")
  26.     os.pullEvent("key")
  27.     os.reboot()
  28. end
  29.  
  30. -- IMPLEMENTAZIONE DEL DRAWFILLEDBOX
  31. local function drawPixelInternal(xPos, yPos)
  32.     term.setCursorPos(xPos, yPos)
  33.     term.write(" ")
  34. end
  35.  
  36. local tColourLookup = {}
  37. for n = 1, 16 do
  38.     tColourLookup[string.byte("0123456789abcdef", n, n)] = 2 ^ (n - 1)
  39. end
  40.  
  41. function drawFilledBox(startX, startY, endX, endY, nColour)
  42.     if type(startX) ~= "number" or type(startX) ~= "number" or type(endX) ~=
  43.         "number" or type(endY) ~= "number" or
  44.         (nColour ~= nil and type(nColour) ~= "number") then
  45.         error("Expected startX, startY, endX, endY, colour", 2)
  46.     end
  47.  
  48.     startX = math.floor(startX)
  49.     startY = math.floor(startY)
  50.     endX = math.floor(endX)
  51.     endY = math.floor(endY)
  52.  
  53.     if nColour then term.setBackgroundColor(nColour) end
  54.     if startX == endX and startY == endY then
  55.         drawPixelInternal(startX, startY)
  56.         return
  57.     end
  58.  
  59.     local minX = math.min(startX, endX)
  60.     if minX == startX then
  61.         minY = startY
  62.         maxX = endX
  63.         maxY = endY
  64.     else
  65.         minY = endY
  66.         maxX = startX
  67.         maxY = startY
  68.     end
  69.  
  70.     for x = minX, maxX do for y = minY, maxY do drawPixelInternal(x, y) end end
  71. end
  72.  
  73. function clear()
  74.     sfondo(colors.lightBlue)
  75.     term.clear()
  76.     term.setCursorPos(1, 1)
  77. end
  78.  
  79. function clearRed()
  80.     sfondo(colors.red)
  81.     term.clear()
  82.     term.setCursorPos(1, 1)
  83. end
  84.  
  85.  
  86. function colore(sfumatura) term.setTextColour(sfumatura) end
  87.  
  88. function sfondo(sfumaturaSfondo) term.setBackgroundColour(sfumaturaSfondo) end
  89.  
  90. function titolo(testo)
  91.     drawFilledBox(1, 1, maxw, 1, colors.yellow)
  92.     term.setCursorPos((maxw - #testo) / 2, 1)
  93.     colore(colors.black)
  94.     term.write(testo)
  95.     sfondo(colors.lightBlue)
  96. end
  97.  
  98. clear()
  99.  
  100. titolo("KARABASHMED MATERIALI ALL'INGROSSO")
  101. -- startx, starty, endx, endy
  102. drawFilledBox(3, 5, 15, 7, colors.black)
  103. drawFilledBox(3, 10, 15, 12, colors.lightGray)
  104. drawFilledBox(3, 15, 15, 17, colors.orange)
  105. term.setCursorPos(4, 6)
  106. colore(colors.white)
  107. sfondo(colors.black)
  108. term.write("Carbone")
  109.  
  110. term.setCursorPos(4, 11)
  111. colore(colors.black)
  112. sfondo(colors.lightGray)
  113. term.write("Ferro")
  114.  
  115. term.setCursorPos(4, 16)
  116. colore(colors.black)
  117. sfondo(colors.orange)
  118. term.write("Rame")
  119.  
  120. while true do
  121.     drawFilledBox(18, 5, 50, 17, colors.yellow)
  122.     term.setCursorPos(19, 6)
  123.     colore(colors.black)
  124.     sfondo(colors.yellow)
  125.     term.write("ISTRUZIONI - LEGGERE:")
  126.     term.setCursorPos(19, 8)
  127.     term.write("1. Posizionare il vagone sotto")
  128.     term.setCursorPos(19, 9)
  129.     term.write("   l'erogatore appropriato")
  130.     term.setCursorPos(19, 10)
  131.     term.write("2. Controllare che la spia")
  132.     term.setCursorPos(19, 11)
  133.     term.write("   «presenza vagone» sia accesa")
  134.     term.setCursorPos(19, 12)
  135.     term.write("3. Selezionare su questo PC")
  136.     term.setCursorPos(19, 13)
  137.     term.write("   il prodotto desiderato")
  138.     term.setCursorPos(19, 14)
  139.     term.write("4. Attendere l'erogazione")
  140.     term.setCursorPos(19, 15)
  141.     term.write("5. Non appena l'operazione sarà")
  142.     term.setCursorPos(19, 16)
  143.     term.write("   terminata liberare l'area")
  144.  
  145.     local event, par1, x, y = os.pullEvent("mouse_click")
  146.     if event == "mouse_click" and x >= 3 and x <= 15 and y >= 5 and y <= 7 then
  147.         cavo1 = colors.white
  148.         cavo2 = colors.lightBlue
  149.         prezzo = 25
  150.         materiale = "Carbone"
  151.         break
  152.     elseif event == "mouse_click" and x >= 3 and x <= 15 and y >= 10 and y <= 12 then
  153.         cavo1 = colors.orange
  154.         cavo2 = colors.yellow
  155.         prezzo = 512
  156.         materiale = "Ferro"
  157.         break
  158.     elseif event == "mouse_click" and x >= 3 and x <= 15 and y >= 15 and y <= 17 then
  159.         cavo1 = colors.magenta
  160.         cavo2 = colors.lime
  161.         prezzo = 490
  162.         materiale = "Rame"
  163.         break
  164.     end
  165.     sleep(1)
  166. end
  167. clear()
  168.  
  169. while true do
  170.     titolo("Vendita di un vagone di " .. materiale)
  171.  
  172.     term.setCursorPos(3, 5)
  173.     print("Si sta per acquistare un vagone di " .. materiale)
  174.     print("\n  Il prezzo è di " .. prezzo .. " IC")
  175.     colore(colors.white)
  176.     print("\n\n  Inserire la carta per procedere\n  con l'acquisto")
  177.  
  178.     drawFilledBox(15, 14, 36, 16, colors.red)
  179.     term.setCursorPos(22, 15)
  180.     colore(colors.white)
  181.     term.write("Annulla")
  182.  
  183.     m = peripheral.wrap("bottom") -- magcard
  184.     m.setInsertCardLight(true)
  185.  
  186.     local event, par1, x, y = os.pullEvent()
  187.     if event == "mouse_click" and x >= 15 and x <= 36 and y >= 14 and y <= 16 then
  188.         m.setInsertCardLight(false)
  189.         os.reboot()
  190.     elseif event == "mag_swipe" then
  191.         m.setInsertCardLight(false)
  192.         break
  193.     end
  194. end
  195.  
  196. if par1 == nil then cartaVuota() end
  197.  
  198. tab = textutils.unserialize(par1)
  199.  
  200. if type(tab) ~= "table" then cartaNonValida() end
  201.  
  202. if tab.t ~= "cc" then cartaNonValida() end
  203.  
  204. if tab.e ~= "BN" then cartaNonValida() end
  205.  
  206. credito = tonumber(tab.v)
  207.  
  208. if prezzo > credito then
  209.     creditoInsufficiente()
  210. end
  211.  
  212. titolo("Conferma pagamento")
  213.  
  214. print("\n\nInserire nuovamente la carta per confermare il pagamento")
  215.  
  216.  
  217. tabr.t = tab.t
  218. tabr.e = tab.e
  219. tabr.p = tab.p
  220. tabr.v = tostring(credito - prezzo)
  221.  
  222. ncr = textutils.serialize(tabr)
  223. m.setInsertCardLight(true)
  224. m.beginWrite(ncr,lbl)
  225. os.pullEvent("mag_write_done")
  226. m.setInsertCardLight(false)
  227.  
  228. titolo("Erogazione in corso")
  229.  
  230. print("\n\nErogazione " .. materiale .. " in corso...")
Advertisement
Add Comment
Please, Sign In to add comment