residentsleeper/scripts/timetracker.lua

124 lines
3.2 KiB
Lua
Raw Normal View History

local event = require('event')
local commands = require('commands')
config = require('config')
playtimes = {}
playerLogin = {}
initialized = false
function initTimeTracking()
if initialized then
return
end
initialized = true
print('Initializing time tracking')
local loadedPlaytimes, err = config.load('playtime')
if loadedPlaytimes and not err then
playtimes = loadedPlaytimes
end
online, max, names, err = rcon:OnlinePlayers()
if err ~= nil then
print('Unable to load online players: ' .. err:Error())
return
end
for index = 1, #names do
print('Setting login time for ' .. names[index])
playerLogin[names[index]] = os.time()
end
end
initTimeTracking()
event.on('init', initTimeTracking)
event.on('join', function(user)
playerLogin[user] = os.time()
end)
event.on('leave', function(user)
local timeOnline = os.time() - playerLogin[user]
if playtimes[user] ~= nil then
playtimes[user] = playtimes[user] + timeOnline
else
playtimes[user] = timeOnline
end
playerLogin[user] = nil
err = config.save('playtime', playtimes)
if err ~= nil then
print('Unable to save playtimes')
end
end)
commands.register('playtime [name]', function(user, name)
if name == nil then
name = user
end
print('User ' .. user .. ' is checking playtime of ' .. name)
if playtimes[name] == nil and playerLogin[name] == nil then
rcon:SendMessage(user, 'User has not logged any playtime.')
return
end
local playTime = playtimes[name] or 0
if playerLogin[name] ~= nil then
local timeOnline = os.time() - playerLogin[name]
playTime = playTime + timeOnline
end
print('Time played: ' .. playTime)
rcon:SendColorfulMessage('@a', 'green', name .. ' has played for ' .. formatElapsed(playTime))
end)
function formatElapsed(elapsedSeconds)
local weeks, days, hours, minutes, seconds = formatSeconds(elapsedSeconds)
local weeksTxt, daysTxt, hoursTxt, minutesTxt, secondsTxt = ""
if weeks == 1 then weeksTxt = 'week' else weeksTxt = 'weeks' end
if days == 1 then daysTxt = 'day' else daysTxt = 'days' end
if hours == 1 then hoursTxt = 'hour' else hoursTxt = 'hours' end
if minutes == 1 then minutesTxt = 'minute' else minutesTxt = 'minutes' end
if seconds == 1 then secondsTxt = 'second' else secondsTxt = 'seconds' end
if elapsedSeconds >= 604800 then
return weeks..' '..weeksTxt..', '..days..' '..daysTxt..', '..hours..' '..hoursTxt..', '..minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
elseif elapsedSeconds >= 86400 then
return days..' '..daysTxt..', '..hours..' '..hoursTxt..', '..minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
elseif elapsedSeconds >= 3600 then
return hours..' '..hoursTxt..', '..minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
elseif elapsedSeconds >= 60 then
return minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
else
return seconds..' '..secondsTxt
end
end
function formatSeconds(secondsArg)
local weeks = math.floor(secondsArg / 604800)
local remainder = secondsArg % 604800
local days = math.floor(remainder / 86400)
local remainder = remainder % 86400
local hours = math.floor(remainder / 3600)
local remainder = remainder % 3600
local minutes = math.floor(remainder / 60)
local seconds = remainder % 60
return weeks, days, hours, minutes, seconds
end