Advertisement
zkb1325

Minecraft Computer Craft View Visitor Log

Apr 6th, 2024 (edited)
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.11 KB | None | 0 0
  1. --[[
  2.     Pair with https://pastebin.com/ga0KRZ4f to view the logs
  3.     You will need a 6 wide monitor
  4.  
  5.     Simple startup file
  6.  
  7.     if not peripheral.find("playerDetector") then
  8.         term.setCursorPos(1, 1)
  9.         term.write("No player detector found")
  10.         return
  11.     end
  12.    
  13.     if not peripheral.find("monitor") then
  14.         term.setCursorPos(1, 1)
  15.         term.write("No monitor found to display logs")
  16.         return
  17.     end
  18.    
  19.     shell.openTab("lookUp.lua")
  20.     shell.openTab("visitorLog.lua")
  21. ]]
  22. Monitor = peripheral.find("monitor")
  23. term.clear()
  24.  
  25. function EnterInput()
  26.     term.setCursorPos(1, 1)
  27.     term.clearLine()
  28.     term.write("'list' to list logged names")
  29.     term.setCursorPos(1, 2)
  30.     term.clearLine()
  31.     term.write("<playerName> to display logs of player")
  32.     term.setCursorPos(1, 3)
  33.     term.clearLine()
  34.     term.write("Input: ")
  35.     return read()
  36. end
  37.  
  38. function GetLogs()
  39.     local visitorFile = fs.open("visitorLog.txt", "r")
  40.     local log = visitorFile.readAll()
  41.     visitorFile.close()
  42.     return textutils.unserialize(log)
  43. end
  44.  
  45. local function formatSeconds(seconds, noDays)
  46.     local days = math.floor(seconds/86400)
  47.     local hours = math.floor(math.mod(seconds, 86400)/3600)
  48.     local minutes = math.floor(math.mod(seconds,3600)/60)
  49.     local seconds = math.floor(math.mod(seconds,60))
  50.     if noDays then
  51.         return string.format("%02d:%02d:%02d",hours,minutes,seconds)
  52.     else
  53.         return string.format("%d:%02d:%02d:%02d",days,hours,minutes,seconds)
  54.     end
  55. end
  56.  
  57. function DisplayLogs(lookUpName)
  58.     Monitor.clear()
  59.     Monitor.setCursorPos(1, 1)
  60.     Monitor.setTextColor(colors.white)
  61.     Monitor.write("Logs for ")
  62.     Monitor.setCursorPos(10, 1)
  63.     Monitor.setTextColor(colors.blue)
  64.     Monitor.write(lookUpName)
  65.     Monitor.setCursorPos(10+#lookUpName, 1)
  66.     Monitor.setTextColor(colors.white)
  67.     Monitor.write(" | ")
  68.     Monitor.setCursorPos(10+#lookUpName+3, 1)
  69.     Monitor.setTextColor(colors.orange)
  70.     Monitor.write(formatSeconds(GetPlayerTotalTime(lookUpName)))
  71.     local visitorLogs = GetLogs()
  72.     if not visitorLogs[lookUpName] then
  73.         Monitor.setCursorPos(1, 2)
  74.         Monitor.write("No logs found")
  75.         return
  76.     end
  77.  
  78.     local playersLogs = visitorLogs[lookUpName]
  79.     for i,v in ipairs(Reverse(playersLogs)) do
  80.         Monitor.setCursorPos(1, i+1)
  81.         Monitor.setTextColor(colors.green)
  82.         Monitor.write(v[1])
  83.        
  84.         Monitor.setCursorPos(24, i+1)
  85.         Monitor.setTextColor(colors.white)
  86.         Monitor.write(" | ")
  87.        
  88.         Monitor.setCursorPos(27, i+1)
  89.         Monitor.setTextColor(colors.red)
  90.         Monitor.write((v[2] or "?"))
  91.        
  92.         Monitor.setCursorPos(51, i+1)
  93.         Monitor.setTextColor(colors.orange)
  94.         Monitor.write(formatSeconds(v[3] or 0))
  95.     end
  96.  
  97. end
  98.  
  99. function GetPlayerTotalTime(playerName)
  100.     local playersLogs = GetLogs()[playerName]
  101.     if not playersLogs then
  102.         return 0
  103.     end
  104.     local totalTime = 0
  105.     for i,v in ipairs(playersLogs) do
  106.         if v[2] then
  107.             totalTime = totalTime + v[3]
  108.         end
  109.     end
  110.     return totalTime
  111. end
  112.  
  113. function ListPlayers()
  114.     local logs = GetLogs()
  115.     term.setCursorPos(1, 5)
  116.     term.clearLine()
  117.     term.write("There are logs for...")
  118.     local count = 0
  119.     for i,v in pairs(logs) do
  120.         term.setCursorPos(1, 6+count)
  121.         term.clearLine()
  122.         term.setTextColor(colors.blue)
  123.         term.write(i)
  124.         term.setCursorPos(#i+1, 6+count)
  125.         term.setTextColor(colors.white)
  126.         term.write(" | ")
  127.         term.setCursorPos(#i+4, 6+count)
  128.         term.setTextColor(colors.orange)
  129.         term.write(formatSeconds(GetPlayerTotalTime(i)))
  130.         term.setTextColor(colors.white)
  131.         count = count+1
  132.     end
  133. end
  134.  
  135. function Reverse(table)
  136.     for i = 1, #table/2, 1 do
  137.         table[i], table[#table-i+1] = table[#table-i+1], table[i]
  138.     end
  139.     return table
  140. end
  141.  
  142. while true do
  143.     local search = EnterInput()
  144.     if search == "list" or search == "List" then
  145.         ListPlayers()
  146.     else
  147.         DisplayLogs(search)
  148.     end
  149. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement