goregous.lua 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. local Goregous = {}
  2. Goregous.mock = true
  3. local function explode(line)
  4. local res = {}
  5. while true do
  6. local pos = line:find(',')
  7. if not pos then break end
  8. table.insert(res, line:sub(1, pos))
  9. line = line:sub(pos + 1)
  10. end
  11. if #line > 0 then table.insert(res, line) end
  12. return res
  13. end
  14. function Goregous:getConnection()
  15. if self.created then return self.socket end
  16. self.socket = (require('socket')).tcp()
  17. self.socket:settimeout(10)
  18. local ip = table.has(arg, 'local') and '127.0.0.1' or ''
  19. local _, e = self.socket:connect(ip, 6060)
  20. if e then self.socket = nil
  21. else self.socket:settimeout(5) end
  22. self.created = true
  23. return self.socket
  24. end
  25. function Goregous:send(data)
  26. if not self:getConnection() then return end
  27. self.socket:send(table.concat(data, ',') .. '\n')
  28. end
  29. function Goregous:patch(version, os)
  30. if not self:getConnection() then return false end
  31. self:send({'version', version, os})
  32. local bytes = tonumber(self.socket:receive('*l'))
  33. if bytes == 0 then return false end
  34. local code, data
  35. code = self.socket:receive(bytes)
  36. data = self.socket:receive(tonumber(self.socket:receive('*l')))
  37. return code, data
  38. end
  39. function Goregous:login(username, password)
  40. if self.mock then return true end
  41. if not self:getConnection() then return false end
  42. self:send({'login', username})
  43. local response = self.socket:receive('*l')
  44. if response == 'ok' then return true end
  45. return false
  46. end
  47. function Goregous:createServer()
  48. if self.mock then return true end
  49. if not self:getConnection() then return false end
  50. self:send({'createServer'})
  51. local response = self.socket:receive('*l')
  52. if response == 'ok' then return true end
  53. return false
  54. end
  55. function Goregous:listServers()
  56. if self.mock then return {} end
  57. if not self:getConnection() then return false end
  58. self:send({'listServers'})
  59. local count = self.socket:receive('*l')
  60. local servers = {}
  61. for i = 1, count do
  62. local line = self.socket:receive('*l')
  63. local data = explode(line)
  64. table.insert(servers, {name = data[1], ip = data[2]})
  65. end
  66. return servers
  67. end
  68. return Goregous