CharlyZM

Untitled

Jan 1st, 2023 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. local unicode = require("unicode")
  2. local event = require("event")
  3. local computer = require("computer")
  4. local fs = require("filesystem")
  5. local com = require('component')
  6. --local interface = com.me_interface
  7. local gpu = com.gpu
  8. local choice,run = false,true
  9. local drawFrom = 0
  10. local items,pos_str = {},{}
  11. local patch_items = "/home/items.lua"
  12.  
  13. if not fs.exists(patch_items) then
  14. local f = io.open(patch_items,'w')
  15. f:write("{".."\n")
  16. f:write(" shop = {".."\n")
  17. --local data = interface.getItemsInNetwork()
  18. for item = 1,#data do
  19. if data[item] then
  20. f:write(' { text = "'..data[item].label..'", price = "0", label = "'..data[item].label..'" },'..'\n')
  21. end
  22. end
  23. f:write(" }".."\n")
  24. f:write("}")
  25. f:close()
  26. os.execute("edit "..patch_items)
  27. os.exit()
  28. end
  29.  
  30. local f, err = io.open(patch_items, "r")
  31. if not f then
  32. error(err, 2)
  33. end
  34. local text = f:read('*a')
  35. f:close()
  36. local chunk, err = load("return " .. text, "=items.lua", "t")
  37. if not chunk then
  38. error(err, 2)
  39. else
  40. items = chunk()
  41. end
  42. table.sort(items.shop, function(a,b) if a.text then return a.text < b.text end end)
  43. for i = 1,#items.shop do
  44. --items.shop[i].available = "0"
  45. items.shop[i].available = tostring(math.random(0,10))
  46. end
  47.  
  48. local ind = {}
  49. for i = 1,#items.shop do
  50. if items.shop[i].available ~= "0" then
  51. table.insert(ind,i)
  52. end
  53. end
  54.  
  55. local function square(x,y,width,height,color)
  56. if color and gpu.getBackground() ~= color then
  57. gpu.setBackground(color)
  58. end
  59. gpu.fill(x,y,width,height," ")
  60. end
  61.  
  62. local function drawlist()
  63. pos_str = {}
  64. local yPos = 4
  65. for i = 1,11 do
  66. square(1,yPos,77,1,0x000000)
  67. local i = drawFrom + i
  68. if items.shop[ind[i]] then
  69. gpu.setForeground(0xFFFFFF)
  70. table.insert(pos_str,{yPos,ind[i]})
  71. gpu.set(4,yPos,items.shop[ind[i]].text)
  72. gpu.set(54,yPos,items.shop[ind[i]].price)
  73. if tonumber(items.shop[ind[i]].available) > 0 then
  74. gpu.set(64,yPos,items.shop[ind[i]].available)
  75. else
  76. gpu.set(64,yPos,"-")
  77. end
  78. end
  79. yPos = yPos + 2
  80. end
  81. end
  82.  
  83. local function scroll(n)
  84. if n == 1 or n == "+" then
  85. drawFrom = drawFrom - 11
  86. else
  87. drawFrom = drawFrom + 11
  88. end
  89. if drawFrom >= #ind - 11 then
  90. drawFrom = #ind - 11
  91. end
  92. if drawFrom <= 0 then
  93. drawFrom = 0
  94. end
  95. drawlist()
  96. end
  97.  
  98. gpu.setResolution(80,25)
  99. gpu.setBackground(0x000000)
  100. gpu.setForeground(0xFFFFFF)
  101. os.execute("cls")
  102. drawlist()
  103.  
  104. while run do
  105. local e = {event.pull(1)}
  106. if e[1] == "key_down" then
  107. if e[4] == 29 then
  108. run = false
  109. elseif e[4] == 200 then
  110. scroll("+")
  111. elseif e[4] == 208 then
  112. scroll("-")
  113. end
  114. elseif e[1] == "scroll" then
  115. scroll(e[5])
  116. elseif e[1] == "touch" then
  117. gpu.setBackground(0x000000)
  118. gpu.setForeground(0xFFFFFF)
  119. gpu.set(1,1,e[3].." "..e[4].." ")
  120. choice = false
  121. for i = 1,#pos_str do
  122. if e[3] <= 77 and e[4] == pos_str[i][1] then
  123. choice = pos_str[i][2]
  124. break
  125. end
  126. end
  127. if choice then
  128. drawlist()
  129. gpu.set(10,1,"choice = "..choice.." ")
  130. square(1,e[4],77,1,0xDEDE6C)
  131. gpu.setForeground(0x3366CC)
  132. gpu.set(4,e[4],items.shop[choice].text)
  133. gpu.set(54,e[4],items.shop[choice].price)
  134. if tonumber(items.shop[choice].available) > 0 then
  135. gpu.set(64,e[4],items.shop[choice].available)
  136. else
  137. gpu.set(64,e[4],"-")
  138. end
  139. end
  140. end
  141. end
  142.  
  143. gpu.setResolution(80,25)
  144. gpu.setBackground(0x000000)
  145. gpu.setForeground(0xFFFFFF)
  146. os.execute("cls")
Advertisement
Add Comment
Please, Sign In to add comment