DrFair

Storage UI

Jan 27th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. rednet.open("back")
  2. items = {}
  3. amount = {}
  4. order = {}
  5. sx,sy = term.getSize()
  6. input = ""
  7. selection = 1
  8. state = "search"
  9. 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 }
  10. alpha = "abcdefghijklmnopqrstuvwxyz"
  11. timerid = os.startTimer(30)
  12.  
  13. function swrite(str,x,y,color)
  14. term.setCursorPos(x,y)
  15. term.setTextColor(col[color])
  16. term.write(str)
  17. end
  18.  
  19. function swritecenter(str,y,color)
  20. term.setCursorPos((sx/2-#str/2)+1,y)
  21. term.setTextColor(col[color])
  22. term.write(str)
  23. end
  24.  
  25. function swriteline(str,y,color)
  26. local sstr = str
  27. while #sstr < sx do
  28. sstr = sstr..str
  29. end
  30. term.setCursorPos(1,y)
  31. term.setTextColor(col[color])
  32. term.write(sstr)
  33. end
  34.  
  35. function swriterow(str,x,y1,y2,color)
  36. local n = y2-y1
  37. for i=1,n do
  38. term.setCursorPos(x,y1+i-1)
  39. term.setTextColor(col[color])
  40. term.write(str)
  41. end
  42. end
  43.  
  44. function search(str)
  45. results = {}
  46. local sstr = string.lower(str)
  47. for i=1,#items do
  48. if string.find(string.lower(items[i]),sstr) then
  49. table.insert(results,items[i])
  50. end
  51. end
  52. if selection > #results then
  53. selection = #results
  54. end
  55. for i=1,15 do
  56. swrite(" ",32,4+i,"white")
  57. end
  58. for i=1,15 do
  59. swrite(" ",31,4+i,"red")
  60. end
  61. if #results > 0 then
  62. for i=1,#results do
  63. if selection == i then
  64. swrite(">",31,4+i,"red")
  65. end
  66. if #results[i] > 13 then
  67. swrite(string.sub(results[i],1,11).."..",32,4+i,"white")
  68. else
  69. swrite(results[i],32,4+i,"white")
  70. end
  71. swrite("("..amount[results[i]]..")",46,4+i,"white")
  72. end
  73. else
  74. swrite("Sorry, no results.",32,5,"white")
  75. selection = 1
  76. end
  77. term.setCursorPos(2+#input,4)
  78. end
  79.  
  80. function checkfor(str)
  81. exist = false
  82. for i=1,#items do
  83. if items[i] == str then
  84. exist = true
  85. end
  86. end
  87. if not exist then
  88. table.insert(items,str)
  89. end
  90. end
  91.  
  92. function update(str)
  93. if state == "search" then
  94. if #str > 0 then
  95. search(str)
  96. else
  97. search("Made by Fair")
  98. end
  99. end
  100. term.setCursorPos(2,4)
  101. term.setTextColor(1)
  102. term.write(" ")
  103. term.setCursorPos(2,4)
  104. term.write(str)
  105. end
  106.  
  107. function lastorder(str)
  108. term.setCursorPos(2,7)
  109. term.setTextColor(1)
  110. term.write(" ")
  111. term.setCursorPos(2,7)
  112. term.write(str)
  113. end
  114.  
  115. function printui()
  116. term.clear()
  117. swritecenter("Welcome to Fair's storage system",1,"blue")
  118. swriteline("-",2,"lightblue")
  119. swrite("Search for item:",2,3,"white")
  120. swriterow("|",30,3,sy+1,"lightblue")
  121. swrite("Search results:",32,3,"green")
  122. swrite("Last order/status:",2,6,"yellow")
  123. lastorder("None.")
  124. swrite("Usage:",2,9,"yellow")
  125. swrite("1. Search for an item.",2,10,"white")
  126. swrite("2. Press enter and type",2,11,"white")
  127. swrite(" the amount of the item",2,12,"white")
  128. swrite(" you want.",2,13,"white")
  129. swrite("3. Items will be in the",2,14,"white")
  130. swrite(" enderchest to the left.",2,15,"white")
  131. swrite("4. Unsorted items will be",2,16,"white")
  132. swrite(" in the chests.",2,17,"white")
  133. swrite("Entirely coded by Fair.",1,sy,"white")
  134. term.setTextColor(1)
  135. term.setCursorPos(2,4)
  136. term.setCursorBlink(true)
  137. end
  138.  
  139. printui()
  140. rednet.broadcast("updatestock")
  141.  
  142. while true do
  143. event,id,message = os.pullEvent()
  144. if event == "timer" and id == timerid then
  145. rednet.broadcast("updatestock")
  146. timerid = os.startTimer(10)
  147. elseif event == "rednet_message" then
  148. if string.sub(message,1,1) == "{" then
  149. item = textutils.unserialize(message)
  150. if item[1]=="item" then
  151. checkfor(item[2])
  152. amount[item[2]] = item[3]
  153. end
  154. end
  155. elseif event == "char" and #input < 27 then
  156. if state == "search" then
  157. input = input..id
  158. update(input)
  159. elseif state == "amount" then
  160. 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
  161. input = input..id
  162. update(input)
  163. end
  164. end
  165. elseif event == "key" then
  166. if id == 14 then
  167. input = string.sub(input,1,#input-1)
  168. update(input)
  169. elseif id == 28 then
  170. if state == "search" then
  171. if #results > 0 then
  172. swrite("Type amount needed:",2,3,"white")
  173. state = "amount"
  174. else
  175. lastorder("No such item.")
  176. end
  177. elseif state == "amount" then
  178. if tonumber(input) <= amount[results[selection]] and tonumber(input) <= 500 then
  179. if tonumber(input) > 0 then
  180. order = { [1]="order"..results[selection], [2]=tonumber(input) }
  181. rednet.broadcast(textutils.serialize(order))
  182. lastorder(input.." of "..results[selection])
  183. amount[results[selection]] = amount[results[selection]] - input
  184. else
  185. lastorder("Cannot order 0 items.")
  186. end
  187. else
  188. lastorder("Too many items ordered.")
  189. end
  190. swrite("Search for item: ",2,3,"white")
  191. state = "search"
  192. selection = 1
  193. end
  194. input = ""
  195. update(input)
  196. elseif id == 200 then
  197. if selection == 1 then
  198. if #results > 15 then
  199. selection = 15
  200. else
  201. selection = #results
  202. end
  203. else
  204. selection = selection - 1
  205. end
  206. search(input)
  207. elseif id == 208 then
  208. if selection == #results or selection == 15 then
  209. selection = 1
  210. else
  211. selection = selection + 1
  212. end
  213. search(input)
  214. end
  215. end
  216. end
Advertisement
Add Comment
Please, Sign In to add comment