Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tu= textutils.unserialize
- ts= textutils.serialize
- m= peripheral.wrap('top')
- function isClient(message)
- data= {}
- data= textutils.unserialize(message)
- for i=1,#client_list do
- if (data[1] == client_list[i]) then return true end
- end
- return false
- end
- client_list= {'door_1'}
- object= {}
- function createObjects()
- for i=1,#client_list do
- object[#object+1]= {}
- object[#object]['name']= client_list[i]
- object[#object]['status']= false
- object[#object]['time']= 0
- end
- end
- function removeObject(count)
- table.remove(object,count)
- end
- function updateObjects()
- for i=#object,1,-1 do
- object[i]['time']= object[i]['time']+1
- if (object[i]['time'] > 200) then removeObject(i) end
- end
- end
- function drawList()
- term.clear()
- term.setCursorPos(1,1)
- for i=1,#object do
- print(object[i]['name'],' ',object[i]['time'],' ',object[i]['status'])
- end
- end
- function updateTime(name)
- for i=1,#object do
- if (object[i]['name'] == name) then object[i]['time']= 0 end
- end
- end
- function updateStatus(name,status)
- for i=1,#object do
- if (object[i]['name'] == name) then object[i]['status']= status end
- end
- end
- function createPackage(name,datas)
- data= {}
- data[1]= name
- data[2]= datas
- tmp= ts(data)
- return tmp
- end
- function transmitPackage(package)
- m.transmit(CHANNELSEND,CHANNELSEND,package)
- end
- function ni(mess)
- s= {}
- tokens= {}
- for i=1,#mess do
- s[#s+1]= string.sub(mess,i,i)
- end
- forS= false
- tmp= ''
- for i=1,#s do
- forS= false
- if (s[i] == ' ' or s[i+1] == nil) then
- if (s[i+1] == nil) then
- tmp= tmp..s[i]
- tokens[#tokens+1]= tmp
- tmp= ''
- break
- end
- forS= true
- tokens[#tokens+1]= tmp
- tmp= ''
- end
- if (forS == false) then
- tmp= tmp..s[i]
- end
- end
- if (tokens[1] == 'open') then
- transmitPackage(createPackage(tokens[2],ts({'open'})))
- end
- if (tokens[1] == 'close') then
- transmitPackage(createPackage(tokens[2],ts({'close'})))
- end
- end
- MESSAGE= ''
- CHANNELSEND= 1
- CHANNELRECEIVE= 2
- NAME= 'mainserver'
- m.open(CHANNELSEND)
- m.open(CHANNELRECEIVE)
- createObjects()
- os.startTimer(0)
- X,Y= term.getSize()
- while true do
- drawList()
- term.setCursorPos(1,Y-1)
- write('Write Command:'..MESSAGE)
- event,a,b,c,d= os.pullEvent()
- if (event == 'modem_message') then
- if (isClient(d)) then
- data= {}
- datas= {}
- data= tu(d)
- datas= tu(data[2])
- if (datas[1] == 'ping') then
- updateTime(data[1])
- updateStatus(data[1],datas[2])
- end
- end
- end
- if (event == 'char') then
- MESSAGE= MESSAGE..a
- end
- if (event == 'key') then
- if (a == 14) then
- MESSAGE= string.sub(MESSAGE,1,#MESSAGE-1)
- end
- if (a == 28) then
- ni(MESSAGE)
- MESSAGE= ''
- end
- end
- if (event == 'timer') then
- updateObjects()
- os.startTimer(0)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement