snoooz

board.lua

Dec 15th, 2022 (edited)
1,383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.47 KB | None | 0 0
  1. local board_img_id = "vkckzmq3"
  2. local mon, old, loggedIn, selected, name, balance, winningNumber
  3.  
  4. local state = {
  5.   red     = 0,
  6.   black   = 0,
  7.  
  8.   onetosix      = 0,
  9.   fourtonine    = 0,
  10.   seventotwelve = 0,
  11.  
  12.   one     = 0,
  13.   two     = 0,
  14.   three   = 0,
  15.   four    = 0,
  16.   five    = 0,
  17.   six     = 0,
  18.   seven   = 0,
  19.   eight   = 0,
  20.   nine    = 0,
  21.   ten     = 0,
  22.   eleven  = 0,
  23.   twelve  = 0,
  24.   zero    = 0
  25. }
  26.  
  27. function init()
  28.   shell.run("delete board.nfp")
  29.   shell.run("pastebin", "get", board_img_id, "board.nfp")
  30.   board_img = paintutils.loadImage("board.nfp")
  31.  
  32.   mon = peripheral.find("monitor")
  33.   mon.setTextScale(0.5)
  34.  
  35.   old = term.redirect(mon)
  36.   w, h = term.getSize()
  37.  
  38.   rednet.open("left")
  39.  
  40.   loggedIn = false
  41.   selected = 1
  42.   winningNumber = nil
  43. end
  44.  
  45. function renderBoard()
  46.   if board_img ~= nil then
  47.     paintutils.drawImage(board_img, 1, 1)
  48.   end
  49.  
  50.   term.setBackgroundColor(colors.green)
  51.   term.setTextColor(colors.red)
  52.   term.setCursorPos(3, 23)
  53.   term.write("leave")
  54.  
  55.   term.setTextColor(colors.white)
  56.   term.setCursorPos(22, 1)
  57.   term.write("welcome, ")
  58.   term.write(name)
  59.   term.write("!")
  60.  
  61.   term.setCursorPos(42, 1)
  62.   term.write("balance: $")
  63.   term.write(balance)
  64.  
  65.   term.setCursorPos(9,7)
  66.   if state.red ~= 0 then setCoinColor(state.red) else term.setBackgroundColor(colors.red) end
  67.   term.write("  ")
  68.   term.setCursorPos(9,16)
  69.   if state.black ~= 0 then setCoinColor(state.black) else term.setBackgroundColor(colors.black) end
  70.   term.write("  ")
  71.  
  72.   term.setCursorPos(19,6)
  73.   if state.onetosix ~= 0 then setCoinColor(state.onetosix) else term.setBackgroundColor(colors.green) end
  74.   term.write("1 - 6")
  75.   term.setCursorPos(19,12)
  76.   if state.fourtonine ~= 0 then setCoinColor(state.fourtonine) else term.setBackgroundColor(colors.green) end
  77.   term.write("4 - 9")
  78.   term.setCursorPos(19,18)
  79.   if state.seventotwelve ~= 0 then setCoinColor(state.seventotwelve) else term.setBackgroundColor(colors.green) end
  80.   term.write("7 - 12")
  81.  
  82.   term.setCursorPos(34,6)
  83.   if state.three ~= 0 then setCoinColor(state.three) else term.setBackgroundColor(colors.red) end
  84.   term.write("3")
  85.   term.setCursorPos(34,12)
  86.   if state.two ~= 0 then setCoinColor(state.two) else term.setBackgroundColor(colors.black) end
  87.   term.write("2")
  88.   term.setCursorPos(34,18)
  89.   if state.one ~= 0 then setCoinColor(state.one) else term.setBackgroundColor(colors.red) end
  90.   term.write("1")
  91.  
  92.   term.setCursorPos(41,6)
  93.   if state.six ~= 0 then setCoinColor(state.six) else term.setBackgroundColor(colors.black) end
  94.   term.write("6")
  95.   term.setCursorPos(41,12)
  96.   if state.five ~= 0 then setCoinColor(state.five) else term.setBackgroundColor(colors.red) end
  97.   term.write("5")
  98.   term.setCursorPos(41,18)
  99.   if state.four ~= 0 then setCoinColor(state.four) else term.setBackgroundColor(colors.black) end
  100.   term.write("4")
  101.  
  102.   term.setCursorPos(48,6)
  103.   if state.nine ~= 0 then setCoinColor(state.nine) else term.setBackgroundColor(colors.red) end
  104.   term.write("9")
  105.   term.setCursorPos(48,12)
  106.   if state.eight ~= 0 then setCoinColor(state.eight) else term.setBackgroundColor(colors.black) end
  107.   term.write("8")
  108.   term.setCursorPos(48,18)
  109.   if state.seven ~= 0 then setCoinColor(state.seven) else term.setBackgroundColor(colors.red) end
  110.   term.write("7")
  111.  
  112.   term.setCursorPos(55,6)
  113.   if state.twelve ~= 0 then setCoinColor(state.twelve) else term.setBackgroundColor(colors.black) end
  114.   term.write("12")
  115.   term.setCursorPos(55,12)
  116.   if state.eleven ~= 0 then setCoinColor(state.eleven) else term.setBackgroundColor(colors.red) end
  117.   term.write("11")
  118.   term.setCursorPos(55,18)
  119.   if state.ten ~= 0 then setCoinColor(state.ten) else term.setBackgroundColor(colors.black) end
  120.   term.write("10")
  121.  
  122.   term.setCursorPos(62,12)
  123.   if state.zero ~= 0 then setCoinColor(state.zero) else term.setBackgroundColor(colors.green) end
  124.   term.write("0")
  125.  
  126.   term.setTextColor(colors.black)
  127.   term.setCursorPos(72,6)
  128.   if selected == 1 then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.yellow) end
  129.   term.write("$1")
  130.   term.setCursorPos(72,10)
  131.   if selected == 4 then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.orange) end
  132.   term.write("$4")
  133.   term.setCursorPos(72,14)
  134.   if selected == 16 then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.pink) end
  135.   term.write("$16")
  136.   term.setCursorPos(72,18)
  137.   if selected == 64 then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lightBlue) end
  138.   term.write("$64")
  139. end
  140.  
  141. function clickHandler()
  142.   event, side, x, y = os.pullEvent("monitor_touch")
  143.  
  144.   if side ~= nil then
  145.     if checkBounds(x, y, 1, 22, 10, 24) then
  146.       loggedIn = false
  147.  
  148.       local b = fs.open("/disk/b.dat", "w")
  149.       b.write(balance)
  150.       b.close()
  151.  
  152.       disk.eject("right")
  153.       name = nil
  154.       balance = nil
  155.     end
  156.     if checkBounds(x, y, 5, 4,  14, 11) then
  157.       if state.red == selected then
  158.         balance = balance + state.red
  159.         state.red = 0
  160.       elseif selected <= balance then
  161.         balance = balance - selected
  162.         state.red = selected
  163.       end
  164.     end
  165.     if checkBounds(x, y, 5, 13, 14, 20) then
  166.       if state.black == selected then
  167.         balance = balance + state.black
  168.         state.black = 0
  169.       elseif selected <= balance then
  170.         balance = balance - selected
  171.         state.black = selected
  172.       end
  173.     end
  174.    
  175.     if checkBounds(x, y, 17, 4,  25, 8) then
  176.       if state.onetosix == selected then
  177.         balance = balance + state.onetosix
  178.         state.onetosix = 0
  179.       elseif selected <= balance then
  180.         balance = balance - selected
  181.         state.onetosix = selected
  182.       end
  183.     end
  184.     if checkBounds(x, y, 17, 10, 25, 14) then
  185.       if state.fourtonine == selected then
  186.         balance = balance + state.fourtonine
  187.         state.fourtonine = 0
  188.       elseif selected <= balance then
  189.         balance = balance - selected
  190.         state.fourtonine = selected
  191.       end
  192.     end
  193.     if checkBounds(x, y, 17, 16, 25, 20) then
  194.       if state.seventotwelve == selected then
  195.         balance = balance + state.seventotwelve
  196.         state.seventotwelve = 0
  197.       elseif selected <= balance then
  198.         balance = balance - selected
  199.         state.seventotwelve = selected
  200.       end
  201.     end
  202.  
  203.     if checkBounds(x, y, 32, 4,  36, 8) then
  204.       if state.three == selected then
  205.         balance = balance + state.three
  206.         state.three = 0
  207.       elseif selected <= balance then
  208.         balance = balance - selected
  209.         state.three = selected
  210.       end
  211.     end
  212.     if checkBounds(x, y, 32, 10, 36, 14) then
  213.       if state.two == selected then
  214.         balance = balance + state.two
  215.         state.two = 0
  216.       elseif selected <= balance then
  217.         balance = balance - selected
  218.         state.two = selected
  219.       end
  220.     end
  221.     if checkBounds(x, y, 32, 16, 36, 20) then
  222.       if state.one == selected then
  223.         balance = balance + state.one
  224.         state.one = 0
  225.       elseif selected <= balance then
  226.         balance = balance - selected
  227.         state.one = selected
  228.       end
  229.     end
  230.  
  231.     if checkBounds(x, y, 39, 4,  43, 8) then
  232.       if state.six == selected then
  233.         balance = balance + state.six
  234.         state.six = 0
  235.       elseif selected <= balance then
  236.         balance = balance - selected
  237.         state.six = selected
  238.       end
  239.     end
  240.     if checkBounds(x, y, 39, 10, 43, 14) then
  241.       if state.five == selected then
  242.         balance = balance + state.five
  243.         state.five = 0
  244.       elseif selected <= balance then
  245.         balance = balance - selected
  246.         state.five = selected
  247.       end
  248.     end
  249.     if checkBounds(x, y, 39, 16, 43, 20) then
  250.       if state.four == selected then
  251.         balance = balance + state.four
  252.         state.four = 0
  253.       elseif selected <= balance then
  254.         balance = balance - selected
  255.         state.four = selected
  256.       end
  257.     end
  258.  
  259.     if checkBounds(x, y, 46, 4,  50, 8) then
  260.       if state.nine == selected then
  261.         balance = balance + state.nine
  262.         state.nine = 0
  263.       elseif selected <= balance then
  264.         balance = balance - selected
  265.         state.nine = selected
  266.       end
  267.     end
  268.     if checkBounds(x, y, 46, 10, 50, 14) then
  269.       if state.eight == selected then
  270.         balance = balance + state.eight
  271.         state.eight = 0
  272.       elseif selected <= balance then
  273.         balance = balance - selected
  274.         state.eight = selected
  275.       end
  276.     end
  277.     if checkBounds(x, y, 46, 16, 50, 20) then
  278.       if state.seven == selected then
  279.         balance = balance + state.seven
  280.         state.seven = 0
  281.       elseif selected <= balance then
  282.         balance = balance - selected
  283.         state.seven = selected
  284.       end
  285.     end
  286.  
  287.     if checkBounds(x, y, 53, 4,  57, 8) then
  288.       if state.twelve == selected then
  289.         balance = balance + state.twelve
  290.         state.twelve = 0
  291.       elseif selected <= balance then
  292.         balance = balance - selected
  293.         state.twelve = selected
  294.       end
  295.     end
  296.     if checkBounds(x, y, 53, 10, 57, 14) then
  297.       if state.eleven == selected then
  298.         balance = balance + state.eleven
  299.         state.eleven = 0
  300.       elseif selected <= balance then
  301.         balance = balance - selected
  302.         state.eleven = selected
  303.       end
  304.     end
  305.     if checkBounds(x, y, 53, 16, 57, 20) then
  306.       if state.ten == selected then
  307.         balance = balance + state.ten
  308.         state.ten = 0
  309.       elseif selected <= balance then
  310.         balance = balance - selected
  311.         state.ten = selected
  312.       end
  313.     end
  314.  
  315.     if checkBounds(x, y, 60, 4,  64, 20) then
  316.       if state.zero == selected then
  317.         balance = balance + state.zero
  318.         state.zero = 0
  319.       elseif selected <= balance then
  320.         balance = balance - selected
  321.         state.zero = selected
  322.       end
  323.     end
  324.  
  325.     if checkBounds(x, y, 71, 5,  75, 7)   then selected = 1  end
  326.     if checkBounds(x, y, 71, 9,  75, 11)  then selected = 4  end
  327.     if checkBounds(x, y, 71, 13, 75, 15)  then selected = 16 end
  328.     if checkBounds(x, y, 71, 17, 75, 19)  then selected = 64 end
  329.   end
  330. end
  331.  
  332. function checkBounds(x, y, x1, y1, x2, y2)
  333.   return x >= x1 and y >= y1 and x <= x2 and y <= y2
  334. end
  335.  
  336. function setCoinColor(coin)
  337.   if coin == 1 then
  338.     term.setBackgroundColor(colors.yellow)
  339.   elseif coin == 4 then
  340.     term.setBackgroundColor(colors.orange)
  341.   elseif coin == 16 then
  342.     term.setBackgroundColor(colors.pink)
  343.   else
  344.     term.setBackgroundColor(colors.lightBlue)
  345.   end
  346. end
  347.  
  348. function calculateWinnings()
  349.   local p = 0
  350.   if winningNumber == 0 then
  351.     if state.zero ~= 0 then p = p + (12 * state.zero) end
  352.   elseif winningNumber == 1 then
  353.     if state.one ~= 0 then p = p + (12 * state.one) end
  354.     if state.red ~= 0 then p = p + (2 * state.red) end
  355.     if state.onetosix ~= 0 then p = p + (3 * state.onetosix) end
  356.   elseif winningNumber == 2 then
  357.     if state.two ~= 0 then p = p + (12 * state.two) end
  358.     if state.black ~= 0 then p = p + (2 * state.black) end
  359.     if state.onetosix ~= 0 then p = p + (3 * state.onetosix) end
  360.   elseif winningNumber == 3 then
  361.     if state.three ~= 0 then p = p + (12 * state.three) end
  362.     if state.red ~= 0 then p = p + (2 * state.red) end
  363.     if state.onetosix ~= 0 then p = p + (3 * state.onetosix) end
  364.   elseif winningNumber == 4 then
  365.     if state.four ~= 0 then p = p + (12 * state.four) end
  366.     if state.black ~= 0 then p = p + (2 * state.black) end
  367.     if state.onetosix ~= 0 then p = p + (3 * state.onetosix) end
  368.     if state.fourtonine ~= 0 then p = p + (3 * state.fourtonine) end
  369.   elseif winningNumber == 5 then
  370.     if state.five ~= 0 then p = p + (12 * state.five) end
  371.     if state.red ~= 0 then p = p + (2 * state.red) end
  372.     if state.onetosix ~= 0 then p = p + (3 * state.onetosix) end
  373.     if state.fourtonine ~= 0 then p = p + (3 * state.fourtonine) end
  374.   elseif winningNumber == 6 then
  375.     if state.six ~= 0 then p = p + (12 * state.six) end
  376.     if state.black ~= 0 then p = p + (2 * state.black) end
  377.     if state.onetosix ~= 0 then p = p + (3 * state.onetosix) end
  378.     if state.fourtonine ~= 0 then p = p + (3 * state.fourtonine) end
  379.   elseif winningNumber == 7 then
  380.     if state.seven ~= 0 then p = p + (12 * state.seven) end
  381.     if state.red ~= 0 then p = p + (2 * state.red) end
  382.     if state.fourtonine ~= 0 then p = p + (3 * state.fourtonine) end
  383.     if state.seventotwelve ~= 0 then p = p + (3 * state.seventotwelve) end
  384.   elseif winningNumber == 8 then
  385.     if state.eight ~= 0 then p = p + (12 * state.eight) end
  386.     if state.black ~= 0 then p = p + (2 * state.black) end
  387.     if state.fourtonine ~= 0 then p = p + (3 * state.fourtonine) end
  388.     if state.seventotwelve ~= 0 then p = p + (3 * state.seventotwelve) end
  389.   elseif winningNumber == 9 then
  390.     if state.nine ~= 0 then p = p + (12 * state.nine) end
  391.     if state.red ~= 0 then p = p + (2 * state.red) end
  392.     if state.fourtonine ~= 0 then p = p + (3 * state.fourtonine) end
  393.     if state.seventotwelve ~= 0 then p = p + (3 * state.seventotwelve) end
  394.   elseif winningNumber == 10 then
  395.     if state.ten ~= 0 then p = p + (12 * state.ten) end
  396.     if state.black ~= 0 then p = p + (2 * state.black) end
  397.     if state.seventotwelve ~= 0 then p = p + (3 * state.seventotwelve) end
  398.   elseif winningNumber == 11 then
  399.     if state.eleven ~= 0 then p = p + (12 * state.eleven) end
  400.     if state.red ~= 0 then p = p + (2 * state.red) end
  401.     if state.seventotwelve ~= 0 then p = p + (3 * state.seventotwelve) end
  402.   elseif winningNumber == 12 then
  403.     if state.twelve ~= 0 then p = p + (12 * state.twelve) end
  404.     if state.black ~= 0 then p = p + (2 * state.black) end
  405.     if state.seventotwelve ~= 0 then p = p + (3 * state.seventotwelve) end
  406.   end
  407.   balance = balance + p
  408. end
  409.  
  410. function checkRedstoneSignalOn()
  411.   while not redstone.getInput("back") do sleep(0.1) end
  412. end
  413.  
  414. function checkRedstoneSignalOff()
  415.   while redstone.getInput("back") do sleep(0.1) end
  416. end
  417.  
  418. function main()
  419.   init()
  420.  
  421.   while true do
  422.     if loggedIn then
  423.       renderBoard()
  424.  
  425.       if not redstone.getInput("back") or winningNumber ~= nil then
  426.         if winningNumber ~= nil then
  427.           old.write(winningNumber)
  428.           calculateWinnings()
  429.           winningNumber = nil
  430.           state = {
  431.             red     = 0,
  432.             black   = 0,
  433.            
  434.             onetosix      = 0,
  435.             fourtonine    = 0,
  436.             seventotwelve = 0,
  437.          
  438.             one     = 0,
  439.             two     = 0,
  440.             three   = 0,
  441.             four    = 0,
  442.             five    = 0,
  443.             six     = 0,
  444.             seven   = 0,
  445.             eight   = 0,
  446.             nine    = 0,
  447.             ten     = 0,
  448.             eleven  = 0,
  449.             twelve  = 0,
  450.             zero    = 0
  451.           }
  452.           checkRedstoneSignalOff()
  453.           renderBoard()
  454.         end
  455.         parallel.waitForAny(clickHandler, checkRedstoneSignalOn)
  456.       else
  457.         local i,m,p = rednet.receive()
  458.         winningNumber = tonumber(m)
  459.       end
  460.     else
  461.       term.setBackgroundColor(colors.green)
  462.       term.clear()
  463.  
  464.       if disk.isPresent("front") then
  465.         local n = fs.open("/disk/n.dat", "r")
  466.         local b = fs.open("/disk/b.dat", "r")
  467.  
  468.         if n and b then
  469.           name    = n.readLine()
  470.           balance = math.floor(tonumber(b.readLine()))
  471.         else
  472.           print("ERROR")
  473.         end
  474.  
  475.         n.close()
  476.         b.close()
  477.  
  478.         loggedIn = true
  479.         sleep(2)
  480.       end
  481.     end
  482.  
  483.     sleep(0.1)
  484.   end
  485. end
  486.  
  487. main()
Advertisement
Add Comment
Please, Sign In to add comment