Advertisement
zkb1325

Minecraft Computer Craft Play Time Tracker

Feb 7th, 2024
1,149
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.54 KB | None | 1 0
  1. -- Run on a computer craft command computer
  2. -- Will track the play time of players as they connect saving each players time to their own file
  3. -- Will also display in chat players total time upon connecting and session time upon disconnecting
  4. -- Uses vanilla command /list to get current online players so no peripherals needed
  5. -- It will expect a monitor to be placed somewhere around the computer to display stats
  6.  
  7. PlayerData = {}
  8.  
  9. local function splitStr (inputstr)
  10.     local t={}
  11.     for str in string.gmatch(inputstr, "([^%s]+)") do
  12.         table.insert(t, str)
  13.     end
  14.     return t
  15. end
  16.  
  17. local function formatSeconds(seconds)
  18.     local days = math.floor(seconds/86400)
  19.     local hours = math.floor(math.mod(seconds, 86400)/3600)
  20.     local minutes = math.floor(math.mod(seconds,3600)/60)
  21.     local seconds = math.floor(math.mod(seconds,60))
  22.     return string.format("%d:%02d:%02d:%02d",days,hours,minutes,seconds)
  23. end
  24.  
  25. local function getOnlinePlayers()
  26.     local list = {}
  27.     local success, playersStr = commands.exec("list")
  28.     playersStr[1] = string.gsub(playersStr[1], ",", "")
  29.     local playersStrTable = splitStr(playersStr[1])
  30.     local startIndex = false
  31.  
  32.     for _,p in ipairs(playersStrTable) do
  33.         if startIndex then
  34.             list[p] = p
  35.         end
  36.         if p == "online:" and not startIndex then
  37.             startIndex = true
  38.         end
  39.     end
  40.     return list
  41. end
  42.  
  43. local function removeOldPlayers()
  44.     local onlinePlayers = getOnlinePlayers()
  45.     for i,pd in pairs(PlayerData) do
  46.         if not onlinePlayers[i] then
  47.             commands.say(i.." disconnected after playing for "..formatSeconds(PlayerData[i]["session"]))
  48.             PlayerData[i] = nil
  49.         end
  50.     end
  51. end
  52. local function loadPlayerData(player)
  53.     if fs.exists(player..".txt") then
  54.         local playerFile = fs.open(player..".txt", "r")
  55.         PlayerData[player] = {};
  56.         PlayerData[player]["total"] = tonumber(playerFile.readLine())
  57.         PlayerData[player]["session"] = 0
  58.         playerFile.close()
  59.     else
  60.         PlayerData[player] = {};
  61.         PlayerData[player]["total"] = 0
  62.         PlayerData[player]["session"] = 0
  63.     end
  64. end
  65.  
  66. local function updatePlaytime()
  67.     removeOldPlayers()
  68.  
  69.     for i,p in pairs(getOnlinePlayers()) do
  70.         if not PlayerData[p] then
  71.             loadPlayerData(p)
  72.             commands.say(p.." joined with "..formatSeconds(PlayerData[p]["total"]).." total playtime")
  73.         end
  74.         PlayerData[p]["session"] = PlayerData[p]["session"]+1
  75.         PlayerData[p]["total"] = PlayerData[p]["total"]+1
  76.         local playerFile = fs.open(p..".txt", "w")
  77.         playerFile.write(PlayerData[p]["total"])
  78.         playerFile.close()
  79.     end
  80. end
  81.  
  82. local cursorY = 1
  83.  
  84. m = peripheral.find("monitor")
  85. m.setCursorPos(1,cursorY)
  86. m.clear()
  87. m.setBackgroundColor(colors.black)
  88. m.setTextColor(colors.white)
  89. m.setTextScale(1.5)
  90.  
  91.  
  92. while true do
  93.     updatePlaytime()
  94.     m.clear()
  95.     cursorY = 1
  96.  
  97.     m.setCursorPos(1,cursorY)
  98.     cursorY = cursorY+1
  99.     m.write(os.date("%b %d %G %I:%M %p"))
  100.  
  101.     for i,p in pairs(getOnlinePlayers()) do
  102.         if PlayerData[p] then
  103.             m.setCursorPos(1,cursorY)
  104.             cursorY = cursorY+1
  105.             m.write(p)
  106.             m.setCursorPos(3,cursorY)
  107.             cursorY = cursorY+1
  108.             m.write("Session: "..formatSeconds(PlayerData[p]["session"]))
  109.             m.setCursorPos(3,cursorY)
  110.             cursorY = cursorY+1
  111.             m.write("  Total: "..formatSeconds(PlayerData[p]["total"]))
  112.         end
  113.     end
  114.     sleep(1)
  115. end
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement