cnl_cnl_cnl

now you're sorry you said anything

Aug 22nd, 2025
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.91 KB | None | 0 0
  1.  
  2. -- only run if we know the PC name
  3. if not m or not m.pc_name then return end
  4.  
  5. cnl_chars = cnl_chars or {}
  6. cnl_chars.afk = cnl_chars.afk or {}
  7.  
  8. -- detect new login (pc_name change)
  9. if cnl_chars.afk.pc_name ~= m.pc_name then
  10.   cnl_chars.afk = { pc_name = m.pc_name }
  11. end
  12.  
  13. local c = cnl_chars.afk
  14. local now = getEpoch()
  15. local idle = now - (m.conn_cmd_ts or now) -- default now if nil
  16. local x = 3.5 * 60  -- idle threshold
  17.  
  18. -- initialize if needed
  19. c.start              = c.start or now
  20. c.state              = c.state or "active"
  21. c.last_cmd           = c.last_cmd or (m.conn_cmd_ts or now)
  22. c.last_state_change  = c.last_state_change or now
  23. c.total_afk          = c.total_afk or 0
  24. c.total_active       = c.total_active or 0
  25.  
  26. -- helper: close current segment
  27. local function close_segment(new_state)
  28.   local seg_time = now - c.last_state_change
  29.   if c.state == "active" then
  30.     c.total_active = c.total_active + seg_time
  31.   elseif c.state == "afk" then
  32.     c.total_afk = c.total_afk + seg_time
  33.   end
  34.   c.state = new_state
  35.   c.last_state_change = now
  36. end
  37.  
  38. -- detect transitions
  39. if c.state == "active" and idle > x then
  40.   cecho("<red>AFK started!\n")
  41.   close_segment("afk")
  42. elseif c.state == "afk" and m.conn_cmd_ts > c.last_cmd then
  43.   cecho("<green>AFK ended!\n")
  44.   close_segment("active")
  45. end
  46.  
  47. -- update last_cmd if new command arrived
  48. if m.conn_cmd_ts > c.last_cmd then
  49.   c.last_cmd = m.conn_cmd_ts
  50. end
  51.  
  52. -- totals (include current open segment)
  53. local total_time = now - c.start
  54. local active_time, afk_time = c.total_active, c.total_afk
  55. if c.state == "active" then
  56.   active_time = active_time + (now - c.last_state_change)
  57. elseif c.state == "afk" then
  58.   afk_time = afk_time + (now - c.last_state_change)
  59. end
  60.  
  61. local percent_afk = (afk_time / total_time) * 100
  62.  
  63. -- optional display while AFK
  64. if c.state == "afk" then
  65.   cecho(string.format("<ansi_cyan>[%s: %.1f%% AFK]\n", m.pc_name, percent_afk))
  66. end
  67.  
Advertisement
Add Comment
Please, Sign In to add comment