imposiblaa

funyunCC

Feb 26th, 2021 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.79 KB | None | 0 0
  1. print("downloading surface...")
  2. shell.run("pastebin", "get", "UxweEuqf", "surface.lua")
  3. shell.run("pastebin", "get", "k7hXdpy3", "assets.lua")
  4.  
  5. local surface = dofile('surface.lua')
  6. local assets = dofile('assets.lua')
  7.  
  8. local monitor = peripheral.find("monitor")
  9. local modem = peripheral.find("modem")
  10.  
  11. local w, h = monitor.getSize()
  12. local currentScreen = "title"
  13. local screen = surface.create(50, 19)
  14. local shopStock = { }
  15. local usrInput = ""
  16. local fontSurface = surface.load(assets.font.picture, true)
  17. local font = surface.loadFont(fontSurface)
  18. local selectedNumber = 0
  19. local selectedItem = {}
  20. local inputMode = "search"
  21. local selectedAmount = "no quanity entered"
  22. local buyButton = surface.load(assets.buy.picture, true)
  23. local quantityMode = "item"
  24.  
  25. modem.open(47603)
  26.  
  27. function startsWith(str, start)
  28.     return str:sub(1, #start) == start
  29. end
  30.  
  31. function centerText(text)
  32.     return (math.floor((w - string.len(text)) / 2))
  33. end
  34.  
  35. function textBox(title, content, height, titleColor, contentColor, bgcolor, notCentered, x)
  36.     content = tostring(content)
  37.     title = tostring(title)
  38.     screen:drawLine(0, height, 50, height, colors.gray, colors.white)
  39.     screen:drawLine(0, height + 1, 50, height + 1, colors.gray, colors.yellow)
  40.     if(#title>=#content)then
  41.         fixedTitle = " " .. title .. " "
  42.         screen:drawString(fixedTitle, centerText(fixedTitle), height, bgcolor, titleColor)
  43.         screen:drawString(tostring(content), centerText(tostring(content)), height+1)
  44.     else
  45.         fixedContent = " " .. content .. " "
  46.         screen:drawString(title, centerText(title), height, bgcolor, titleColor)
  47.         screen:drawString(tostring(fixedContent), centerText(tostring(fixedContent)), height+1, bgcolor, contentColor)
  48.     end
  49. end
  50.  
  51. function stringSplit (inputstr, sep)
  52.     if sep == nil then
  53.             sep = "%s"
  54.     end
  55.     local t={}
  56.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  57.             table.insert(t, str)
  58.     end
  59.     return t
  60. end
  61.  
  62.  
  63. --draws the selected screen to the monitor
  64. function drawScreen()
  65.     inputMode = "search"
  66.     if(currentScreen == "title")then
  67.         screen:clear()
  68.         titleSurface = surface.load(assets.title.picture, true)
  69.         screen:drawSurfaceSmall(titleSurface, 0, 0)
  70.     elseif(currentScreen == "buy")then
  71.         inputMode = "search"
  72.         screen:clear()
  73.         screen:drawString("Showing results for: " .. usrInput, 0, 0)
  74.         for i = 1, 17,1 do
  75.             if(i%2 == 0)then
  76.                 screen:drawLine(0, i, 23, i, colors.gray, colors.white)
  77.                 screen:drawLine(26, i, 50, i, colors.gray, colors.white)
  78.             else
  79.                 screen:drawLine(0, i, 23, i, colors.lightGray, colors.black)
  80.                 screen:drawLine(26, i, 50, i, colors.lightGray, colors.black)
  81.             end
  82.         end
  83.         local itemNumber = 0
  84.         for number, item in pairs(searchedShopStock) do
  85.             if(itemNumber>31)then
  86.                 break
  87.             end
  88.             if(itemNumber%2 == 0)then
  89.                 screen:drawString(item.name, 0, ((itemNumber/2)+1))
  90.             else
  91.                 screen:drawString(item.name, 26, (((itemNumber-1)/2)+1))
  92.             end
  93.             itemNumber = itemNumber + 1
  94.         end
  95.         screen:drawString("O", 0, 18, colors.black, colors.yellow)
  96.         screen:drawString("Funyun", 1, 18, colors.black, colors.white)
  97.         screen:drawString(" Search --> ", 39, 18, colors.black, colors.yellow)
  98.         screen:drawString(" home ", 22, 18, colors.gray, colors.yellow)
  99.     elseif(currentScreen == "page")then
  100.         inputMode = "quantity"
  101.         screen:clear()
  102.         if(tonumber(selectedAmount) ~= nil)then
  103.             totalPrice = (selectedAmount / searchedShopStock[selectedNumber].info.amount) * searchedShopStock[selectedNumber].info.price
  104.             if(totalPrice % 1 ~= 0)then
  105.                 totalPrice = totalPrice + 1 - (totalPrice % 1)
  106.             end
  107.             displayAmount = " " .. selectedAmount .. " item(s) |"
  108.             displayStacks = "| ".. selectedAmount/64 .. " stack(s) "
  109.             totalPrice = totalPrice .. " diamond(s)"
  110.             screen:drawLine(0, 5, 50, 5, colors.gray, colors.white)
  111.             screen:drawLine(0, 6, 50, 6, colors.gray, colors.white)
  112.             screen:drawString(displayAmount, 25 - #displayAmount, 6, colors.gray, colors.yellow)
  113.             screen:drawString(displayStacks, 25, 6, colors.gray, colors.yellow)
  114.             screen:drawString("Quantity", centerText("Quantity"), 5)
  115.         else
  116.             displayAmount = selectedAmount
  117.             totalPrice = "input quantity"
  118.             textBox("Quantity", displayAmount, 5, colors.white, colors.yellow, colors.gray)
  119.         end
  120.         textBox("Price per " .. searchedShopStock[selectedNumber].info.amount .. " items", searchedShopStock[selectedNumber].info.price .. " diamond(s)", 2, colors.white, colors.yellow, colors.gray)
  121.         textBox("Total Price", totalPrice, 8, colors.white, colors.yellow, colors.gray)
  122.         screen:drawSurfaceSmall(buyButton, 19, 12)
  123.         screen:drawLine(0,0,50,0, colors.gray, colors.white)
  124.         screen:drawString(selectedItem, centerText(selectedItem), 0)
  125.         screen:drawString("O", 0, 18, colors.black, colors.yellow)
  126.         screen:drawString("Funyun", 1, 18, colors.black, colors.white)
  127.         screen:drawString(" back ", 22, 18, colors.gray, colors.yellow)
  128.         screen:drawString(" Quantity --> ", 37, 18, colors.black, colors.yellow)
  129.         screen:drawRect(0, 12, 12, 4, colors.yellow, colors.black)
  130.         screen:drawString(" item mode", 0, 12)
  131.         if(quantityMode == "item")then
  132.             screen:drawString("  Items   ", 1, 13, colors.lightGray, colors.black)
  133.             screen:drawString("  Stacks  ", 1, 14, colors.gray, colors.white)
  134.         else
  135.             screen:drawString("  Items   ", 1, 13, colors.gray, colors.white)
  136.             screen:drawString("  Stacks  ", 1, 14, colors.lightGray, colors.black)
  137.         end
  138.     end
  139.     screen:output(monitor)
  140. end
  141.  
  142. --checks for touchscreen button presses
  143. function checkButtons()
  144.     os.sleep(0.05)
  145.     local event, side, clickx, clicky = os.pullEvent("monitor_touch")
  146.     if(currentScreen == "title")then
  147.         for button, coords in pairs(assets.title.buttons) do
  148.             if(coords[1]<=clickx and clickx<=coords[2] and coords[3]<=clicky and clicky<=coords[4])then
  149.                 if(button == "buy")then
  150.                     getShop()
  151.                     searchedShopStock = {}
  152.                     for item, details in pairs(shopStock) do
  153.                         table.insert(searchedShopStock, {name = item, info = details})
  154.                     end
  155.                 end
  156.                 currentScreen = button
  157.             end
  158.         end
  159.     elseif(currentScreen == "buy")then
  160.         if(23<=clickx and clickx<=28 and clicky == 19)then
  161.             currentScreen = "title"
  162.         elseif(0<clicky and clicky<18 and clickx ~= 25 and clickx ~=26 and clicky>1 and clicky<19)then
  163.             if(clickx<25)then
  164.                 selectedNumber = ((clicky-1)*2)-1
  165.                 if(selectedNumber <= #searchedShopStock)then
  166.                     selectedItem = searchedShopStock[selectedNumber].name
  167.                     selectedAmount = "no quanity entered"
  168.                     currentScreen = "page"
  169.                 end
  170.             elseif(clickx>26)then
  171.                 selectedNumber = ((clicky-1)*2)
  172.                 if(selectedNumber <= #searchedShopStock)then
  173.                     selectedItem = searchedShopStock[selectedNumber].name
  174.                     selectedAmount = "no quanity entered"
  175.                     currentScreen = "page"
  176.                 end
  177.             end
  178.         end
  179.     elseif(currentScreen == "page")then
  180.         if(23<=clickx and clickx<=28 and clicky == 19)then
  181.             getShop()
  182.             searchedShopStock = {}
  183.             for item, details in pairs(shopStock) do
  184.                 table.insert(searchedShopStock, {name = item, info = details})
  185.             end
  186.             usrInput = ""
  187.             currentScreen = "buy"
  188.         elseif(20<=clickx and clickx<=31 and 13<=clicky and clicky <=16)then
  189.             getShop()
  190.             searchedShopStock = {}
  191.             for item, details in pairs(shopStock) do
  192.                 table.insert(searchedShopStock, {name = item, info = details})
  193.             end
  194.             usrInput = ""
  195.             currentScreen = "buy"
  196.         elseif(2<=clickx and clickx<=11 and 14<=clicky and clicky<=15)then
  197.             if(clicky == 14)then
  198.                 quantityMode = "item"
  199.             else
  200.                 quantityMode = "stack"
  201.             end
  202.         end
  203.     end
  204. end
  205.  
  206.  
  207. --gets list of items, their price, and how many are in stock
  208. function getShop()
  209.     modem.transmit(47603, 47603, "getShop")
  210.     local event, side, channel, replaychannel, message = os.pullEvent("modem_message")
  211.     shopStock = message
  212. end
  213.  
  214.  
  215. function tick()
  216.     while true do
  217.         os.sleep(0.2)
  218.         drawScreen()
  219.     end
  220. end
  221.  
  222. function input()
  223.     term.clear()
  224.     term.setCursorPos(17, 9)
  225.     print('input> ')
  226.     term.setCursorPos(25, 9)
  227.     usrInput = read()
  228.     if(inputMode == "search")then      
  229.         searchedShopStock = {}
  230.         for item, details in pairs(shopStock) do
  231.             if(startsWith(item, usrInput))then
  232.                 table.insert(searchedShopStock, {name = item, info = details})
  233.             end
  234.         end
  235.     elseif(inputMode == "quantity")then
  236.         if(tonumber(usrInput) ~= nil)then
  237.             if(quantityMode == "item")then
  238.                 selectedAmount = usrInput
  239.             else
  240.                 selectedAmount = usrInput * 64
  241.             end
  242.         else
  243.             selectedAmount = "no quanity entered"
  244.         end
  245.     end
  246. end
  247.  
  248. while true do
  249.     os.sleep(0.05)
  250.     parallel.waitForAny(checkButtons, tick, input)
  251. end
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
Advertisement
Add Comment
Please, Sign In to add comment