Guest User

Untitled

a guest
Apr 18th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.60 KB | None | 0 0
  1. local configFile = fs.open("client.cfg", "r")
  2. local config = textutils.unserialise(configFile.readAll())
  3. configFile.close()
  4.  
  5. rednet.open(config.modemLocation)
  6.  
  7. local Orientation = {
  8.     NORTH = 0,
  9.     SOUTH = 1,
  10.     WEST = 2,
  11.     EAST = 3
  12. }
  13.  
  14. local startingOrientation = config.orientation
  15. local serverID = config.serverID
  16. local map = {}
  17.  
  18. local function connectToServer()
  19.     rednet.send(config.serverID, {
  20.         type = "connect"
  21.     })
  22.    
  23.     local id,msg = rednet.receive(5)
  24.     if msg == nil or msg.type ~= "accepted" then
  25.         printError("Connection refused.")
  26.         return false
  27.     else
  28.         print("Connected to server (" .. id .. ")")
  29.         serverID = id
  30.         return true
  31.     end
  32. end
  33.  
  34. local function clientLoop(eventData)
  35.     local e, p1, p2, p3, p4, p5 = unpack(eventData)
  36.     if e == "rednet_message" then
  37.         local id, tbl = p1, p2
  38.        
  39.         if id == serverID then
  40.             if tbl.type == "keepalive" then
  41.                 rednet.send(id, { type = "keepalive" })
  42.             elseif tbl.type == "disconnect" then
  43.                 print("Disconnected: " .. tbl.reason)
  44.                 return false
  45.             elseif tbl.type == "map" then
  46.                 print("Received map from server")
  47.                 map = tbl.data
  48.             end
  49.         else
  50.             print("Message from unknown sender " .. id)
  51.             rednet.send(config.serverID, {
  52.                 type = "suspicious_computer",
  53.                 id = id
  54.             })
  55.         end
  56.     elseif e == "terminate" then
  57.         rednet.send(serverID, { type = "disconnect" })
  58.         return false
  59.     end
  60.    
  61.     return true
  62. end
  63.  
  64. local ok = connectToServer(config.serverID)
  65. if not ok then return end
  66.  
  67. rednet.send(config.serverID, { type = "map" })
  68.  
  69. while true do
  70.     local eventData = { os.pullEventRaw() }
  71.     if not clientLoop(eventData) then break end
  72. end
Advertisement
Add Comment
Please, Sign In to add comment