joeld98

CCBillboard

Jul 2nd, 2016
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.loadAPI("ocs/apis/sensor")
  2. --WS = sensor.wrap("right")
  3. PS = sensor.wrap("top")
  4. FDisplay = peripheral.wrap("monitor_2")
  5. RDisplay = peripheral.wrap("monitor_3")
  6.  
  7. local function log(msg) -- adapted from: https://github.com/rxi/log.lua
  8.   if not os.loadAPI("rwt") then
  9.         if not shell.run("pastebin", "get", "TNLjCCKq", "rwt") then
  10.             print("Could not load Time API")
  11.         else
  12.             os.loadAPI("rwt")
  13.         end
  14.   end
  15.   logTime = rwt.getTime("utc",-4,dateTime24_mmddyyyy,10)
  16.   local fp = io.open("CCBillboard.log", "a")
  17.   local str = string.format("[%s]%s\n",logTime, msg)
  18.       fp:write(str)
  19.       fp:close()
  20. end
  21.  
  22. local function round(num, idp) -- Thank you: http://lua-users.org/wiki/SimpleRound
  23.   local mult = 10^(idp or 0)
  24.   return math.floor(num * mult + 0.5) / mult
  25. end
  26.  
  27. local function saveTable(table,fileName) --Adapted from:http://www.computercraft.info/forums2/index.php?/topic/10499-luahelp-how-to-save-a-table-to-file/http://www.computercraft.info/forums2/index.php?/topic/10499-luahelp-how-to-save-a-table-to-file/
  28. log("Saving Table Data...")
  29. hWrite = fs.open(fileName, "w")
  30. hWrite.write(textutils.serialize(table))
  31. hWrite.close()
  32. end
  33.  
  34. local function loadTable(fileName) --Adapted from:http://www.computercraft.info/forums2/index.php?/topic/10499-luahelp-how-to-save-a-table-to-file/http://www.computercraft.info/forums2/index.php?/topic/10499-luahelp-how-to-save-a-table-to-file/
  35. log("Loading Table Data...")
  36.   local hRead = fs.open(fileName, "r")
  37.   if hRead then
  38.    local hTable = textutils.unserialise(hRead.readAll())
  39.    hRead.close()
  40.    return hTable
  41.   else
  42.   return nil
  43.   end
  44. end
  45.  
  46. local function getMonitorSize(display)
  47. local mon = display
  48. local xSize, ySize = mon.getSize()
  49. return xSize, ySize
  50. end
  51.  
  52. function getDistance(pos)
  53.   local xd = pos.X - offset.X
  54.   local yd = pos.Y - offset.Y
  55.   local zd = pos.Z - offset.Z
  56.   return math.sqrt(xd*xd + yd*yd + zd*zd)
  57. end
  58.  
  59. local function playerNearby()
  60.  local targets = PS.getTargets()
  61.  term.write("Player Nearby Called")
  62.  term.setCursorPos(1,3)
  63.  for k, v in pairs(targets) do
  64.     if v.IsPlayer == true then
  65.         term.write("Player found")
  66.         term.setCursorPos(1,4)
  67.         return true
  68.     end
  69.  end  
  70.  return false
  71. end
  72.  
  73. local function displayTitleMarquee (display, message, size, color, bgColor, duration, rate)
  74.   local mon = display
  75.   local width = 0
  76.   local height = 0
  77.   local currLoop = 0
  78.   local speed = rate
  79.   local textColor = colors.white
  80.   local backColor = colors.black
  81.   local text = message
  82.   local textScale = size
  83.   width, height = mon.getSize()
  84.   local loopCount = ((#text + 4) * duration) + (width * 2)
  85.   local currPos = (width + 2)
  86.   mon.setTextScale(5)
  87.   mon.setTextColor(textColor)
  88.   mon.setBackgroundColor(backColor)
  89.   for currLoop = 1, loopCount do
  90.     if currPos == 1 - (#text + 3) then
  91.       currPos = width + 2
  92.     end
  93.     mon.setCursorPos(currPos, 1)
  94.     mon.write(text)
  95.     currPos = currPos - 1
  96.     os.sleep(0.15)  
  97.   end
  98. end
  99.  
  100. local function displayRollMarquee (display, lineOne, lineTwo, lineThree, size, color, bgColor, duration, rate)
  101.   local mon = display
  102.   local width = 0
  103.   local height = 0
  104.   local currLoop = 0
  105.   local speed = rate
  106.   local textColor = color
  107.   local backColor = bgColor
  108.   local textOne = lineOne
  109.   local textTwo = lineTwo
  110.   local textThree = lineThree
  111.   local textScale = size
  112.   local tRow = 1
  113.   local cColumn = 0
  114.   mon.setTextScale(5)
  115.   mon.clear()
  116.   local width, height = mon.getSize()
  117.   local centerRow = (height / 2)
  118.   local currPos = (height + 3)
  119.   local tColumn = ((width / 2) - (#textOne / 2)) + 1.5
  120.   local mColumn = ((width / 2) - (#textTwo / 2)) + 1.5
  121.   local bColumn = ((width / 2) - (#textThree / 2))+ 1.5
  122.   local topLine = window.create(mon,currPos,tColumn,#textOne,1,true)
  123.   topLine.setBackgroundColor(backColor)
  124.   topLine.setTextColor(textColor)
  125.   local midLine = window.create(mon,currPos,mColumn,#textTwo,1)
  126.   midLine.setBackgroundColor(backColor)
  127.   midLine.setTextColor(textColor)
  128.   local bottomLine = window.create(mon,currPos,bColumn,#textThree,1,true)  
  129.   bottomLine.setBackgroundColor(backColor)
  130.   bottomLine.setTextColor(textColor)
  131.   while currPos > (centerRow - 3) do
  132.     mon.clear()
  133.     topLine.reposition(tColumn,currPos,#textOne,1,true)
  134.       topLine.write(textOne)
  135.     currPos = currPos - 1
  136.       os.sleep(speed)
  137.   end
  138.   topLine.setTextColor(textColor)
  139.   topLine.write(textOne)
  140.   os.sleep(speed*2)
  141.   currPos=height+3
  142.   while currPos > (centerRow) do
  143.     mon.clear()
  144.     midLine.reposition(mColumn,currPos,#textTwo,1,true)
  145.       midLine.write(textTwo)
  146.     topLine.redraw()
  147.     currPos = currPos - 1
  148.       os.sleep(speed)
  149.   end
  150.   topLine.redraw()
  151.   midLine.redraw()
  152.   os.sleep(speed*2)
  153.   currPos=height+3
  154.   while currPos > (centerRow + 3) do
  155.     mon.clear()
  156.     bottomLine.reposition(bColumn,currPos,#textThree,1,true)
  157.       bottomLine.write(textThree)
  158.     topLine.redraw()
  159.     midLine.redraw()
  160.     currPos = currPos - 1
  161.     os.sleep(speed)
  162.   end
  163.   topLine.redraw()
  164.   midLine.redraw()
  165.   bottomLine.redraw()
  166.   --os.sleep(speed*2)
  167.   mon = nil
  168. end
  169.  
  170. local function getTimeDate(timezone, offset)
  171.     if not os.loadAPI("rwt") then
  172.         if not shell.run("pastebin", "get", "TNLjCCKq", "rwt") then
  173.             log("Could not load Time API")
  174.         else
  175.             os.loadAPI("rwt")
  176.         end
  177.     end
  178.     local timeTable = rwt.getTimeTable(timezone,offset,true,10)
  179.     return timeTable
  180. end
  181.  
  182. local function updateTimeDate(timezone, offset)
  183.     if not os.loadAPI("rwt") then
  184.         if not shell.run("pastebin", "get", "TNLjCCKq", "rwt") then
  185.             print("Could not load Time API")
  186.         else
  187.             os.loadAPI("rwt")
  188.         end
  189.     end
  190.     if playerNearby() then
  191.         lastDateTime = rwt.getTimeTable(timezone,offset,true,10)
  192.     end
  193. end
  194.  
  195. local function scanPlayerData ()
  196.   storedPlayerData = loadTable("seenPlayers.dat")  
  197.   if not storedPlayerData then
  198.     log("Stored player data not found")
  199.     storedPlayerData = {}
  200.   else 
  201.     log("seenPlayers.dat Loaded")
  202.   end
  203.   local targets = PS.getTargets()
  204.   local playerCount = 0
  205.   for k, v in pairs(targets) do
  206.     if v.IsPlayer == true then
  207.         log("Player Target being updated")
  208.         pData = PS.getTargetDetails(k)
  209.         pID = pData.Username
  210.         storedPlayerData[pID] = {lastFullDate = lastDateTime.preferredDate, lastSeenMonth = lastDateTime.month,lastSeenDay = lastDateTime.day,lastSeenYear = lastDateTime.year,lastSeenTime = lastDateTime.preferredTime, playerHealth = pData.Health, playerFoodLevel = pData.FoodLevel,playerExpericenceLevel = pData.ExperienceLevel   }
  211.         playerCount = playerCount + 1
  212.         log(string.format("%s:%s", "Players updated", playerCount))
  213.     end
  214.   end
  215.   if playerCount == 0 then
  216.     log("No Players Updated")
  217.     return false
  218.   else
  219.     log(string.format("%s:%s", "Players updated", playerCount))
  220.     saveTable(storedPlayerData,"seenPlayers.dat")
  221.     log(string.format("%s:%s", "Players saved", playerCount))
  222.     return true
  223.   end
  224. end
  225.  
  226. local function scanPlayerData ()
  227.   storedPlayerData = loadTable("seenPlayers.dat")  
  228.   if not storedPlayerData then
  229.     log("Stored player data not found")
  230.     storedPlayerData = {}
  231.   else 
  232.     log("seenPlayers.dat Loaded")
  233.   end
  234.   local targets = PS.getTargets()
  235.   local playerCount = 0
  236.   for k, v in pairs(targets) do
  237.     if v.IsPlayer == true then
  238.         log("Player Target being updated")
  239.         pData = PS.getTargetDetails(k)
  240.         pID = pData.Username
  241.         storedPlayerData[pID] = {lastFullDate = lastDateTime.preferredDate, lastSeenMonth = lastDateTime.month,lastSeenDay = lastDateTime.day,lastSeenYear = lastDateTime.year,lastSeenTime = lastDateTime.preferredTime, playerHealth = pData.Health, playerFoodLevel = pData.FoodLevel,playerExpericenceLevel = pData.ExperienceLevel   }
  242.         playerCount = playerCount + 1
  243.         log(string.format("%s:%s", "Players updated", playerCount))
  244.     end
  245.   end
  246.   if playerCount == 0 then
  247.     log("No Players Updated")
  248.     return false
  249.   else
  250.     log(string.format("%s:%s", "Players updated", playerCount))
  251.     saveTable(storedPlayerData,"seenPlayers.dat")
  252.     log(string.format("%s:%s", "Players saved", playerCount))
  253.     return true
  254.   end
  255. end
  256.  
  257. local function displayPlayerData (display, message, size, color, bgColor, duration, rate)
  258.     local mon = display
  259.     local width = 0
  260.     local height = 0
  261.     local currLoop = 0
  262.     local speed = rate
  263.     local textColor = color
  264.     local backColor = bgColor
  265.     local textOne = message
  266.     local textScale = size
  267.     mon.setTextScale(textScale)
  268.     local width, height = mon.getSize()
  269.     log("Calling scanPlayerData")
  270.     result = scanPlayerData()
  271.     log("scanPlayerData returned")
  272.     log(tostring(result))
  273.     local row = 1
  274.     local tColumn = ((width / 2) - (#textOne / 2)) + 1.5
  275.     mon.setBackgroundColor(backColor)
  276.     mon.setTextColor(textColor)
  277.     mon.setCursorPos(tColumn, row) 
  278.     mon.write(textOne)
  279.     row = row + 4
  280.     if result then
  281.         log("scanPlayerData succeeded")
  282.         sortedPlayers = {}
  283.         for player, v in pairs(storedPlayerData) do
  284.             table.insert(sortedPlayers, {key=player, value=v}) 
  285.         end
  286.         table.sort(sortedPlayers, function(a,b) return a.value.lastFullDate > b.value.lastFullDate end)
  287.         saveTable(sortedPlayers,"sortedPlayers.dat")
  288.         for i, v in ipairs(sortedPlayers) do
  289.         --  print(sf("Row %d, key=%s, value=%s",i, v.key, v.value))
  290.                 mon.setCursorPos(2,row)
  291.                 mon.setTextColor(colors.white)
  292.                 mon.write(string.format("%s last seen on %s", v.key,v.value.lastFullDate))
  293.                 row = row + 1
  294.         end
  295.     else
  296.         log("scanPlayerData failed")
  297.     end
  298. end
  299.  
  300. local function displayDateTime (display, timezone, offset, size, color, bgColor, duration, rate)
  301.   updateTimeDate(timezone, offset)
  302.   local mTime = os.time()
  303.   local mon = display
  304.   local width = 0
  305.   local height = 0
  306.   local currLoop = 0
  307.   local speed = rate
  308.   local timeColor = color
  309.   local timeBackColor = bgColor
  310.   local timeOne = lastDateTime.dayNameLong .. "," .. lastDateTime.monthNameLong .. " "..tostring(lastDateTime.day) .. " " .. tostring(lastDateTime.year)
  311.   local timeTwo = "Minecraft Time is: " .. mTime
  312.   local timeThree = "EDT time is: " .. tostring(lastDateTime.preferredTime)
  313.   local timetextScale = size
  314.   local timeTRow = 1
  315.   local timeCColumn = 1
  316.   mon.clear()
  317.   mon.setTextScale(timetextScale)
  318.   local tWidth, tHeight = mon.getSize()
  319.   local timeCenterRow = (tHeight / 2)
  320.   local timeCurrPos = 1
  321.   local timeTColumn = ((tWidth / 2) - (#timeOne / 2)) + 1.5
  322.   local timeMColumn = ((tWidth / 2) - (#timeTwo / 2)) + 1.5
  323.   local timeBColumn = ((tWidth / 2) - (#timeThree / 2)) + 1.5
  324.   local timeTRow = (timeCenterRow - 3)
  325.   local timeBRow = (timeCenterRow + 3)
  326.   local topTimeLine = window.create(mon,timeTColumn,timeTRow,#timeOne,1,true)
  327.   topTimeLine.setBackgroundColor(timeBackColor)
  328.   topTimeLine.setTextColor(timeColor)
  329.   topTimeLine.write(timeOne)
  330.   local midTimeLine = window.create(mon,timeMColumn,timeCenterRow,#timeTwo,1,true)
  331.   midTimeLine.setBackgroundColor(timeBackColor)
  332.   midTimeLine.setTextColor(timeColor)
  333.   midTimeLine.write(timeTwo)
  334.   --if #timeThree > 30 then mon.setTextScale((timetextScale - 1)) end
  335.   local bottomTimeLine = window.create(mon,timeBColumn,timeBRow,#timeThree,1,true)  
  336.   bottomTimeLine.setBackgroundColor(timeBackColor)
  337.   bottomTimeLine.setTextColor(timeColor)
  338.   bottomTimeLine.write(timeThree)
  339.   mon=nil
  340. end
  341.  
  342. local function displaySun (display, timezone, offset, size, color, bgColor, duration, rate)
  343.   updateTimeDate(timezone, offset)
  344.   local mTime = os.time()
  345.   local mon = display
  346.   local width = 0
  347.   local height = 0
  348.   local currLoop = 0
  349.   local speed = rate
  350.   local timeColor = color
  351.   local timeBackColor = bgColor
  352.   local timeOne = "Sun rise: " .. mTime
  353.   local timeTwo = "Sun set: " .. mTime
  354.   local timeThree = "EDT time is: " .. lastDateTime.preferredTime
  355.   local timetextScale = size
  356.   local timeTRow = 1
  357.   local timeCColumn = 1
  358.   mon.clear()
  359.   mon.setTextScale(timetextScale)
  360.   local tWidth, tHeight = mon.getSize()
  361.   local timeCenterRow = (tHeight / 2)
  362.   local timeCurrPos = 1
  363.   local timeTColumn = ((tWidth / 2) - (#timeOne / 2)) + 1.5
  364.   local timeMColumn = ((tWidth / 2) - (#timeTwo / 2)) + 1.5
  365.   local timeBColumn = ((tWidth / 2) - (#timeThree / 2)) + 1.5
  366.   local timeTRow = (timeCenterRow - 3)
  367.   local timeBRow = (timeCenterRow + 3)
  368.   local topTimeLine = window.create(mon,timeTColumn,timeTRow,#timeOne,1,true)
  369.   topTimeLine.setBackgroundColor(timeBackColor)
  370.   topTimeLine.setTextColor(timeColor)
  371.   topTimeLine.write(timeOne)
  372.   local midTimeLine = window.create(mon,timeMColumn,timeCenterRow,#timeTwo,1,true)
  373.   midTimeLine.setBackgroundColor(timeBackColor)
  374.   midTimeLine.setTextColor(timeColor)
  375.   midTimeLine.write(timeTwo)
  376.   --if #timeThree > 30 then mon.setTextScale((timetextScale - 1)) end
  377.   local bottomTimeLine = window.create(mon,timeBColumn,timeBRow,#timeThree,1,true)  
  378.   bottomTimeLine.setBackgroundColor(timeBackColor)
  379.   bottomTimeLine.setTextColor(timeColor)
  380.   bottomTimeLine.write(timeThree)
  381.   mon=nil
  382. end
  383.  
  384. log("***************************************************")
  385. log("Logging Started")
  386. lastDateTime = getTimeDate("utc", -5)
  387. while true do
  388. displayRollMarquee(FDisplay, "Welcome",  "to", "Valhalla Sector", 5 ,colors.white,colors.black, 5, .15)
  389. displayRollMarquee(RDisplay, "Welcome",  "to", "Valhalla Sector", 5 ,colors.white,colors.black, 5, .15)
  390. os.sleep(10)
  391. displayDateTime(FDisplay,"utc",-5,3,colors.white,colors.black,5,.15)
  392. displayDateTime(RDisplay,"utc",-5,3,colors.white,colors.black,5,.15)
  393. os.sleep(10)
  394. displayPlayerData(FDisplay,"Recently Seen Players", 2 ,colors.blue,colors.black, 5, .15)
  395. displayPlayerData(RDisplay,"Recently Seen Players", 2 ,colors.blue,colors.black, 5, .15)
  396. os.sleep(10)
  397. end
RAW Paste Data