Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- only run if we know the PC name
- if not m or not m.pc_name then return end
- cnl_chars = cnl_chars or {}
- cnl_chars.afk = cnl_chars.afk or {}
- -- detect new login (pc_name change)
- if cnl_chars.afk.pc_name ~= m.pc_name then
- cnl_chars.afk = { pc_name = m.pc_name }
- end
- local c = cnl_chars.afk
- local now = getEpoch()
- local idle = now - (m.conn_cmd_ts or now) -- default now if nil
- local x = 3.5 * 60 -- idle threshold
- -- initialize if needed
- c.start = c.start or now
- c.state = c.state or "active"
- c.last_cmd = c.last_cmd or (m.conn_cmd_ts or now)
- c.last_state_change = c.last_state_change or now
- c.total_afk = c.total_afk or 0
- c.total_active = c.total_active or 0
- -- helper: close current segment
- local function close_segment(new_state)
- local seg_time = now - c.last_state_change
- if c.state == "active" then
- c.total_active = c.total_active + seg_time
- elseif c.state == "afk" then
- c.total_afk = c.total_afk + seg_time
- end
- c.state = new_state
- c.last_state_change = now
- end
- -- detect transitions
- if c.state == "active" and idle > x then
- cecho("<red>AFK started!\n")
- close_segment("afk")
- elseif c.state == "afk" and m.conn_cmd_ts > c.last_cmd then
- cecho("<green>AFK ended!\n")
- close_segment("active")
- end
- -- update last_cmd if new command arrived
- if m.conn_cmd_ts > c.last_cmd then
- c.last_cmd = m.conn_cmd_ts
- end
- -- totals (include current open segment)
- local total_time = now - c.start
- local active_time, afk_time = c.total_active, c.total_afk
- if c.state == "active" then
- active_time = active_time + (now - c.last_state_change)
- elseif c.state == "afk" then
- afk_time = afk_time + (now - c.last_state_change)
- end
- local percent_afk = (afk_time / total_time) * 100
- -- optional display while AFK
- if c.state == "afk" then
- cecho(string.format("<ansi_cyan>[%s: %.1f%% AFK]\n", m.pc_name, percent_afk))
- end
Advertisement
Add Comment
Please, Sign In to add comment