skypop

server

Sep 8th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- http://pastebin.com/1yDdHUvM
  2. os.loadAPI("ini")
  3. local modLIP_ServerPatchedVersion = ini.serverPatchedVersion
  4. local reloadUsersTimeout = ini.userlistTimeout
  5. local rsSide = ini.rsOutput
  6. local driveUser = peripheral.wrap(ini.driveUser)
  7. local modem = peripheral.wrap(ini.wirelessModem)
  8. local wi = peripheral.find("WorldInterface")
  9. local ed = peripheral.find("EntityDetector")
  10. local usersAPI = "/"..driveUser.getMountPath().."/users"
  11. if not os.loadAPI(usersAPI) then
  12.     error("Users API not found",0)
  13.     return
  14. end
  15.  
  16. local _setCursorPos = term.setCursorPos
  17. local _clear,_clearLine = term.clear,term.clearLine
  18. local _write = term.write
  19. local _json = textutils.serializeJSON
  20. local _startTimer = os.startTimer
  21. local _pullEventRaw = os.pullEventRaw
  22. local _loadAPI,_unloadAPI = os.loadAPI,os.unloadAPI
  23. local _clock = os.clock
  24. local _floor = math.floor
  25. local _format = string.format
  26.  
  27. if not (modem and wi and ed) then
  28.     _setCursorPos(1,1)
  29.     _clear()
  30.     print(_format([[modem: %s
  31. WorldInterface: %s
  32. EntityDetector: %s]],
  33.         modem and "ok" or "missing",
  34.         wi and "ok" or "missing",
  35.         ed and "ok" or "missing")
  36.     )
  37.     error("Missing Peripheral",0)
  38.     return
  39. end
  40.  
  41. local w,h = term.getSize()
  42.  
  43. local function output(line,str)
  44.     _setCursorPos(1,line)
  45.     _clearLine()
  46.     _write(str)
  47. end
  48. local function strTime(sec)
  49.     local str,d,h,m,s="",_floor(sec/86400),_floor(sec/3600)%24,_floor(sec/60)%60,_floor(sec%60)
  50.     if d>0 then str = d>1 and d.." days " or "1 day " end
  51.     if h>0 then str = str..h.."h" end
  52.     if m>0 then str = str..(m>9 and m or "0"..m) end
  53.     if h==0 then str = m>0 and str..":"..(s>9 and s or "0"..s) or s.."s" end
  54.     return str
  55. end
  56.  
  57. local function _(query)
  58.     local b,r = false,"405 Method Not Allowed"
  59.     if  #query>=2 and query[1]=="WorldInterface" then
  60.         if query[2]=="getBiome" then
  61.             if #query>=5 and tonumber(query[3]) and tonumber(query[4]) and tonumber(query[5]) then
  62.                 b = true
  63.                 r = wi.getBiome(tonumber(query[3]),tonumber(query[4]),tonumber(query[5]))
  64.             else
  65.                 b = false
  66.                 r = [[WorldInterface.getBiome(X,Y,Z)]]
  67.             end
  68.         elseif  query[2]=="getWeather" then
  69.             b = true
  70.             r = wi.getWeather()
  71.         elseif query[2]=="getBlockInfos" then
  72.             if #query>=5 and tonumber(query[3]) and tonumber(query[4]) and tonumber(query[5]) then
  73.                 b = true
  74.                 r = wi.getBlockInfos(tonumber(query[3]),tonumber(query[4]),tonumber(query[5]))
  75.             else
  76.                 b = false
  77.                 r = [[WorldInterface.getBlockInfos(X,Y,Z)]]
  78.             end
  79.         elseif modLIP_ServerPatchedVersion and query[2]=="getBlockDatatags" then
  80.             if #query>=5 and tonumber(query[3]) and tonumber(query[4]) and tonumber(query[5]) then
  81.                 b = true
  82.                 r = wi.getBlockDatatags(tonumber(query[3]),tonumber(query[4]),tonumber(query[5]))
  83.             else
  84.                 b = false
  85.                 r = [[WorldInterface.getBlockDatatags(X,Y,Z)]]
  86.             end
  87.         elseif query[2]=="getRealDate" then
  88.             b = true
  89.             r = wi.getRealDate()
  90.         elseif query[2]=="getPlayerList" then
  91.             b = true
  92.             r = wi.getPlayerList()
  93.         end
  94.     elseif  #query>=2 and query[1]=="EntityDetector" then
  95.         if query[2]=="getEntityList" then
  96.             if #query>=6 and tonumber(query[3]) and tonumber(query[4]) and tonumber(query[5]) and tonumber(query[6]) then
  97.                 b = true
  98.                 r = ed.getEntityList(tonumber(query[3]),tonumber(query[4]),tonumber(query[5]),tonumber(query[6]))
  99.             else
  100.                 b = false
  101.                 r = [[EntityDetector.getEntityList(Range,X,Y,Z)]]
  102.             end
  103.         elseif modLIP_ServerPatchedVersion and query[2]=="getEntityListAdvanced" then
  104.             if #query>=6 and tonumber(query[3]) and tonumber(query[4]) and tonumber(query[5]) and tonumber(query[6]) then
  105.                 b = true
  106.                 r = ed.getEntityListAdvanced(tonumber(query[3]),tonumber(query[4]),tonumber(query[5]),tonumber(query[6]))
  107.             else
  108.                 b = false
  109.                 r = [[EntityDetector.getEntityListAdvanced(Range,X,Y,Z)]]
  110.             end
  111.         elseif query[2]=="getPlayerDetail" then
  112.             if #query>=3 then
  113.                 b = true
  114.                 r = ed.getPlayerDetail(tostring(query[3]))
  115.             else
  116.                 b = false
  117.                 r = [[EntityDetector.getPlayerDetail(PlayerName)]]
  118.             end
  119.         end
  120.     end
  121.     return b and r, r
  122. end
  123.  
  124. local function terminate()
  125.     _setCursorPos(1,1)
  126.     _clear()
  127.     print("/server - "..string.char(169).." SukaiPoppuGo")
  128.     error("Terminated",0)
  129. end
  130.  
  131. local function process(from, query)
  132.     local _key = query[1]
  133.     local valid = type(users.key[_key])~="nil" and users.key[_key]
  134.     query = query[2]
  135.     if valid then
  136.         local b,r
  137.         if type(query)=="table" and #query>=1 then
  138.             b,r = _(query)
  139.         end
  140.         modem.transmit(from,os.getComputerID(),{b,r})
  141.     end
  142.     output(4,(valid and "200" or "403").." "..tostring(_key))
  143.     output(5,_format("to: %s, from: %s, dist: %s",tostring(to), tostring(from), tostring(dist)))
  144.     output(6,_json(query))
  145. end
  146.  
  147. modem.open(os.getComputerID())
  148. _setCursorPos(1,1)
  149. _clear()
  150. output(1,"#"..os.getComputerID().." - "..os.getComputerLabel())
  151. output(3,string.rep(string.char(140),w))
  152. local reloadUsers = _startTimer(reloadUsersTimeout)
  153. local t = _startTimer(5)
  154. while true do
  155.     output(2,"Running: "..strTime(_clock()))
  156.     local e, param, to, from, query, dist = _pullEventRaw()
  157.     if e=="terminate" then
  158.         terminate()
  159.         return
  160.     elseif e=="timer" and param==reloadUsers then
  161.         _unloadAPI(usersAPI) _loadAPI(usersAPI)
  162.         reloadUsers = _startTimer(reloadUsersTimeout)
  163.     elseif e=="timer" and param==t then
  164.         t = _startTimer(5)
  165.         --sleep(.1)
  166.     elseif e=="modem_message" and type(query)=="table" and #query==2 then
  167.         process(from, query)
  168.     end
  169. end
Add Comment
Please, Sign In to add comment