Advertisement
DevilTvLP

openComputersMicroRedstone

Jun 26th, 2018 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1. local name = ""
  2. local channel = ""
  3. local channelPassword = ""
  4. local side = require("sides").left
  5.  
  6. --*************Imports*********************
  7. local event = require("event")
  8. local net = require("internet")
  9. local os = require("os")
  10. local component = require("component")
  11.  
  12. --***************************TCPClient***************************************
  13. local tcp = {}
  14. tcp.__index = tcp
  15. function tcp:connect(name)
  16.   local t = {}
  17.   setmetatable(t, tcp)
  18.   t.con = net.open("clockville.eu", 23456)
  19.   if t.con then
  20.     t:sendString("login " .. name)
  21.     return t
  22.   else
  23.     return nil
  24.   end
  25. end
  26. function tcp:isConnected()
  27.   if self.con then
  28.     return true
  29.   else
  30.     return false
  31.   end
  32. end
  33. function tcp:sendString(s)
  34.   self.con:write(s .. "\n")
  35.   self.con:flush()
  36. end
  37. function tcp:receiveString()
  38.   local s = self.con:read()
  39.   self.con:flush()
  40.   return s
  41. end
  42. --****************************CommandExecutor********************************
  43. function tcp:execute(command, callback)
  44.   local split_ = command:gmatch("%S+")
  45.   local split = {}
  46.   for word in split_ do table.insert(split, word) end
  47.  
  48.   local channel = split[2]
  49.   local from = split[3]
  50.   local label = ""
  51.   local args = {}
  52.   for i=4,#split do
  53.     args[i-3] = split[i]
  54.     label = label..split[i]
  55.     if i<#split then
  56.       label = label.." "
  57.     end
  58.   end
  59.   callback(channel, from, label, args)
  60. end
  61.  --***************************Program start**********************************
  62.  
  63. client = tcp:connect(name)
  64. if client:isConnected() then
  65.   client:sendString("createChannel "..channel.." "..channelPassword)
  66.   client:sendString("joinChannel "..channel.." "..channelPassword)
  67.   client:sendString("listenToChannel "..channel)
  68.  
  69.   while true do
  70.     rcv = client:receiveString()
  71.     tcp:execute(rcv, function(channel, from, label, args)
  72.       if label=="on" then component.redstone.setOutput(side, 15) end
  73.       if label=="off" then component.redstone.setOutput(side, 0) end
  74.     end)
  75.   end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement