Jayex_Designs

atmDisplay

Jun 20th, 2021 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.27 KB | None | 0 0
  1. args = {...}
  2. if not args[1] or not args[2] then
  3.     error("Set the id of the server and the receiver")
  4. end
  5. ServerId = tonumber(args[1])
  6. ReceiverId = tonumber(args[2])
  7.  
  8. rednet.open("bottom")
  9.  
  10. Display = peripheral.wrap("top")
  11.  
  12. DisplayWidth, DisplayHeight = Display.getSize()
  13.  
  14.  
  15. CurrentAccount = nil
  16.  
  17.  
  18.  
  19. function DrawButton(xPos, yPos, text, color, bgColor)
  20.     Display.setCursorPos(xPos, yPos)
  21.     local row = "\x81" .. string.rep("\x80", DisplayWidth-4) .. "\x82"
  22.     Display.blit(row, bgColor .. string.rep(bgColor, string.len(row)-2) .. bgColor, color .. string.rep(color, string.len(row)-2) .. color)
  23.     Display.setCursorPos(xPos, yPos+1)
  24.     Display.blit(string.rep("\x80", DisplayWidth-2), string.rep(bgColor, DisplayWidth-2), string.rep(color, DisplayWidth-2))
  25.     Display.setCursorPos(xPos, yPos+2)
  26.     row = "\x90" .. string.rep("\x80", DisplayWidth-4) .. "\x9f"
  27.     Display.blit(row, bgColor .. string.rep(bgColor, string.len(row)-2) .. color, color .. string.rep(color, string.len(row)-2) .. bgColor)
  28.     Display.setCursorPos((DisplayWidth - string.len(text))/2+1, yPos+1)
  29.     Display.blit(text, string.rep(bgColor, string.len(text)), string.rep(color, string.len(text)))
  30.     return xPos, yPos+2
  31. end
  32.  
  33. function DrawKeyboard()
  34.     local text = ""
  35.     Display.setCursorPos(1, DisplayHeight-8)
  36.     Display.blit(string.rep("-", DisplayWidth), string.rep("0", DisplayWidth), string.rep("f", DisplayWidth))
  37.     Display.setCursorPos(1, DisplayHeight-7)
  38.     text = "q w e r t y u i o p"
  39.     text = string.rep(" ", math.floor((DisplayWidth - string.len(text))/2)) .. text
  40.     Display.blit(text, string.rep("0", string.len(text)), string.rep("f", string.len(text)))
  41.  
  42.     Display.setCursorPos(1, DisplayHeight-5)
  43.     text = "a s d f g h j k l"
  44.     text = string.rep(" ", math.floor((DisplayWidth - string.len(text))/2)) .. text
  45.     Display.blit(text, string.rep("0", string.len(text)), string.rep("f", string.len(text)))
  46.  
  47.     Display.setCursorPos(1, DisplayHeight-3)
  48.     text = "z x c v b n m"
  49.     text = string.rep(" ", math.floor((DisplayWidth - string.len(text))/2)) .. text
  50.     Display.blit(text, string.rep("0", string.len(text)), string.rep("f", string.len(text)))
  51.  
  52.     Display.setCursorPos(1, DisplayHeight-1)
  53.     text = "back up down send"
  54.     text = string.rep(" ", math.floor((DisplayWidth - string.len(text))/2)) .. text
  55.     Display.blit(text, string.rep("0", string.len(text)), string.rep("f", string.len(text)))
  56. end
  57.  
  58. function GetKeyboardPress()
  59.     local _, _, x, y = os.pullEvent("monitor_touch")
  60.  
  61.     local keys = {
  62.         ["q"] = {
  63.             x = 6,
  64.             y = 12
  65.         },
  66.         ["w"] = {
  67.             x = 8,
  68.             y = 12
  69.         },
  70.         ["e"] = {
  71.             x = 10,
  72.             y = 12
  73.         },
  74.         ["r"] = {
  75.             x = 12,
  76.             y = 12
  77.         },
  78.         ["t"] = {
  79.             x = 14,
  80.             y = 12
  81.         },
  82.         ["y"] = {
  83.             x = 16,
  84.             y = 12
  85.         },
  86.         ["u"] = {
  87.             x = 18,
  88.             y = 12
  89.         },
  90.         ["i"] = {
  91.             x = 20,
  92.             y = 12
  93.         },
  94.         ["o"] = {
  95.             x = 22,
  96.             y = 12
  97.         },
  98.         ["p"] = {
  99.             x = 24,
  100.             y = 12
  101.         },
  102.         ["a"] = {
  103.             x = 7,
  104.             y = 14
  105.         },
  106.         ["s"] = {
  107.             x = 9,
  108.             y = 14
  109.         },
  110.         ["d"] = {
  111.             x = 11,
  112.             y = 14
  113.         },
  114.         ["f"] = {
  115.             x = 13,
  116.             y = 14
  117.         },
  118.         ["g"] = {
  119.             x = 15,
  120.             y = 14
  121.         },
  122.         ["h"] = {
  123.             x = 17,
  124.             y = 14
  125.         },
  126.         ["j"] = {
  127.             x = 19,
  128.             y = 14
  129.         },
  130.         ["k"] = {
  131.             x = 21,
  132.             y = 14
  133.         },
  134.         ["l"] = {
  135.             x = 23,
  136.             y = 14
  137.         },
  138.         ["z"] = {
  139.             x = 9,
  140.             y = 16
  141.         },
  142.         ["x"] = {
  143.             x = 11,
  144.             y = 16
  145.         },
  146.         ["c"] = {
  147.             x = 13,
  148.             y = 16
  149.         },
  150.         ["v"] = {
  151.             x = 15,
  152.             y = 16
  153.         },
  154.         ["b"] = {
  155.             x = 17,
  156.             y = 16
  157.         },
  158.         ["n"] = {
  159.             x = 19,
  160.             y = 16
  161.         },
  162.         ["m"] = {
  163.             x = 21,
  164.             y = 16
  165.         },
  166.         ["back"] = {
  167.             x1 = 7,
  168.             x2 = 10,
  169.             y = 18
  170.         },
  171.         ["up"] = {
  172.             x1 = 12,
  173.             x2 = 13,
  174.             y = 18
  175.         },
  176.         ["down"] = {
  177.             x1 = 15,
  178.             x2 = 18,
  179.             y = 18
  180.         },
  181.         ["send"] = {
  182.             x1 = 20,
  183.             x2 = 23,
  184.             y = 18
  185.         }
  186.     }
  187.  
  188.     for k, v in pairs(keys) do
  189.         if type(v["x"]) == "number" and v["x"] == x and v["y"] == y then
  190.             return k
  191.         elseif type(v["x"]) == "nil" and v["x1"] <= x and v["x2"] >= x and v["y"] == y then
  192.             return k
  193.         end
  194.     end
  195.     return nil, x, y
  196. end
  197.  
  198.  
  199.  
  200. function PrintMainScreen()
  201.     Display.setCursorBlink(false)
  202.     Display.setTextScale(1)
  203.     Display.clear()
  204.  
  205.     Display.setCursorPos(2, 2)
  206.     local text = "Add Potato Coins"
  207.     Display.blit(text, string.rep("0", string.len(text)), string.rep("f", string.len(text)))
  208.     Display.setCursorPos(1, 3)
  209.     Display.blit(string.rep("-", DisplayWidth), string.rep("0", DisplayWidth), string.rep("f", DisplayWidth))
  210.  
  211.     local xPos, yPos = DrawButton(2, 7, "Register", "4", "f")
  212.     xPos, yPos = DrawButton(xPos, yPos+2, "Login", "0", "f")
  213.  
  214.     Display.setCursorPos(xPos, DisplayHeight)
  215.     text = "No refunds"
  216.     Display.blit(text, string.rep("7", string.len(text)), string.rep("f", string.len(text)))
  217.  
  218.     while true do
  219.         Display.setCursorBlink(false)
  220.         local _, _, x, y = os.pullEvent("monitor_touch")
  221.         if x >= 2 and x <= 28 and y >= 7 and y <= 9 then
  222.             local username, password = PrintFormScreen()
  223.             if username == nil then
  224.                 break
  225.             end
  226.             rednet.send(ServerId, username .. " " .. password, "registerRequest")
  227.             local id, message, protocol = rednet.receive()
  228.             if message and protocol == "registerRequest" then
  229.                 PrintLoggedScreen(username)
  230.                 break
  231.             elseif not message and protocol == "registerRequest" then
  232.                 break
  233.             end
  234.  
  235.         elseif x >= 2 and x <= 28 and y >= 11 and y <= 13 then
  236.             local username, password = PrintFormScreen()
  237.             if username == nil then
  238.                 break
  239.             end
  240.             rednet.send(ServerId, username .. " " .. password, "loginRequest")
  241.             local id, message, protocol = rednet.receive()
  242.             if message and protocol == "loginRequest" then
  243.                 PrintLoggedScreen(username)
  244.                 break
  245.             elseif not message and protocol == "loginRequest" then
  246.                 break
  247.             end
  248.         end
  249.     end
  250. end
  251.  
  252. function PrintFormScreen()
  253.     Display.clear()
  254.  
  255.     DrawKeyboard()
  256.  
  257.     local text = "Return"
  258.     Display.setCursorPos(DisplayWidth - string.len(text), 1)
  259.     Display.blit(text, string.rep("4", string.len(text)), string.rep("f", string.len(text)))
  260.  
  261.     Display.setCursorPos(2, 3)
  262.     Display.blit(string.rep("\x80", DisplayWidth-2), string.rep("f", DisplayWidth-2), string.rep("0", DisplayWidth-2))
  263.     Display.setCursorPos(2, 5)
  264.     Display.blit(string.rep("\x80", DisplayWidth-2), string.rep("f", DisplayWidth-2), string.rep("0", DisplayWidth-2))
  265.  
  266.     local username = ""
  267.     local password = ""
  268.  
  269.     Display.setCursorBlink(true)
  270.     Display.setCursorPos(2, 3)
  271.  
  272.     while true do
  273.         local key, x, y = GetKeyboardPress()
  274.         if key == nil then
  275.             if x >= DisplayWidth - string.len(text) and x <= DisplayWidth and y == 1 then
  276.                 return nil
  277.             end
  278.         elseif string.len(key) == 1 then
  279.             local _, cy = Display.getCursorPos()
  280.             if cy == 3 and string.len(username) < DisplayWidth-2 then
  281.                 username = username .. key
  282.             elseif cy == 5 and string.len(password) < DisplayWidth-2 then
  283.                 password = password .. key
  284.             end
  285.         elseif key == "back" then
  286.             local _, cy = Display.getCursorPos()
  287.             if cy == 3 then
  288.                 username = string.sub(username, 1, string.len(username)-1)
  289.             elseif cy == 5 then
  290.                 password = string.sub(password, 1, string.len(password)-1)
  291.             end
  292.         elseif key == "up" then
  293.             Display.setCursorPos(2, 3)
  294.         elseif key == "down" then
  295.             Display.setCursorPos(2, 5)
  296.         elseif key == "send" then
  297.             return username, password
  298.         end
  299.  
  300.         local _, cy = Display.getCursorPos()
  301.         Display.setCursorPos(2, 3)
  302.         Display.blit(
  303.             username .. string.rep("\x80", DisplayWidth-2-string.len(username)),
  304.             string.rep("0", string.len(username)) .. string.rep("f", DisplayWidth-2-string.len(username)),
  305.             string.rep("f", string.len(username)) .. string.rep("0", DisplayWidth-2-string.len(username))
  306.         )
  307.         Display.setCursorPos(2, 5)
  308.         Display.blit(
  309.             string.rep("*", string.len(password)) .. string.rep("\x80", DisplayWidth-2-string.len(password)),
  310.             string.rep("0", string.len(password)) .. string.rep("f", DisplayWidth-2-string.len(password)),
  311.             string.rep("f", string.len(password)) .. string.rep("0", DisplayWidth-2-string.len(password))
  312.         )
  313.         if cy == 3 then
  314.             Display.setCursorPos(math.max(string.len(username)+1, 2), cy)
  315.         elseif cy == 5 then
  316.             Display.setCursorPos(math.max(string.len(password)+1, 2), cy)
  317.         end
  318.     end
  319. end
  320.  
  321. function PrintLoggedScreen(username, balance)
  322.     if balance == nil then
  323.         balance = 0
  324.         rednet.send(ServerId, username, "getBalanceRequest")
  325.         local id, message, protocol = rednet.receive()
  326.         if protocol == "getBalanceRequest" then
  327.             balance = message
  328.         end
  329.     end
  330.  
  331.     rednet.send(ReceiverId, true, "userLogged")
  332.  
  333.     Display.setCursorBlink(false)
  334.     Display.clear()
  335.  
  336.     Display.setCursorPos(2, 2)
  337.     local text = "Logged in as"
  338.     Display.blit(text, string.rep("0", string.len(text)), string.rep("f", string.len(text)))
  339.     Display.setCursorPos(2,3)
  340.     Display.blit(username, string.rep("0", string.len(username)), string.rep("f", string.len(username)))
  341.     Display.setCursorPos(1,4)
  342.     Display.blit(string.rep("-", DisplayWidth), string.rep("0", DisplayWidth), string.rep("f", DisplayWidth))
  343.  
  344.     text = "You have " .. tostring(balance) .. " potato coins"
  345.     Display.setCursorPos(math.floor((DisplayWidth - string.len(text))/2)+1,7)
  346.     Display.blit(text, string.rep("0", string.len(text)), string.rep("f", string.len(text)))
  347.     text = "Throw materials supported"
  348.     Display.setCursorPos(math.floor((DisplayWidth - string.len(text))/2)+1,9)
  349.     Display.blit(text, string.rep("0", string.len(text)), string.rep("f", string.len(text)))
  350.     text = "in exchange for potato coins"
  351.     Display.setCursorPos(math.floor((DisplayWidth - string.len(text))/2)+1,10)
  352.     Display.blit(text, string.rep("0", string.len(text)), string.rep("f", string.len(text)))
  353.  
  354.     DrawButton(2, 14, "Done", "0", "f")
  355.  
  356.     local ended = false
  357.     local function waitForButtonPress()
  358.         while true do
  359.             local _, _, x, y = os.pullEvent("monitor_touch")
  360.             if x >= 2 and x <= 28 and y >= 14 and y <= 16 then
  361.                 ended = true
  362.                 rednet.send(ReceiverId, false, "userLogged")
  363.                 break
  364.             end
  365.         end
  366.     end
  367.  
  368.     local function waitForBalanceUpdate()
  369.         while true do
  370.             local id, message, protocol = rednet.receive()
  371.             if protocol == "balanceUpdate" then
  372.                 rednet.send(ServerId, username .. " " .. message, "addBalance")
  373.                 balance = balance + message
  374.                 break
  375.             end
  376.         end
  377.     end
  378.  
  379.     parallel.waitForAny(waitForButtonPress, waitForBalanceUpdate)
  380.     if ended == true then
  381.         return
  382.     else
  383.         PrintLoggedScreen(username, balance)
  384.     end
  385. end
  386.  
  387.  
  388.  
  389. while true do
  390.     PrintMainScreen()
  391. end
Advertisement
Add Comment
Please, Sign In to add comment