Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Run on a computer craft command computer
- -- Will track the play time of players as they connect saving each players time to their own file
- -- Will also display in chat players total time upon connecting and session time upon disconnecting
- -- Uses vanilla command /list to get current online players so no peripherals needed
- -- It will expect a monitor to be placed somewhere around the computer to display stats
- PlayerData = {}
- local function splitStr (inputstr)
- local t={}
- for str in string.gmatch(inputstr, "([^%s]+)") do
- table.insert(t, str)
- end
- return t
- end
- local function formatSeconds(seconds)
- local days = math.floor(seconds/86400)
- local hours = math.floor(math.mod(seconds, 86400)/3600)
- local minutes = math.floor(math.mod(seconds,3600)/60)
- local seconds = math.floor(math.mod(seconds,60))
- return string.format("%d:%02d:%02d:%02d",days,hours,minutes,seconds)
- end
- local function getOnlinePlayers()
- local list = {}
- local success, playersStr = commands.exec("list")
- playersStr[1] = string.gsub(playersStr[1], ",", "")
- local playersStrTable = splitStr(playersStr[1])
- local startIndex = false
- for _,p in ipairs(playersStrTable) do
- if startIndex then
- list[p] = p
- end
- if p == "online:" and not startIndex then
- startIndex = true
- end
- end
- return list
- end
- local function removeOldPlayers()
- local onlinePlayers = getOnlinePlayers()
- for i,pd in pairs(PlayerData) do
- if not onlinePlayers[i] then
- commands.say(i.." disconnected after playing for "..formatSeconds(PlayerData[i]["session"]))
- PlayerData[i] = nil
- end
- end
- end
- local function loadPlayerData(player)
- if fs.exists(player..".txt") then
- local playerFile = fs.open(player..".txt", "r")
- PlayerData[player] = {};
- PlayerData[player]["total"] = tonumber(playerFile.readLine())
- PlayerData[player]["session"] = 0
- playerFile.close()
- else
- PlayerData[player] = {};
- PlayerData[player]["total"] = 0
- PlayerData[player]["session"] = 0
- end
- end
- local function updatePlaytime()
- removeOldPlayers()
- for i,p in pairs(getOnlinePlayers()) do
- if not PlayerData[p] then
- loadPlayerData(p)
- commands.say(p.." joined with "..formatSeconds(PlayerData[p]["total"]).." total playtime")
- end
- PlayerData[p]["session"] = PlayerData[p]["session"]+1
- PlayerData[p]["total"] = PlayerData[p]["total"]+1
- local playerFile = fs.open(p..".txt", "w")
- playerFile.write(PlayerData[p]["total"])
- playerFile.close()
- end
- end
- local cursorY = 1
- m = peripheral.find("monitor")
- m.setCursorPos(1,cursorY)
- m.clear()
- m.setBackgroundColor(colors.black)
- m.setTextColor(colors.white)
- m.setTextScale(1.5)
- while true do
- updatePlaytime()
- m.clear()
- cursorY = 1
- m.setCursorPos(1,cursorY)
- cursorY = cursorY+1
- m.write(os.date("%b %d %G %I:%M %p"))
- for i,p in pairs(getOnlinePlayers()) do
- if PlayerData[p] then
- m.setCursorPos(1,cursorY)
- cursorY = cursorY+1
- m.write(p)
- m.setCursorPos(3,cursorY)
- cursorY = cursorY+1
- m.write("Session: "..formatSeconds(PlayerData[p]["session"]))
- m.setCursorPos(3,cursorY)
- cursorY = cursorY+1
- m.write(" Total: "..formatSeconds(PlayerData[p]["total"]))
- end
- end
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement