Initial release 1.0.0
Browse Source1.0.0 - Initial release.
4 changed files
with 246 additions and 0 deletions
A
CHANGELOG.md
+3 -0
A
README.md
+2 -0
A
addons/amxmodx/scripting/csb_playtime_tracker.sma
+205 -0
A
csb.json
+36 -0
CHANGELOG.md
A
| @@ -0,0 +1,3 @@ | ||
| 1 | +## 1.0.0 (2026-04-01) | |
| 2 | + | |
| 3 | +1.0.0 - Initial release. |
README.md
A
| @@ -0,0 +1,2 @@ | ||
| 1 | +<p><strong>CSB Playtime Tracker</strong> counts how long each SteamID has spent on your server. Time is accumulated in seconds, flushed to <strong>nVault</strong> once a minute and again on disconnect, so a crash loses at most 60 seconds. <code>say /time</code> shows a player their total hours and minutes; <code>say /toptime</code> shows a small ladder of the online players by time played.</p><p>The plugin also registers a native, <code>csb_get_playtime(id)</code>, that returns a player's live total in seconds — VIP, rewards or rank plugins can call it to unlock perks after so many hours.</p><h2>Features</h2><ul><li>Per-SteamID second-accurate accumulation, persisted in nVault.</li><li>One-minute periodic flush plus flush on disconnect and on map end.</li><li><code>say /time</code> and <code>say /toptime</code> (top 5 of the online players).</li><li><code>csb_get_playtime(id)</code> native for other plugins.</li></ul><h2>Using the native</h2><pre>native csb_get_playtime(id); | |
| 2 | +// hours = csb_get_playtime(id) / 3600;</pre><h2>Installation</h2><ol><li>Enable the <code>nvault</code> module in <code>addons/amxmodx/configs/modules.ini</code>.</li><li>Compile <code>csb_playtime_tracker.sma</code>, copy it to <code>addons/amxmodx/plugins/</code> and add it to <code>plugins.ini</code>. Load it <em>before</em> any plugin that calls its native.</li><li>Change the map. The vault file <code>csb_playtime.vault</code> appears in <code>addons/amxmodx/data/vault/</code>.</li></ol> |
addons/amxmodx/scripting/csb_playtime_tracker.sma
A
| @@ -0,0 +1,205 @@ | ||
| 1 | +/* | |
| 2 | + * CSB Playtime Tracker | |
| 3 | + * Copyright (C) 2026 counter-strike-boost.com | |
| 4 | + * | |
| 5 | + * Accumulates connected seconds per SteamID in nVault (flushed every minute and | |
| 6 | + * on disconnect), prints total hours with say /time and a say /toptime ladder, | |
| 7 | + * and exposes a native csb_get_playtime(id) that the VIP / rewards plugins can | |
| 8 | + * read to gate perks by hours played. | |
| 9 | + * | |
| 10 | + * This program is free software: you can redistribute it and/or modify it under | |
| 11 | + * the terms of the GNU General Public License as published by the Free Software | |
| 12 | + * Foundation, either version 3 of the License, or (at your option) any later | |
| 13 | + * version. It is distributed in the hope that it will be useful, but WITHOUT | |
| 14 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 15 | + * FOR A PARTICULAR PURPOSE. See <https://www.gnu.org/licenses/> for details. | |
| 16 | + */ | |
| 17 | + | |
| 18 | +#include <amxmodx> | |
| 19 | +#include <amxmisc> | |
| 20 | +#include <nvault> | |
| 21 | + | |
| 22 | +new const PLUGIN[] = "CSB Playtime Tracker" | |
| 23 | +new const VERSION[] = "1.0.0" | |
| 24 | +new const AUTHOR[] = "counter-strike-boost.com" | |
| 25 | + | |
| 26 | +#define NO_VAULT (-1) | |
| 27 | + | |
| 28 | +new g_iVault = NO_VAULT | |
| 29 | + | |
| 30 | +new g_iTotal[33] /* total accumulated seconds, loaded from the vault */ | |
| 31 | +new g_iSince[33] /* systime of the last flush / connect */ | |
| 32 | +new g_szAuth[33][35] | |
| 33 | +new g_szName[33][32] | |
| 34 | + | |
| 35 | +public plugin_init() | |
| 36 | +{ | |
| 37 | + register_plugin(PLUGIN, VERSION, AUTHOR) | |
| 38 | + | |
| 39 | + register_clcmd("say /time", "cmdTime") | |
| 40 | + register_clcmd("say_team /time", "cmdTime") | |
| 41 | + register_clcmd("say /toptime", "cmdTopTime") | |
| 42 | + register_clcmd("say_team /toptime", "cmdTopTime") | |
| 43 | + | |
| 44 | + set_task(60.0, "taskFlush", 0, _, _, "b") | |
| 45 | +} | |
| 46 | + | |
| 47 | +public plugin_natives() | |
| 48 | +{ | |
| 49 | + register_native("csb_get_playtime", "native_get_playtime") | |
| 50 | +} | |
| 51 | + | |
| 52 | +public plugin_cfg() | |
| 53 | +{ | |
| 54 | + g_iVault = nvault_open("csb_playtime") | |
| 55 | + | |
| 56 | + if (g_iVault == NO_VAULT) | |
| 57 | + log_amx("[CSB Playtime] could not open nVault 'csb_playtime' - tracking disabled.") | |
| 58 | +} | |
| 59 | + | |
| 60 | +public plugin_end() | |
| 61 | +{ | |
| 62 | + new players[32], num | |
| 63 | + get_players(players, num, "c") | |
| 64 | + | |
| 65 | + for (new i = 0; i < num; i++) | |
| 66 | + flushPlayer(players[i]) | |
| 67 | + | |
| 68 | + if (g_iVault != NO_VAULT) | |
| 69 | + nvault_close(g_iVault) | |
| 70 | +} | |
| 71 | + | |
| 72 | +public client_authorized(id) | |
| 73 | +{ | |
| 74 | + g_iTotal[id] = 0 | |
| 75 | + g_iSince[id] = get_systime() | |
| 76 | + g_szAuth[id][0] = 0 | |
| 77 | + g_szName[id][0] = 0 | |
| 78 | + | |
| 79 | + if (is_user_bot(id) || is_user_hltv(id)) | |
| 80 | + return | |
| 81 | + | |
| 82 | + get_user_authid(id, g_szAuth[id], charsmax(g_szAuth[])) | |
| 83 | + get_user_name(id, g_szName[id], charsmax(g_szName[])) | |
| 84 | + | |
| 85 | + if (g_iVault == NO_VAULT) | |
| 86 | + return | |
| 87 | + | |
| 88 | + new value[16] | |
| 89 | + | |
| 90 | + if (nvault_get(g_iVault, g_szAuth[id], value, charsmax(value))) | |
| 91 | + g_iTotal[id] = str_to_num(value) | |
| 92 | +} | |
| 93 | + | |
| 94 | +public client_disconnected(id) | |
| 95 | +{ | |
| 96 | + flushPlayer(id) | |
| 97 | + g_iSince[id] = 0 | |
| 98 | +} | |
| 99 | + | |
| 100 | +flushPlayer(id) | |
| 101 | +{ | |
| 102 | + if (!g_szAuth[id][0]) | |
| 103 | + return | |
| 104 | + | |
| 105 | + new now = get_systime() | |
| 106 | + | |
| 107 | + if (g_iSince[id] > 0 && now > g_iSince[id]) | |
| 108 | + { | |
| 109 | + g_iTotal[id] += (now - g_iSince[id]) | |
| 110 | + g_iSince[id] = now | |
| 111 | + } | |
| 112 | + | |
| 113 | + if (g_iVault != NO_VAULT) | |
| 114 | + { | |
| 115 | + new value[16] | |
| 116 | + num_to_str(g_iTotal[id], value, charsmax(value)) | |
| 117 | + nvault_pset(g_iVault, g_szAuth[id], value) | |
| 118 | + } | |
| 119 | +} | |
| 120 | + | |
| 121 | +public taskFlush() | |
| 122 | +{ | |
| 123 | + new players[32], num | |
| 124 | + get_players(players, num, "c") | |
| 125 | + | |
| 126 | + for (new i = 0; i < num; i++) | |
| 127 | + flushPlayer(players[i]) | |
| 128 | +} | |
| 129 | + | |
| 130 | +seconds(id) | |
| 131 | +{ | |
| 132 | + new extra = 0 | |
| 133 | + | |
| 134 | + if (g_iSince[id] > 0) | |
| 135 | + extra = get_systime() - g_iSince[id] | |
| 136 | + | |
| 137 | + return g_iTotal[id] + extra | |
| 138 | +} | |
| 139 | + | |
| 140 | +public cmdTime(id) | |
| 141 | +{ | |
| 142 | + new secs = seconds(id) | |
| 143 | + new hours = secs / 3600 | |
| 144 | + new mins = (secs % 3600) / 60 | |
| 145 | + | |
| 146 | + client_print(id, print_chat, "[CSB] You have played %d hour(s) and %d minute(s) on this server.", hours, mins) | |
| 147 | + return PLUGIN_HANDLED | |
| 148 | +} | |
| 149 | + | |
| 150 | +public cmdTopTime(id) | |
| 151 | +{ | |
| 152 | + new players[32], num | |
| 153 | + get_players(players, num, "c") | |
| 154 | + | |
| 155 | + /* simple selection sort of the connected players by playtime */ | |
| 156 | + new order[32] | |
| 157 | + | |
| 158 | + for (new i = 0; i < num; i++) | |
| 159 | + order[i] = players[i] | |
| 160 | + | |
| 161 | + for (new i = 0; i < num; i++) | |
| 162 | + { | |
| 163 | + new best = i | |
| 164 | + | |
| 165 | + for (new j = i + 1; j < num; j++) | |
| 166 | + { | |
| 167 | + if (seconds(order[j]) > seconds(order[best])) | |
| 168 | + best = j | |
| 169 | + } | |
| 170 | + | |
| 171 | + if (best != i) | |
| 172 | + { | |
| 173 | + new tmp = order[i] | |
| 174 | + order[i] = order[best] | |
| 175 | + order[best] = tmp | |
| 176 | + } | |
| 177 | + } | |
| 178 | + | |
| 179 | + client_print(id, print_chat, "[CSB] --- Top playtime (online players) ---") | |
| 180 | + | |
| 181 | + new shown = num < 5 ? num : 5 | |
| 182 | + | |
| 183 | + for (new i = 0; i < shown; i++) | |
| 184 | + { | |
| 185 | + new pid = order[i] | |
| 186 | + new secs = seconds(pid) | |
| 187 | + new name[32] | |
| 188 | + get_user_name(pid, name, charsmax(name)) | |
| 189 | + | |
| 190 | + client_print(id, print_chat, "[CSB] %d. %s - %dh %dm", i + 1, name, secs / 3600, (secs % 3600) / 60) | |
| 191 | + } | |
| 192 | + | |
| 193 | + return PLUGIN_HANDLED | |
| 194 | +} | |
| 195 | + | |
| 196 | +/* native csb_get_playtime(id) -> total seconds played, live */ | |
| 197 | +public native_get_playtime(plugin, params) | |
| 198 | +{ | |
| 199 | + new id = get_param(1) | |
| 200 | + | |
| 201 | + if (id < 1 || id > 32 || !is_user_connected(id)) | |
| 202 | + return 0 | |
| 203 | + | |
| 204 | + return seconds(id) | |
| 205 | +} |
csb.json
A
| @@ -0,0 +1,36 @@ | ||
| 1 | +{ | |
| 2 | + "name": "CSB Playtime Tracker", | |
| 3 | + "category": "stats", | |
| 4 | + "tags": "playtime, hours, nvault", | |
| 5 | + "image": "", | |
| 6 | + "original_author": "", | |
| 7 | + "original_url": "", | |
| 8 | + "license": "GPL-3.0", | |
| 9 | + "title": "CSB Playtime Tracker", | |
| 10 | + "short_desc": "Accumulates connected time per SteamID in nVault, shows /time and /toptime, and exposes a csb_get_playtime() native.", | |
| 11 | + "version": "1.0.0", | |
| 12 | + "amxx_min": "1.9", | |
| 13 | + "amxx_compile_default": "1.9", | |
| 14 | + "rehlds_ok": true, | |
| 15 | + "hlds_ok": true, | |
| 16 | + "regamedll_required": false, | |
| 17 | + "metamod_flavor": "any", | |
| 18 | + "reapi_required": false, | |
| 19 | + "modules": "nvault", | |
| 20 | + "install_notes": "1. Enable the nvault module in addons/amxmodx/configs/modules.ini\n2. Put csb_playtime_tracker.amxx in addons/amxmodx/plugins/ and add it to plugins.ini (before plugins that call csb_get_playtime)\n3. Change map", | |
| 21 | + "known_conflicts": "", | |
| 22 | + "cvars": [], | |
| 23 | + "commands": [ | |
| 24 | + { | |
| 25 | + "name": "say /time", | |
| 26 | + "access": "", | |
| 27 | + "description": "Show your total playtime on this server." | |
| 28 | + }, | |
| 29 | + { | |
| 30 | + "name": "say /toptime", | |
| 31 | + "access": "", | |
| 32 | + "description": "Show a playtime ladder of the online players." | |
| 33 | + } | |
| 34 | + ], | |
| 35 | + "main": "addons/amxmodx/scripting/csb_playtime_tracker.sma" | |
| 36 | +} | |
| 0 | 37 |









