Initial release 1.0.0
Browse Source1.0.0 - Initial release.
4 changed files
with 299 additions and 0 deletions
A
CHANGELOG.md
+3 -0
A
README.md
+1 -0
A
addons/amxmodx/scripting/csb_match_stats_export.sma
+238 -0
A
csb.json
+57 -0
CHANGELOG.md
A
| @@ -0,0 +1,3 @@ | ||
| 1 | +## 1.0.0 (2026-02-08) | |
| 2 | + | |
| 3 | +1.0.0 - Initial release. |
README.md
A
| @@ -0,0 +1 @@ | ||
| 1 | +<p><strong>CSB Match Stats Export</strong> writes a complete match scoreboard so a website can render the match page. It tracks per-player kills, deaths, headshots and damage (ADR = damage / rounds), and on <code>amx_exportstats</code> inserts one row per player into MySQL with fully threaded SQLx under a numeric match id. If the database is not configured it falls back to a JSON-ish text file under <code>addons/amxmodx/logs</code>.</p><h2>Features</h2><ul><li>Per-player kills, deaths, headshots, damage and ADR.</li><li>Threaded SQLx insert - the game thread is never blocked.</li><li>Numeric match id groups the rows for the website.</li><li>Text-file fallback when the DB is not configured.</li><li><code>amx_resetstats</code> to clear tracking between matches.</li></ul><h2>Installation</h2><ol><li>Enable the <code>sqlx</code> and <code>hamsandwich</code> modules in <code>modules.ini</code>.</li><li>Set the <code>csb_export_*</code> cvars to your database credentials.</li><li>Compile <code>csb_match_stats_export.sma</code>, copy it to <code>addons/amxmodx/plugins/</code> and add it to <code>plugins.ini</code>.</li><li>Change the map.</li></ol> |
addons/amxmodx/scripting/csb_match_stats_export.sma
A
| @@ -0,0 +1,238 @@ | ||
| 1 | +/* | |
| 2 | + * CSB Match Stats Export | |
| 3 | + * Copyright (C) 2026 counter-strike-boost.com | |
| 4 | + * | |
| 5 | + * At the end of a match it writes the full scoreboard (per-player kills, deaths, | |
| 6 | + * headshots, damage and ADR) into MySQL with threaded SQLx and a numeric match | |
| 7 | + * id, so a website can render the match page. If the database is not configured | |
| 8 | + * it falls back to a JSON-ish text file under addons/amxmodx/logs. | |
| 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 <hamsandwich> | |
| 21 | +#include <sqlx> | |
| 22 | + | |
| 23 | +new const PLUGIN[] = "CSB Match Stats Export" | |
| 24 | +new const VERSION[] = "1.0.0" | |
| 25 | +new const AUTHOR[] = "counter-strike-boost.com" | |
| 26 | + | |
| 27 | +new const TABLE[] = "csb_match_players" | |
| 28 | + | |
| 29 | +new g_pHost, g_pUser, g_pPass, g_pDb | |
| 30 | +new Handle:g_hTuple = Empty_Handle | |
| 31 | + | |
| 32 | +new g_iKills[33], g_iDeaths[33], g_iHs[33], g_iDamage[33] | |
| 33 | +new g_iRounds | |
| 34 | +new g_szAuth[33][35] | |
| 35 | + | |
| 36 | +public plugin_init() | |
| 37 | +{ | |
| 38 | + register_plugin(PLUGIN, VERSION, AUTHOR) | |
| 39 | + | |
| 40 | + g_pHost = register_cvar("csb_export_host", "127.0.0.1") | |
| 41 | + g_pUser = register_cvar("csb_export_user", "csb") | |
| 42 | + g_pPass = register_cvar("csb_export_pass", "") | |
| 43 | + g_pDb = register_cvar("csb_export_db", "csb") | |
| 44 | + | |
| 45 | + register_event("DeathMsg", "evDeath", "a") | |
| 46 | + register_logevent("evRoundStart", 2, "1=Round_Start") | |
| 47 | + RegisterHam(Ham_TakeDamage, "player", "fwDamage") | |
| 48 | + | |
| 49 | + register_concmd("amx_exportstats", "cmdExport", ADMIN_BAN, "- export the current match scoreboard") | |
| 50 | + register_concmd("amx_resetstats", "cmdReset", ADMIN_BAN, "- clear tracked match stats") | |
| 51 | +} | |
| 52 | + | |
| 53 | +public plugin_cfg() | |
| 54 | +{ | |
| 55 | + new host[64], user[64], pass[64], db[64] | |
| 56 | + get_pcvar_string(g_pHost, host, charsmax(host)) | |
| 57 | + get_pcvar_string(g_pUser, user, charsmax(user)) | |
| 58 | + get_pcvar_string(g_pPass, pass, charsmax(pass)) | |
| 59 | + get_pcvar_string(g_pDb, db, charsmax(db)) | |
| 60 | + | |
| 61 | + if (host[0] && db[0]) | |
| 62 | + { | |
| 63 | + g_hTuple = SQL_MakeDbTuple(host, user, pass, db) | |
| 64 | + | |
| 65 | + new q[640] | |
| 66 | + formatex(q, charsmax(q), "CREATE TABLE IF NOT EXISTS `%s` (`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `match_id` INT UNSIGNED NOT NULL, `map` VARCHAR(32) NOT NULL DEFAULT '', `authid` VARCHAR(34) NOT NULL DEFAULT '', `name` VARCHAR(32) NOT NULL DEFAULT '', `kills` INT NOT NULL DEFAULT 0, `deaths` INT NOT NULL DEFAULT 0, `hs` INT NOT NULL DEFAULT 0, `damage` INT NOT NULL DEFAULT 0, `adr` INT NOT NULL DEFAULT 0, KEY `k_match` (`match_id`)) ENGINE=InnoDB", TABLE) | |
| 67 | + | |
| 68 | + SQL_ThreadQuery(g_hTuple, "handlerSimple", q) | |
| 69 | + } | |
| 70 | +} | |
| 71 | + | |
| 72 | +public plugin_end() | |
| 73 | +{ | |
| 74 | + if (g_hTuple != Empty_Handle) | |
| 75 | + SQL_FreeHandle(g_hTuple) | |
| 76 | +} | |
| 77 | + | |
| 78 | +public client_authorized(id) | |
| 79 | +{ | |
| 80 | + get_user_authid(id, g_szAuth[id], charsmax(g_szAuth[])) | |
| 81 | +} | |
| 82 | + | |
| 83 | +public client_putinserver(id) | |
| 84 | +{ | |
| 85 | + g_iKills[id] = 0 | |
| 86 | + g_iDeaths[id] = 0 | |
| 87 | + g_iHs[id] = 0 | |
| 88 | + g_iDamage[id] = 0 | |
| 89 | +} | |
| 90 | + | |
| 91 | +public evRoundStart() | |
| 92 | +{ | |
| 93 | + g_iRounds++ | |
| 94 | +} | |
| 95 | + | |
| 96 | +public evDeath() | |
| 97 | +{ | |
| 98 | + new killer = read_data(1) | |
| 99 | + new victim = read_data(2) | |
| 100 | + new hs = read_data(3) | |
| 101 | + | |
| 102 | + if (victim >= 1 && victim <= 32) | |
| 103 | + g_iDeaths[victim]++ | |
| 104 | + | |
| 105 | + if (killer >= 1 && killer <= 32 && killer != victim) | |
| 106 | + { | |
| 107 | + g_iKills[killer]++ | |
| 108 | + if (hs) | |
| 109 | + g_iHs[killer]++ | |
| 110 | + } | |
| 111 | +} | |
| 112 | + | |
| 113 | +public fwDamage(victim, inflictor, attacker, Float:damage, damagebits) | |
| 114 | +{ | |
| 115 | + if (attacker >= 1 && attacker <= 32 && attacker != victim) | |
| 116 | + g_iDamage[attacker] += floatround(damage) | |
| 117 | + | |
| 118 | + return HAM_IGNORED | |
| 119 | +} | |
| 120 | + | |
| 121 | +public cmdReset(id, level, cid) | |
| 122 | +{ | |
| 123 | + if (!cmd_access(id, level, cid, 1)) | |
| 124 | + return PLUGIN_HANDLED | |
| 125 | + | |
| 126 | + for (new i = 1; i <= 32; i++) | |
| 127 | + { | |
| 128 | + g_iKills[i] = 0 | |
| 129 | + g_iDeaths[i] = 0 | |
| 130 | + g_iHs[i] = 0 | |
| 131 | + g_iDamage[i] = 0 | |
| 132 | + } | |
| 133 | + g_iRounds = 0 | |
| 134 | + | |
| 135 | + console_print(id, "[CSB] Match stats cleared.") | |
| 136 | + return PLUGIN_HANDLED | |
| 137 | +} | |
| 138 | + | |
| 139 | +public cmdExport(id, level, cid) | |
| 140 | +{ | |
| 141 | + if (!cmd_access(id, level, cid, 1)) | |
| 142 | + return PLUGIN_HANDLED | |
| 143 | + | |
| 144 | + new matchId = get_systime() | |
| 145 | + new rounds = g_iRounds > 0 ? g_iRounds : 1 | |
| 146 | + | |
| 147 | + new mapname[32] | |
| 148 | + get_mapname(mapname, charsmax(mapname)) | |
| 149 | + | |
| 150 | + new players[32], num | |
| 151 | + get_players(players, num, "ch") | |
| 152 | + | |
| 153 | + if (g_hTuple == Empty_Handle) | |
| 154 | + { | |
| 155 | + exportFile(matchId, mapname, players, num, rounds) | |
| 156 | + console_print(id, "[CSB] DB not configured; wrote match %d to a text file.", matchId) | |
| 157 | + return PLUGIN_HANDLED | |
| 158 | + } | |
| 159 | + | |
| 160 | + new safeMap[70] | |
| 161 | + sqlEscape(mapname, safeMap, charsmax(safeMap)) | |
| 162 | + | |
| 163 | + for (new i = 0; i < num; i++) | |
| 164 | + { | |
| 165 | + new pid = players[i] | |
| 166 | + new adr = g_iDamage[pid] / rounds | |
| 167 | + | |
| 168 | + new name[32], safeName[70], safeAuth[70] | |
| 169 | + get_user_name(pid, name, charsmax(name)) | |
| 170 | + sqlEscape(name, safeName, charsmax(safeName)) | |
| 171 | + sqlEscape(g_szAuth[pid], safeAuth, charsmax(safeAuth)) | |
| 172 | + | |
| 173 | + new q[600] | |
| 174 | + formatex(q, charsmax(q), "INSERT INTO `%s` (`match_id`, `map`, `authid`, `name`, `kills`, `deaths`, `hs`, `damage`, `adr`) VALUES (%d, '%s', '%s', '%s', %d, %d, %d, %d, %d)", | |
| 175 | + TABLE, matchId, safeMap, safeAuth, safeName, | |
| 176 | + g_iKills[pid], g_iDeaths[pid], g_iHs[pid], g_iDamage[pid], adr) | |
| 177 | + | |
| 178 | + SQL_ThreadQuery(g_hTuple, "handlerSimple", q) | |
| 179 | + } | |
| 180 | + | |
| 181 | + console_print(id, "[CSB] Exported %d players for match id %d.", num, matchId) | |
| 182 | + client_print(0, print_chat, "[CSB] Match scoreboard exported (id %d).", matchId) | |
| 183 | + return PLUGIN_HANDLED | |
| 184 | +} | |
| 185 | + | |
| 186 | +exportFile(matchId, const mapname[], players[], num, rounds) | |
| 187 | +{ | |
| 188 | + new path[128] | |
| 189 | + formatex(path, charsmax(path), "addons/amxmodx/logs/csb_match_%d.txt", matchId) | |
| 190 | + | |
| 191 | + new fp = fopen(path, "wt") | |
| 192 | + if (!fp) | |
| 193 | + return | |
| 194 | + | |
| 195 | + fprintf(fp, "{^n ^"match_id^": %d,^n ^"map^": ^"%s^",^n ^"players^": [^n", matchId, mapname) | |
| 196 | + | |
| 197 | + for (new i = 0; i < num; i++) | |
| 198 | + { | |
| 199 | + new pid = players[i] | |
| 200 | + new adr = g_iDamage[pid] / rounds | |
| 201 | + new name[32] | |
| 202 | + get_user_name(pid, name, charsmax(name)) | |
| 203 | + | |
| 204 | + fprintf(fp, " {^"name^": ^"%s^", ^"authid^": ^"%s^", ^"kills^": %d, ^"deaths^": %d, ^"hs^": %d, ^"damage^": %d, ^"adr^": %d}%s^n", | |
| 205 | + name, g_szAuth[pid], g_iKills[pid], g_iDeaths[pid], g_iHs[pid], g_iDamage[pid], adr, | |
| 206 | + (i < num - 1) ? "," : "") | |
| 207 | + } | |
| 208 | + | |
| 209 | + fprintf(fp, " ]^n}^n") | |
| 210 | + fclose(fp) | |
| 211 | +} | |
| 212 | + | |
| 213 | +public handlerSimple(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime) | |
| 214 | +{ | |
| 215 | + if (failstate != TQUERY_SUCCESS) | |
| 216 | + log_amx("[CSB Export] query failed (%d): %s", errnum, error) | |
| 217 | +} | |
| 218 | + | |
| 219 | +stock sqlEscape(const input[], output[], len) | |
| 220 | +{ | |
| 221 | + new i = 0, j = 0, c | |
| 222 | + | |
| 223 | + while (input[i] != 0 && j < len) | |
| 224 | + { | |
| 225 | + c = input[i] | |
| 226 | + | |
| 227 | + if (c == 39 || c == 34 || c == 92 || c == 59 || c == 37 || c == 96 || c < 32) | |
| 228 | + { | |
| 229 | + i++ | |
| 230 | + continue | |
| 231 | + } | |
| 232 | + | |
| 233 | + output[j++] = c | |
| 234 | + i++ | |
| 235 | + } | |
| 236 | + | |
| 237 | + output[j] = 0 | |
| 238 | +} |
csb.json
A
| @@ -0,0 +1,57 @@ | ||
| 1 | +{ | |
| 2 | + "name": "CSB Match Stats Export", | |
| 3 | + "category": "match", | |
| 4 | + "tags": "export, mysql, match", | |
| 5 | + "image": "", | |
| 6 | + "original_author": "", | |
| 7 | + "original_url": "", | |
| 8 | + "license": "GPL-3.0", | |
| 9 | + "title": "CSB Match Stats Export", | |
| 10 | + "short_desc": "Exports the full match scoreboard (kills/deaths/hs/damage/ADR per player) to MySQL with threaded SQLx, or to a text file when the DB is down.", | |
| 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": "hamsandwich,sqlx", | |
| 20 | + "install_notes": "1. Enable the sqlx and hamsandwich modules in addons/amxmodx/configs/modules.ini\n2. Set the csb_export_* cvars to your DB credentials\n3. Put csb_match_stats_export.amxx in addons/amxmodx/plugins/ and add it to plugins.ini\n4. Change map", | |
| 21 | + "known_conflicts": "Other stats plugins hooking Ham_TakeDamage or DeathMsg track their own numbers independently.", | |
| 22 | + "cvars": [ | |
| 23 | + { | |
| 24 | + "name": "csb_export_host", | |
| 25 | + "default": "127.0.0.1", | |
| 26 | + "description": "MySQL host." | |
| 27 | + }, | |
| 28 | + { | |
| 29 | + "name": "csb_export_user", | |
| 30 | + "default": "csb", | |
| 31 | + "description": "MySQL user." | |
| 32 | + }, | |
| 33 | + { | |
| 34 | + "name": "csb_export_pass", | |
| 35 | + "default": "", | |
| 36 | + "description": "MySQL password." | |
| 37 | + }, | |
| 38 | + { | |
| 39 | + "name": "csb_export_db", | |
| 40 | + "default": "csb", | |
| 41 | + "description": "MySQL database name." | |
| 42 | + } | |
| 43 | + ], | |
| 44 | + "commands": [ | |
| 45 | + { | |
| 46 | + "name": "amx_exportstats", | |
| 47 | + "access": "d (ADMIN_BAN)", | |
| 48 | + "description": "Export the current match scoreboard." | |
| 49 | + }, | |
| 50 | + { | |
| 51 | + "name": "amx_resetstats", | |
| 52 | + "access": "d (ADMIN_BAN)", | |
| 53 | + "description": "Clear the tracked match stats." | |
| 54 | + } | |
| 55 | + ], | |
| 56 | + "main": "addons/amxmodx/scripting/csb_match_stats_export.sma" | |
| 57 | +} | |
| 0 | 58 |









