Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("downloading surface...")
- shell.run("pastebin", "get", "UxweEuqf", "surface.lua")
- shell.run("pastebin", "get", "k7hXdpy3", "assets.lua")
- local surface = dofile('surface.lua')
- local assets = dofile('assets.lua')
- local monitor = peripheral.find("monitor")
- local modem = peripheral.find("modem")
- local w, h = monitor.getSize()
- local currentScreen = "title"
- local screen = surface.create(50, 19)
- local shopStock = { }
- local usrInput = ""
- local fontSurface = surface.load(assets.font.picture, true)
- local font = surface.loadFont(fontSurface)
- local selectedNumber = 0
- local selectedItem = {}
- local inputMode = "search"
- local selectedAmount = "no quanity entered"
- local buyButton = surface.load(assets.buy.picture, true)
- local quantityMode = "item"
- modem.open(47603)
- function startsWith(str, start)
- return str:sub(1, #start) == start
- end
- function centerText(text)
- return (math.floor((w - string.len(text)) / 2))
- end
- function textBox(title, content, height, titleColor, contentColor, bgcolor, notCentered, x)
- content = tostring(content)
- title = tostring(title)
- screen:drawLine(0, height, 50, height, colors.gray, colors.white)
- screen:drawLine(0, height + 1, 50, height + 1, colors.gray, colors.yellow)
- if(#title>=#content)then
- fixedTitle = " " .. title .. " "
- screen:drawString(fixedTitle, centerText(fixedTitle), height, bgcolor, titleColor)
- screen:drawString(tostring(content), centerText(tostring(content)), height+1)
- else
- fixedContent = " " .. content .. " "
- screen:drawString(title, centerText(title), height, bgcolor, titleColor)
- screen:drawString(tostring(fixedContent), centerText(tostring(fixedContent)), height+1, bgcolor, contentColor)
- end
- end
- function stringSplit (inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- --draws the selected screen to the monitor
- function drawScreen()
- inputMode = "search"
- if(currentScreen == "title")then
- screen:clear()
- titleSurface = surface.load(assets.title.picture, true)
- screen:drawSurfaceSmall(titleSurface, 0, 0)
- elseif(currentScreen == "buy")then
- inputMode = "search"
- screen:clear()
- screen:drawString("Showing results for: " .. usrInput, 0, 0)
- for i = 1, 17,1 do
- if(i%2 == 0)then
- screen:drawLine(0, i, 23, i, colors.gray, colors.white)
- screen:drawLine(26, i, 50, i, colors.gray, colors.white)
- else
- screen:drawLine(0, i, 23, i, colors.lightGray, colors.black)
- screen:drawLine(26, i, 50, i, colors.lightGray, colors.black)
- end
- end
- local itemNumber = 0
- for number, item in pairs(searchedShopStock) do
- if(itemNumber>31)then
- break
- end
- if(itemNumber%2 == 0)then
- screen:drawString(item.name, 0, ((itemNumber/2)+1))
- else
- screen:drawString(item.name, 26, (((itemNumber-1)/2)+1))
- end
- itemNumber = itemNumber + 1
- end
- screen:drawString("O", 0, 18, colors.black, colors.yellow)
- screen:drawString("Funyun", 1, 18, colors.black, colors.white)
- screen:drawString(" Search --> ", 39, 18, colors.black, colors.yellow)
- screen:drawString(" home ", 22, 18, colors.gray, colors.yellow)
- elseif(currentScreen == "page")then
- inputMode = "quantity"
- screen:clear()
- if(tonumber(selectedAmount) ~= nil)then
- totalPrice = (selectedAmount / searchedShopStock[selectedNumber].info.amount) * searchedShopStock[selectedNumber].info.price
- if(totalPrice % 1 ~= 0)then
- totalPrice = totalPrice + 1 - (totalPrice % 1)
- end
- displayAmount = " " .. selectedAmount .. " item(s) |"
- displayStacks = "| ".. selectedAmount/64 .. " stack(s) "
- totalPrice = totalPrice .. " diamond(s)"
- screen:drawLine(0, 5, 50, 5, colors.gray, colors.white)
- screen:drawLine(0, 6, 50, 6, colors.gray, colors.white)
- screen:drawString(displayAmount, 25 - #displayAmount, 6, colors.gray, colors.yellow)
- screen:drawString(displayStacks, 25, 6, colors.gray, colors.yellow)
- screen:drawString("Quantity", centerText("Quantity"), 5)
- else
- displayAmount = selectedAmount
- totalPrice = "input quantity"
- textBox("Quantity", displayAmount, 5, colors.white, colors.yellow, colors.gray)
- end
- textBox("Price per " .. searchedShopStock[selectedNumber].info.amount .. " items", searchedShopStock[selectedNumber].info.price .. " diamond(s)", 2, colors.white, colors.yellow, colors.gray)
- textBox("Total Price", totalPrice, 8, colors.white, colors.yellow, colors.gray)
- screen:drawSurfaceSmall(buyButton, 19, 12)
- screen:drawLine(0,0,50,0, colors.gray, colors.white)
- screen:drawString(selectedItem, centerText(selectedItem), 0)
- screen:drawString("O", 0, 18, colors.black, colors.yellow)
- screen:drawString("Funyun", 1, 18, colors.black, colors.white)
- screen:drawString(" back ", 22, 18, colors.gray, colors.yellow)
- screen:drawString(" Quantity --> ", 37, 18, colors.black, colors.yellow)
- screen:drawRect(0, 12, 12, 4, colors.yellow, colors.black)
- screen:drawString(" item mode", 0, 12)
- if(quantityMode == "item")then
- screen:drawString(" Items ", 1, 13, colors.lightGray, colors.black)
- screen:drawString(" Stacks ", 1, 14, colors.gray, colors.white)
- else
- screen:drawString(" Items ", 1, 13, colors.gray, colors.white)
- screen:drawString(" Stacks ", 1, 14, colors.lightGray, colors.black)
- end
- end
- screen:output(monitor)
- end
- --checks for touchscreen button presses
- function checkButtons()
- os.sleep(0.05)
- local event, side, clickx, clicky = os.pullEvent("monitor_touch")
- if(currentScreen == "title")then
- for button, coords in pairs(assets.title.buttons) do
- if(coords[1]<=clickx and clickx<=coords[2] and coords[3]<=clicky and clicky<=coords[4])then
- if(button == "buy")then
- getShop()
- searchedShopStock = {}
- for item, details in pairs(shopStock) do
- table.insert(searchedShopStock, {name = item, info = details})
- end
- end
- currentScreen = button
- end
- end
- elseif(currentScreen == "buy")then
- if(23<=clickx and clickx<=28 and clicky == 19)then
- currentScreen = "title"
- elseif(0<clicky and clicky<18 and clickx ~= 25 and clickx ~=26 and clicky>1 and clicky<19)then
- if(clickx<25)then
- selectedNumber = ((clicky-1)*2)-1
- if(selectedNumber <= #searchedShopStock)then
- selectedItem = searchedShopStock[selectedNumber].name
- selectedAmount = "no quanity entered"
- currentScreen = "page"
- end
- elseif(clickx>26)then
- selectedNumber = ((clicky-1)*2)
- if(selectedNumber <= #searchedShopStock)then
- selectedItem = searchedShopStock[selectedNumber].name
- selectedAmount = "no quanity entered"
- currentScreen = "page"
- end
- end
- end
- elseif(currentScreen == "page")then
- if(23<=clickx and clickx<=28 and clicky == 19)then
- getShop()
- searchedShopStock = {}
- for item, details in pairs(shopStock) do
- table.insert(searchedShopStock, {name = item, info = details})
- end
- usrInput = ""
- currentScreen = "buy"
- elseif(20<=clickx and clickx<=31 and 13<=clicky and clicky <=16)then
- getShop()
- searchedShopStock = {}
- for item, details in pairs(shopStock) do
- table.insert(searchedShopStock, {name = item, info = details})
- end
- usrInput = ""
- currentScreen = "buy"
- elseif(2<=clickx and clickx<=11 and 14<=clicky and clicky<=15)then
- if(clicky == 14)then
- quantityMode = "item"
- else
- quantityMode = "stack"
- end
- end
- end
- end
- --gets list of items, their price, and how many are in stock
- function getShop()
- modem.transmit(47603, 47603, "getShop")
- local event, side, channel, replaychannel, message = os.pullEvent("modem_message")
- shopStock = message
- end
- function tick()
- while true do
- os.sleep(0.2)
- drawScreen()
- end
- end
- function input()
- term.clear()
- term.setCursorPos(17, 9)
- print('input> ')
- term.setCursorPos(25, 9)
- usrInput = read()
- if(inputMode == "search")then
- searchedShopStock = {}
- for item, details in pairs(shopStock) do
- if(startsWith(item, usrInput))then
- table.insert(searchedShopStock, {name = item, info = details})
- end
- end
- elseif(inputMode == "quantity")then
- if(tonumber(usrInput) ~= nil)then
- if(quantityMode == "item")then
- selectedAmount = usrInput
- else
- selectedAmount = usrInput * 64
- end
- else
- selectedAmount = "no quanity entered"
- end
- end
- end
- while true do
- os.sleep(0.05)
- parallel.waitForAny(checkButtons, tick, input)
- end
Advertisement
Add Comment
Please, Sign In to add comment