MeXaN1cK

Табло перед магазином

Apr 18th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.65 KB | None | 0 0
  1. --tablo
  2. local term=require('term')
  3. local event=require('event')
  4. local shell=require('shell')
  5. local com = require("component")
  6. local fs=require('filesystem')
  7. local serialization=require('serialization')
  8. local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
  9. local function loadTbl(fl) file = io.open(fl, 'r') if not file then file = io.open(fl, 'w') file:write('{}') return {} else return serialization.unserialize(file:read('*a')) end file:close() end
  10. local gpu=com.gpu
  11. local m=com.modem
  12. local addressRobot='e51089c7-42c7-465b-9791-ccd8758480ea'
  13. m.open(1)
  14. items1={} price1={}
  15. local thread=require('thread')
  16. thread.init()
  17. gpu.setResolution(80,29)
  18. A,B=gpu.getResolution()
  19. col={blue=0x0000FF,blue1=0x0000AF,grey=0x898989,grey1=0xACACAC}
  20.  
  21. function top()
  22. gpu.setBackground(col.grey) term.clear()
  23. gpu.setBackground(col.grey1) gpu.fill(51,2,15,28,' ')
  24. gpu.setBackground(col.blue1) gpu.fill(1,1,A,1,' ')
  25. term.setCursor(5,1) io.write('Предмет:')
  26. gpu.setBackground(col.blue) term.setCursor(51,1) io.write('   В наличии:  ') gpu.setBackground(col.blue1)
  27. term.setCursor(A-12,1) io.write('Цена за шт:')
  28. end
  29. function wr()
  30. top()
  31. while true do
  32. items=loadTbl('items.txt') for i=1,#items do items1[i]=items[i] end
  33. for i=1,#items do
  34. os.sleep(0.1)
  35. if items1[i]~=nil then
  36. if i==29 and i<57 then top() os.sleep(15) term.clear()
  37. elseif i==1 then term.clear() top()
  38. elseif i==56 and i>28 then top() os.sleep(15) term.clear()
  39. elseif i==84 and i>56 then top() os.sleep(15) term.clear() end
  40. mas=loadTbl(items[i]..'.txt')
  41. if #items1<29 then gpu.setBackground(col.grey) term.setCursor(2,i+1) io.write(items[i]) term.setCursor(57,i+1) gpu.setBackground(col.grey1) io.write(mas[1]) term.setCursor(A-8,i+1) gpu.setBackground(col.grey) io.write(mas[2]) end
  42. if i<29 then gpu.setBackground(col.grey) term.setCursor(2,i+1) io.write(items[i]) term.setCursor(57,i+1) gpu.setBackground(col.grey1) io.write(mas[1]) term.setCursor(A-8,i+1) gpu.setBackground(col.grey) io.write(mas[2])
  43. elseif i<47 and i>28 then gpu.setBackground(col.grey) term.setCursor(2,i+1) io.write(items[i]) term.setCursor(57,i+1) gpu.setBackground(col.grey1) io.write(mas[1]) term.setCursor(A-8,i+1) gpu.setBackground(col.grey) io.write(mas[2])
  44. elseif i<85 and i>48 then gpu.setBackground(col.grey) term.setCursor(2,i+1) io.write(items[i]) term.setCursor(57,i+1) gpu.setBackground(col.grey1) io.write(mas[1]) term.setCursor(A-8,i+1) gpu.setBackground(col.grey) io.write(mas[2]) end
  45. end
  46. end
  47. os.sleep(20)
  48. end
  49. end
  50. gpu.setBackground(col.grey)
  51. function osn()
  52. while true do
  53. c=nil price=nil amount=nil b=nil
  54. key,_,address,port,_,message = event.pull()
  55. if key=='modem_message' then
  56. if message~=nil then
  57. c=string.sub(message,1,3) price=string.sub(message,4,9) amount=string.sub(message,10,13) b=string.sub(message,14)
  58. items=loadTbl('items.txt')
  59. amount=tonumber(amount)
  60. price=tonumber(price)
  61. if c=='add' then
  62. for i=1,#items1 do if items[i]==b then per=1 per2=i break else per=0 end end
  63. if per==0 then items1[#items1+1]=b saveTbl(items1,'items.txt') f1=io.open(b..'.txt','w') f1:write('{'..amount..','..price..'}') f1:close()
  64. elseif per==1 then f2=loadTbl(items1[per2]..'.txt') f2[1]=amount saveTbl(f2,items1[per2]..'.txt') end  
  65. elseif c=='del' then
  66. for i=1,#items do if items[i]==b then per=1 per2=i break else per=0 end end
  67. if per==1 then
  68. shell.execute('del '..items[per2]..'.txt')
  69. items1={}
  70. for sp=1,#items do if items[sp]~=items[per2] then items1[#items1+1]=items[sp] end end
  71. items=loadTbl('items.txt') saveTbl(items1,'items.txt') end  
  72. end end end end end
  73. m.send(addressRobot,1,'check')
  74. thread.create(wr)
  75. thread.create(osn)
  76. thread.waitForAll()
Advertisement
Add Comment
Please, Sign In to add comment