Advertisement
DeGariless

OC Server Spawn

Nov 29th, 2015
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. Spawn program made by DeGariless
  3. ]]--
  4.  
  5. require("term").clear()
  6. print("Initializing Spawn Program")
  7.  
  8. print("Adding Custom Libraries")
  9. local component = require("component")
  10. local computer = require("computer")
  11. local inet = require("internet")
  12. local term = require("term")
  13.  
  14. print("Adding Components")
  15. local fs = component.filesystem
  16. local gpu = component.gpu
  17. if not component.isAvailable("onlinedetector") then
  18.   error("Could not find an Online Detector attached to the computer")
  19. end
  20. local od = component.onlinedetector
  21.  
  22. local file = io.open("/lib/json.lua", "r")
  23. if file == nil then
  24.   file = io.open("/lib/json.lua", "w")
  25.   print("Downloading JSON library")
  26.   local result, response = pcall(inet.request, "http://regex.info/code/JSON.lua")
  27.   if result then
  28.     print("Success")
  29.     for chunk in response do
  30.       file:write(chunk)
  31.     end
  32.   end
  33.   file:close()
  34.   print("file saved to /lib/json.lua")
  35. end
  36. file = nil
  37.  
  38. print("Loading JSON library")
  39. local JSON = loadfile("/lib/json.lua")()
  40. print("\n " .. JSON.AUTHOR_NOTE .. [[
  41.  
  42.  - Simple JSON encoding and decoding in pure Lua.
  43.  -
  44.  - Copyright 2010-2016 Jeffery Friedl
  45.  - http://regex.info/blog
  46.  - Latest version: regex.info.blog.lua.json
  47.  -
  48.  - This code is released under a Creative Commons CC-BY "Attribution" License:
  49.  - http://creativecommons.org/licenses/by/3.0/deed.en_US
  50. ]])
  51. os.sleep(5)
  52.  
  53. --######################
  54. --## GLOBAL VARIABLES ##
  55. --######################
  56. print("Setting global variables.")
  57.  
  58. VERSION = "1.1.0"
  59. local args = {...}
  60.  
  61. local PAGETIME = 15  -- Default is 15
  62. local TABWIDTH = 20
  63.  
  64. --Resolution
  65. local w = 87 -- Default: 87
  66. local h = 25 -- Default: 25
  67.  
  68.  
  69.  
  70. --################
  71. --## LOAD FILES ##
  72. --################
  73. print("Loading Files.")
  74.  
  75. file = io.open("Rules.txt", "r")
  76. if file == nil then
  77.   print("Rules.txt not found. Creating default file.")
  78.   file = io.open("Rules.txt", "w")
  79.   file:write("Add server rules in Rules.txt")
  80.   file:close()
  81.   file = io.open("Rules.txt", "r")
  82. end
  83. local rules = file:read("*all")
  84. file:close()
  85. file = nil
  86.  
  87. file = io.open("PlayerData.json", "r")
  88. if file == nil then
  89.   print("PlayerData.json not found. Creating default file.")
  90.   file = io.open("PlayerData.json", "w")
  91.   file:write('{"owner":[],"staff":[],"players":[]}')
  92.   file:close()
  93.   file = io.open("PlayerData.json", "r")
  94. end
  95. local playerData = JSON:decode( file:read("*all") )
  96. file:close()
  97. file = nil
  98.  
  99. file = io.open("Announcements.txt", "r")
  100. if file == nil then
  101.   print("Announcements.txt not found. Creating default file.")
  102.   file = io.open("Announcements.txt", "w")
  103.   file:write("  There are no server announcementes at this time.")
  104.   file:close()
  105.   file = io.open("Announcements.txt", "r")
  106. end
  107. local announcements = file:read("*all")
  108. file:close()
  109. file = nil
  110.  
  111. file = io.open("FAQ.txt", "r")
  112. if file == nil then
  113.   print("FAQ.txt not found. Creating default file.")
  114.   file = io.open("FAQ.txt", "w")
  115.   file:write("  Q: I can haz free diamonds?\n  A: Nope")
  116.   file:close()
  117.   file = io.open("FAQ.txt", "r")
  118. end
  119. local faq = file:read("*all")
  120. file:close()
  121. file = nil
  122.  
  123.  
  124.  
  125. --###########
  126. --## THEME ##
  127. --###########
  128. print("Setting theme.")
  129.  
  130. local theme = {
  131.   bgLow = 0x004422,
  132.   bg = 0x006633,
  133.   bgHigh = 0x669966,
  134.   textLow = 0x999955,
  135.   text = 0xCC9933,
  136.   textHigh = 0xFFCC99,
  137.   online = 0x44FF44,
  138.   err = 0xFF0000  
  139. }
  140.  
  141. --###############
  142. --## FUNCTIONS ##
  143. --###############
  144. print("Setting functions.")
  145.  
  146.  
  147. local function centerPrint(string, y)
  148.   local start = math.floor( (w - string:len()) / 2 )
  149.   term.setCursor(start, y)
  150.   term.write(string)
  151. end
  152.  
  153. local function title(string)
  154.   gpu.setBackground(theme.bgHigh)
  155.   gpu.setForeground(theme.textHigh)
  156.   gpu.fill(1, 1, w, 4, " ")
  157.   centerPrint(string, 2)
  158. end
  159.  
  160. local function clear()
  161.   gpu.setBackground(theme.bg)
  162.   gpu.setForeground(theme.text)
  163.   gpu.fill(1, 5, w, h-4, " ")
  164.   term.setCursor(1, 6)
  165. end
  166.  
  167. local function writeFile(fileName)
  168.   local file = io.open(fileName, "r")
  169.   local text = file:read("*all")
  170.   file:close()
  171.   term.write(text)  
  172. end
  173.  
  174. local function time()  
  175.   local req = nil
  176.   local data = nil
  177.   req = inet.request("http://www.timeapi.org/utc/now?format=%25Y%25j%25H%25M")
  178.   if not pcall(function () data = req() end) then
  179.     req:close()
  180.     return nil
  181.   end
  182.   if #data ~= 11 then
  183.     return nil
  184.   end
  185.   return data
  186. end
  187.  
  188.  
  189. local function timeDif(pTime, curTime)
  190.   pTime = tostring(pTime)
  191.   local cYear = curTime:sub(1,4)
  192.   local cDay = curTime:sub(5,7)
  193.   local cHour = curTime:sub(8,9)
  194.   local cMin = curTime:sub(10,11)
  195.   local pYear = pTime:sub(1,4)
  196.   local pDay = pTime:sub(5,7)
  197.   local pHour = pTime:sub(8,9)
  198.   local pMin = pTime:sub(10,11)
  199.   local dYear = cYear - pYear
  200.   local dDay = cDay - pDay
  201.   local dHour = cHour - pHour
  202.   local dMin = cMin - pMin
  203.   local dif = math.floor(((dYear * 365 + dDay) * 24 + dHour) * 60 + dMin)  --dif is in minutes
  204.   if dif < 2 then
  205.     return "a moment ago"
  206.   elseif dif < 60 then
  207.     return(dif.." minutes ago")
  208.   end
  209.   dif = math.floor(dif/6+0.5)/10  --dif is in hours
  210.   if dif == 1 then
  211.     return "1 hour ago"
  212.   elseif dif < 24 then
  213.     return(dif.." hours ago")
  214.   end
  215.   dif = math.floor(dif/24)  --dif is in days
  216.   if dif == 1 then
  217.     return "yesterday"
  218.   elseif dif < 7 then
  219.     return(dif.." days ago")
  220.   elseif dif < 14 then
  221.       return "last week"
  222.   elseif dif < 30 then
  223.     return(math.floor(dif/7).." weeks ago")
  224.   elseif dif < 61 then
  225.     return "last month"
  226.   elseif dif < 365 then
  227.     return(math.floor(dif/30.5).." months ago")
  228.   end
  229.   dif = math.floor(dif/36.5+0.5)/10  -- dif is in years
  230.   if dif == 1 then
  231.     return "last year"
  232.   else
  233.     return(dif.." years ago")
  234.   end
  235. end
  236.  
  237. local function orderPlayerData()
  238.   local orderedPlayers = {}
  239.  
  240.   for _, player in pairs(playerData.players) do
  241.     local found = false
  242.     for k, oPlayer in pairs(orderedPlayers) do
  243.       if tonumber(player.lastseen) > tonumber(oPlayer.lastseen) then
  244.         found = true
  245.         table.insert(orderedPlayers, k, player)
  246.         break
  247.       end
  248.     end
  249.     if not found then
  250.       table.insert(orderedPlayers,  #orderedPlayers+1, player)
  251.     end
  252.   end
  253.   playerData.players = orderedPlayers
  254. end
  255.  
  256.  
  257. local function updatePlayerData(curTime)
  258.   for _, onlinePlayer in ipairs(od.getPlayerList()) do
  259.     local found = false
  260.     for _, v in pairs(playerData.owner) do
  261.       if onlinePlayer == v.name then
  262.         v.lastseen = curTime
  263.         found = true
  264.         break
  265.       end
  266.     end
  267.     if not found then
  268.       for _, v in pairs(playerData.staff) do
  269.         if onlinePlayer == v.name then
  270.           v.lastseen = curTime
  271.           found = true
  272.           break
  273.         end
  274.       end
  275.     end
  276.     if not found then
  277.       for _, v in pairs(playerData.players) do
  278.         if onlinePlayer == v.name then
  279.           v.lastseen = curTime
  280.           found = true
  281.           break
  282.         end
  283.       end
  284.     end
  285.     if not found then
  286.       newPlayerData = {name = onlinePlayer, lastseen = curTime, firstseen = curTime}
  287.       table.insert(playerData.players, newPlayerData)
  288.     end
  289.   end
  290.   orderPlayerData()  
  291.   local file = io.open("PlayerData.json", "w")
  292.   file:write(JSON:encode_pretty(playerData))
  293.   file:close()
  294. end
  295.  
  296. local function lastseen(player, curTime)
  297.   if od.isPlayerOnline(player.name) then
  298.     return "Online"
  299.   else
  300.     return timeDif(player.lastseen, curTime)
  301.   end
  302. end
  303.  
  304.  
  305. --
  306. --TAB GENERATOR
  307. --
  308.  
  309. local function tabs(...)
  310.   term.setCursor(1, 4)
  311.   local args = {...}
  312.   local highTab = args[#args]
  313.   gpu.setBackground(theme.bgHigh)
  314.   gpu.setForeground(theme.textLow)
  315.   for i=1, #args-1 do
  316.     term.write(" ")
  317.     if i == highTab then
  318.       gpu.setForeground(theme.textHigh)
  319.       gpu.setBackground(theme.bg)
  320.       term.write(" ".. args[i] .." ")
  321.       gpu.setForeground(theme.textLow)
  322.       gpu.setBackground(theme.bgHigh)
  323.     else
  324.       gpu.setBackground(theme.bgLow)
  325.       term.write(" "..args[i].." ")
  326.       gpu.setBackground(theme.bgHigh)
  327.     end
  328.   end
  329.   gpu.setBackground(theme.bg)
  330.   gpu.setForeground(theme.text)
  331. end
  332.  
  333.  
  334.  
  335. --
  336. --PAGE GENERATORS
  337. --
  338.  
  339. local function rulesPage()
  340.   term.write(rules)
  341. end
  342.  
  343.  
  344. local function lastseenPage(curTime)
  345.   print("  Owner\n  "..string.rep("-", 60))
  346.   gpu.setForeground(theme.textHigh)
  347.   term.write("  Name"..string.rep(" ", TABWIDTH - 4).."Last seen\n")
  348.   gpu.setForeground(theme.text)
  349.   for k,v in ipairs(playerData.owner) do
  350.     term.write("  "..v.name)
  351.     term.write(string.rep(".", TABWIDTH - #v.name))
  352.     local status = lastseen(v, curTime)
  353.     if status == "Online" then
  354.       gpu.setForeground(theme.online)
  355.     end
  356.     term.write(status)
  357.     gpu.setForeground(theme.text)
  358.     if k%2 == 0 then
  359.       print()
  360.     else
  361.       term.write(string.rep(" ", TABWIDTH - #status - 2))
  362.     end
  363.   end
  364.   term.write("\n\n")
  365.   print("  Players\n  "..string.rep("-", 60))
  366.   gpu.setForeground(theme.textHigh)
  367.   term.write("  Name"..string.rep(" ", TABWIDTH-4).."Last seen\n")
  368.   gpu.setForeground(theme.text)
  369.   for k, v in ipairs(playerData.players) do
  370.     term.write("  " .. v.name)
  371.     term.write(string.rep(".", TABWIDTH - #v.name))
  372.     local status = lastseen(v, curTime)
  373.     if status == "Online" then
  374.       gpu.setForeground(theme.online)
  375.     end
  376.     term.write(status)
  377.     gpu.setForeground(theme.text)
  378.     if k%2 == 0 then
  379.       print()
  380.     else
  381.       term.write(string.rep(" ", TABWIDTH - #status - 2))
  382.     end
  383.     local _, y = term.getCursor()
  384.     if y >= h then
  385.       return
  386.     end
  387.   end
  388. end
  389.  
  390. local function announcementsPage()
  391.   term.write(announcements)
  392. end
  393.  
  394. local function faqPage()
  395.   term.write(faq)
  396. end
  397.  
  398. --###################
  399. --## MAIN FUNCTION ##
  400. --###################
  401. print("Starting Program in 5 seconds!")
  402.  
  403. local function main()
  404.   gpu.setResolution(w, h)
  405.   title("Welcome to the server!")
  406.   while true do
  407.     tabs("Rules","Players","Announcements","FAQ",1)
  408.     clear()
  409.     rulesPage()
  410.     os.sleep(PAGETIME)
  411.     local curTime = time()
  412.     if curTime then
  413.       updatePlayerData(curTime)
  414.       tabs("Rules","Players","Announcements","FAQ",2)
  415.       clear()
  416.       lastseenPage(curTime)
  417.       os.sleep(PAGETIME)
  418.     end
  419.     tabs("Rules","Players","Announcements","FAQ",3)
  420.     clear()
  421.     announcementsPage()
  422.     os.sleep(PAGETIME)
  423.     tabs("Rules","Players","Announcements","FAQ",4)
  424.     clear()
  425.     faqPage()
  426.     os.sleep(PAGETIME)
  427.   end
  428. end
  429.  
  430.  
  431.  
  432. --##########
  433. --## MAIN ##
  434. --##########
  435.  
  436. local err = nil
  437. while err == "too long without yeilding" or err == nil do
  438.   pcall(function () os.sleep(5) end)
  439.   _, err = pcall(main)
  440. end
  441.  
  442.  
  443. --#################
  444. --## ERROR CATCH ##
  445. --#################
  446.  
  447. gpu.setBackground(0x001100)
  448. gpu.setForeground(0xFFFFFF)
  449. gpu.fill(1, 1, w, h, " ")
  450. term.setCursor(1, 1)
  451. print()
  452. print("  Oh noes! The spawn program has crashed D:")
  453. print("  Please create an issue on github")
  454. print("  http://github.com/DeGariless/OC-Server-Spawn/")
  455. print("  make sure to include this error message...")
  456. print("  -----------")
  457. gpu.setForeground(theme.err)
  458. print("  " .. err)
  459. gpu.setForeground(0xFFFFFF)
  460. print()
  461. while true do os.sleep(60) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement