Advertisement
ZNZNCOOP

BunkerControl

Jan 8th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. tu= textutils.unserialize
  2. ts= textutils.serialize
  3. m= peripheral.wrap('top')
  4. function isClient(message)
  5.    data= {}
  6.    data= textutils.unserialize(message)
  7.    for i=1,#client_list do
  8.       if (data[1] == client_list[i]) then return true end
  9.    end
  10.    return false
  11. end
  12. client_list= {'door_1'}
  13. object= {}
  14. function createObjects()
  15.    for i=1,#client_list do
  16.       object[#object+1]= {}
  17.       object[#object]['name']= client_list[i]
  18.       object[#object]['status']= false
  19.       object[#object]['time']= 0
  20.    end
  21. end
  22.  
  23. function removeObject(count)
  24.    table.remove(object,count)
  25. end
  26.  
  27. function updateObjects()
  28.    for i=#object,1,-1 do
  29.       object[i]['time']= object[i]['time']+1
  30.       if (object[i]['time'] > 200) then removeObject(i) end
  31.    end
  32. end
  33.  
  34. function drawList()
  35.    term.clear()
  36.    term.setCursorPos(1,1)
  37.    for i=1,#object do
  38.     print(object[i]['name'],' ',object[i]['time'],' ',object[i]['status'])
  39.    end
  40. end
  41.  
  42. function updateTime(name)
  43.    for i=1,#object do
  44.       if (object[i]['name'] == name) then object[i]['time']= 0 end
  45.    end
  46. end
  47.  
  48. function updateStatus(name,status)
  49.    for i=1,#object do
  50.       if (object[i]['name'] == name) then object[i]['status']= status end
  51.    end
  52. end
  53.  
  54. function createPackage(name,datas)
  55.    data= {}
  56.    data[1]= name
  57.    data[2]= datas
  58.    tmp= ts(data)
  59.    return tmp
  60. end
  61.  
  62. function transmitPackage(package)
  63.    m.transmit(CHANNELSEND,CHANNELSEND,package)
  64. end
  65.  
  66. function ni(mess)
  67.    s= {}
  68.    tokens= {}
  69.    for i=1,#mess do
  70.       s[#s+1]= string.sub(mess,i,i)
  71.    end
  72.    forS= false
  73.    tmp= ''
  74.    for i=1,#s do
  75.       forS= false
  76.       if (s[i] == ' ' or s[i+1] == nil) then
  77.          if (s[i+1] == nil) then
  78.             tmp= tmp..s[i]
  79.             tokens[#tokens+1]= tmp
  80.             tmp= ''
  81.             break
  82.          end
  83.          forS= true
  84.          tokens[#tokens+1]= tmp
  85.          tmp= ''
  86.       end
  87.       if (forS == false) then
  88.          tmp= tmp..s[i]
  89.       end
  90.    end
  91.    if (tokens[1] == 'open') then
  92.       transmitPackage(createPackage(tokens[2],ts({'open'})))
  93.    end
  94.    if (tokens[1] == 'close') then
  95.       transmitPackage(createPackage(tokens[2],ts({'close'})))
  96.    end
  97. end
  98.  
  99. MESSAGE= ''
  100. CHANNELSEND= 1
  101. CHANNELRECEIVE= 2
  102. NAME= 'mainserver'
  103. m.open(CHANNELSEND)
  104. m.open(CHANNELRECEIVE)
  105. createObjects()
  106. os.startTimer(0)
  107. X,Y= term.getSize()
  108. while true do
  109.    drawList()
  110.    term.setCursorPos(1,Y-1)
  111.    write('Write Command:'..MESSAGE)
  112.    event,a,b,c,d= os.pullEvent()
  113.    if (event == 'modem_message') then
  114.       if (isClient(d)) then
  115.          data= {}
  116.          datas= {}
  117.          data= tu(d)
  118.          datas= tu(data[2])
  119.          if (datas[1] == 'ping') then
  120.             updateTime(data[1])
  121.             updateStatus(data[1],datas[2])
  122.          end
  123.       end
  124.    end
  125.    if (event == 'char') then
  126.       MESSAGE= MESSAGE..a
  127.    end
  128.    if (event == 'key') then
  129.       if (a == 14) then
  130.          MESSAGE= string.sub(MESSAGE,1,#MESSAGE-1)
  131.       end
  132.       if (a == 28) then
  133.          ni(MESSAGE)
  134.          MESSAGE= ''
  135.       end
  136.    end
  137.    if (event == 'timer') then
  138.       updateObjects()
  139.       os.startTimer(0)
  140.    end
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement