Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("back")
- items = {}
- amount = {}
- order = {}
- sx,sy = term.getSize()
- input = ""
- selection = 1
- state = "search"
- col = { ["white"]=1, ["orange"]=2, ["magenta"]=4, ["lightblue"]=8, ["yellow"]=16, ["lime"]=32, ["pink"]=64, ["gray"]=128, ["lightgray"]=256, ["cyan"]=512, ["purple"]=1024, ["blue"]=2048, ["brown"]=4096, ["green"]=8192, ["red"]=16384, ["black"]=32768 }
- alpha = "abcdefghijklmnopqrstuvwxyz"
- timerid = os.startTimer(30)
- function swrite(str,x,y,color)
- term.setCursorPos(x,y)
- term.setTextColor(col[color])
- term.write(str)
- end
- function swritecenter(str,y,color)
- term.setCursorPos((sx/2-#str/2)+1,y)
- term.setTextColor(col[color])
- term.write(str)
- end
- function swriteline(str,y,color)
- local sstr = str
- while #sstr < sx do
- sstr = sstr..str
- end
- term.setCursorPos(1,y)
- term.setTextColor(col[color])
- term.write(sstr)
- end
- function swriterow(str,x,y1,y2,color)
- local n = y2-y1
- for i=1,n do
- term.setCursorPos(x,y1+i-1)
- term.setTextColor(col[color])
- term.write(str)
- end
- end
- function search(str)
- results = {}
- local sstr = string.lower(str)
- for i=1,#items do
- if string.find(string.lower(items[i]),sstr) then
- table.insert(results,items[i])
- end
- end
- if selection > #results then
- selection = #results
- end
- for i=1,15 do
- swrite(" ",32,4+i,"white")
- end
- for i=1,15 do
- swrite(" ",31,4+i,"red")
- end
- if #results > 0 then
- for i=1,#results do
- if selection == i then
- swrite(">",31,4+i,"red")
- end
- if #results[i] > 13 then
- swrite(string.sub(results[i],1,11).."..",32,4+i,"white")
- else
- swrite(results[i],32,4+i,"white")
- end
- swrite("("..amount[results[i]]..")",46,4+i,"white")
- end
- else
- swrite("Sorry, no results.",32,5,"white")
- selection = 1
- end
- term.setCursorPos(2+#input,4)
- end
- function checkfor(str)
- exist = false
- for i=1,#items do
- if items[i] == str then
- exist = true
- end
- end
- if not exist then
- table.insert(items,str)
- end
- end
- function update(str)
- if state == "search" then
- if #str > 0 then
- search(str)
- else
- search("Made by Fair")
- end
- end
- term.setCursorPos(2,4)
- term.setTextColor(1)
- term.write(" ")
- term.setCursorPos(2,4)
- term.write(str)
- end
- function lastorder(str)
- term.setCursorPos(2,7)
- term.setTextColor(1)
- term.write(" ")
- term.setCursorPos(2,7)
- term.write(str)
- end
- function printui()
- term.clear()
- swritecenter("Welcome to Fair's storage system",1,"blue")
- swriteline("-",2,"lightblue")
- swrite("Search for item:",2,3,"white")
- swriterow("|",30,3,sy+1,"lightblue")
- swrite("Search results:",32,3,"green")
- swrite("Last order/status:",2,6,"yellow")
- lastorder("None.")
- swrite("Usage:",2,9,"yellow")
- swrite("1. Search for an item.",2,10,"white")
- swrite("2. Press enter and type",2,11,"white")
- swrite(" the amount of the item",2,12,"white")
- swrite(" you want.",2,13,"white")
- swrite("3. Items will be in the",2,14,"white")
- swrite(" enderchest to the left.",2,15,"white")
- swrite("4. Unsorted items will be",2,16,"white")
- swrite(" in the chests.",2,17,"white")
- swrite("Entirely coded by Fair.",1,sy,"white")
- term.setTextColor(1)
- term.setCursorPos(2,4)
- term.setCursorBlink(true)
- end
- printui()
- rednet.broadcast("updatestock")
- while true do
- event,id,message = os.pullEvent()
- if event == "timer" and id == timerid then
- rednet.broadcast("updatestock")
- timerid = os.startTimer(10)
- elseif event == "rednet_message" then
- if string.sub(message,1,1) == "{" then
- item = textutils.unserialize(message)
- if item[1]=="item" then
- checkfor(item[2])
- amount[item[2]] = item[3]
- end
- end
- elseif event == "char" and #input < 27 then
- if state == "search" then
- input = input..id
- update(input)
- elseif state == "amount" then
- if id=="1" or id=="2" or id=="3" or id=="4" or id=="5" or id=="6" or id=="7" or id=="8" or id=="9" or id=="0" then
- input = input..id
- update(input)
- end
- end
- elseif event == "key" then
- if id == 14 then
- input = string.sub(input,1,#input-1)
- update(input)
- elseif id == 28 then
- if state == "search" then
- if #results > 0 then
- swrite("Type amount needed:",2,3,"white")
- state = "amount"
- else
- lastorder("No such item.")
- end
- elseif state == "amount" then
- if tonumber(input) <= amount[results[selection]] and tonumber(input) <= 500 then
- if tonumber(input) > 0 then
- order = { [1]="order"..results[selection], [2]=tonumber(input) }
- rednet.broadcast(textutils.serialize(order))
- lastorder(input.." of "..results[selection])
- amount[results[selection]] = amount[results[selection]] - input
- else
- lastorder("Cannot order 0 items.")
- end
- else
- lastorder("Too many items ordered.")
- end
- swrite("Search for item: ",2,3,"white")
- state = "search"
- selection = 1
- end
- input = ""
- update(input)
- elseif id == 200 then
- if selection == 1 then
- if #results > 15 then
- selection = 15
- else
- selection = #results
- end
- else
- selection = selection - 1
- end
- search(input)
- elseif id == 208 then
- if selection == #results or selection == 15 then
- selection = 1
- else
- selection = selection + 1
- end
- search(input)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment