Promote server

Initial release 1.0.0

Browse Source
1.0.0 - Initial release.
Daemon666 committed commit aae0ba6715c4a825eba6fe0f291fd0b79e8b0bcb
5 changed files with 406 additions and 0 deletions
+3 -0 CHANGELOG.md A
@@ -0,0 +1,3 @@
1 +## 1.0.0 (2026-05-24)
2 +
3 +1.0.0 - Initial release.
+1 -0 README.md A
@@ -0,0 +1 @@
1 +<p><strong>CSB MySQL Ranks</strong> uses the same statistics model as the nVault rank plugin but stores everything in MySQL with fully threaded SQLx queries, so the game thread is never blocked. Stats are loaded with an async <code>SELECT</code> on authorise, written back with an async <code>UPSERT</code> on disconnect and at map end, and the player's ladder position is resolved with a <code>COUNT(*)</code> query against a stored score column. Several servers can share one ladder and the website can read the same table.</p><p>A clean-room GPL re-implementation inspired by MySQL rank systems such as PsychoStats; it ships its own tiny schema.</p><h2>Features</h2><ul><li>Threaded SQLx (SQL_MakeDbTuple / SQL_ThreadQuery) - no game-thread stalls</li><li>Async UPSERT on disconnect and at map end</li><li>Exact ladder position via a stored <code>score</code> column and <code>COUNT(*)</code></li><li>Shared ladder across servers and readable by the website</li><li>Ships <code>csb_ranks.sql</code>; the table is also created automatically</li></ul><h2>Installation</h2><ol><li>Put <code>csb_rank_mysql.amxx</code> in <code>addons/amxmodx/plugins/</code></li><li>Add it to <code>plugins.ini</code></li><li>Set the <code>csb_rank_*</code> database cvars in an amxx config</li><li>Optionally import <code>csb_ranks.sql</code></li></ol>
+25 -0 addons/amxmodx/configs/sql/csb_ranks.sql A
@@ -0,0 +1,25 @@
1 +-- CSB MySQL Ranks - schema
2 +-- The plugin creates this table automatically on first run; this file is
3 +-- provided so you can create it by hand, grant a restricted user, or let the
4 +-- website read the same ladder.
5 +
6 +CREATE TABLE IF NOT EXISTS `csb_ranks` (
7 + `authid` VARCHAR(34) NOT NULL,
8 + `name` VARCHAR(32) NOT NULL DEFAULT '',
9 + `kills` INT UNSIGNED NOT NULL DEFAULT 0,
10 + `deaths` INT UNSIGNED NOT NULL DEFAULT 0,
11 + `hs` INT UNSIGNED NOT NULL DEFAULT 0,
12 + `damage` INT UNSIGNED NOT NULL DEFAULT 0,
13 + `rounds` INT UNSIGNED NOT NULL DEFAULT 0,
14 + `playtime` INT UNSIGNED NOT NULL DEFAULT 0,
15 + `score` INT NOT NULL DEFAULT 0,
16 + `last_seen` INT UNSIGNED NOT NULL DEFAULT 0,
17 + PRIMARY KEY (`authid`),
18 + KEY `k_score` (`score`)
19 +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
20 +
21 +-- Top 15 ladder the website can query directly:
22 +-- SELECT name, kills, deaths, hs, score
23 +-- FROM csb_ranks
24 +-- ORDER BY score DESC
25 +-- LIMIT 15;
+320 -0 addons/amxmodx/scripting/csb_rank_mysql.sma A
@@ -0,0 +1,320 @@
1 +/*
2 + * CSB MySQL Ranks
3 + * Copyright (C) 2026 counter-strike-boost.com
4 + *
5 + * The same statistics model as the nVault rank plugin, but stored in MySQL with
6 + * fully threaded SQLx queries so the game thread is never blocked. Stats are
7 + * loaded with an async SELECT on authorise, written back with an async UPSERT
8 + * on disconnect and at map end, and the player's ladder position is resolved
9 + * with a COUNT(*) query against a stored score column. Several servers can
10 + * therefore share one ladder and the website can read the same table.
11 + *
12 + * Inspired by MySQL rank systems such as PsychoStats. This is an independent
13 + * GPL re-implementation with its own tiny schema; no original code is reused.
14 + *
15 + * This program is free software: you can redistribute it and/or modify it under
16 + * the terms of the GNU General Public License as published by the Free Software
17 + * Foundation, either version 3 of the License, or (at your option) any later
18 + * version. It is distributed in the hope that it will be useful, but WITHOUT
19 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 + * FOR A PARTICULAR PURPOSE. See <https://www.gnu.org/licenses/> for details.
21 + */
22 +
23 +#include <amxmodx>
24 +#include <hamsandwich>
25 +#include <sqlx>
26 +
27 +new const PLUGIN[] = "CSB MySQL Ranks"
28 +new const VERSION[] = "1.0.0"
29 +new const AUTHOR[] = "counter-strike-boost.com"
30 +
31 +#define TABLE "csb_ranks"
32 +
33 +enum _:Stats
34 +{
35 + ST_KILLS,
36 + ST_DEATHS,
37 + ST_HS,
38 + ST_DAMAGE,
39 + ST_ROUNDS,
40 + ST_TIME
41 +}
42 +
43 +new g_iStats[33][Stats]
44 +new g_iConnect[33]
45 +new g_szAuth[33][35]
46 +new bool:g_bLoaded[33]
47 +
48 +new Handle:g_hTuple = Empty_Handle
49 +new g_pHost, g_pUser, g_pPass, g_pDb
50 +
51 +public plugin_init()
52 +{
53 + register_plugin(PLUGIN, VERSION, AUTHOR)
54 +
55 + g_pHost = register_cvar("csb_rank_host", "127.0.0.1")
56 + g_pUser = register_cvar("csb_rank_user", "csb")
57 + g_pPass = register_cvar("csb_rank_pass", "")
58 + g_pDb = register_cvar("csb_rank_db", "csb")
59 +
60 + register_event("DeathMsg", "onDeath", "a")
61 + register_event("HLTV", "onNewRound", "a", "1=0", "2=0")
62 +
63 + RegisterHam(Ham_TakeDamage, "player", "fw_TakeDamage")
64 +
65 + register_clcmd("say /rank", "cmdRank")
66 + register_clcmd("say_team /rank", "cmdRank")
67 + register_clcmd("say /stats", "cmdRank")
68 +}
69 +
70 +public plugin_cfg()
71 +{
72 + new host[64], user[64], pass[64], db[64]
73 + get_pcvar_string(g_pHost, host, charsmax(host))
74 + get_pcvar_string(g_pUser, user, charsmax(user))
75 + get_pcvar_string(g_pPass, pass, charsmax(pass))
76 + get_pcvar_string(g_pDb, db, charsmax(db))
77 +
78 + g_hTuple = SQL_MakeDbTuple(host, user, pass, db)
79 +
80 + new query[640]
81 + formatex(query, charsmax(query), "CREATE TABLE IF NOT EXISTS `%s` (`authid` VARCHAR(34) NOT NULL PRIMARY KEY, `name` VARCHAR(32) NOT NULL DEFAULT '', `kills` INT UNSIGNED NOT NULL DEFAULT 0, `deaths` INT UNSIGNED NOT NULL DEFAULT 0, `hs` INT UNSIGNED NOT NULL DEFAULT 0, `damage` INT UNSIGNED NOT NULL DEFAULT 0, `rounds` INT UNSIGNED NOT NULL DEFAULT 0, `playtime` INT UNSIGNED NOT NULL DEFAULT 0, `score` INT NOT NULL DEFAULT 0, `last_seen` INT UNSIGNED NOT NULL DEFAULT 0, KEY `k_score` (`score`)) ENGINE=InnoDB", TABLE)
82 +
83 + SQL_ThreadQuery(g_hTuple, "handlerSimple", query)
84 +}
85 +
86 +public plugin_end()
87 +{
88 + for (new id = 1; id <= 32; id++)
89 + if (g_bLoaded[id])
90 + saveStats(id)
91 +
92 + if (g_hTuple != Empty_Handle)
93 + SQL_FreeHandle(g_hTuple)
94 +}
95 +
96 +/* ---------- load / save ---------- */
97 +
98 +public client_authorized(id)
99 +{
100 + resetSlot(id)
101 +
102 + if (is_user_bot(id) || is_user_hltv(id) || g_hTuple == Empty_Handle)
103 + return
104 +
105 + get_user_authid(id, g_szAuth[id], charsmax(g_szAuth[]))
106 + g_iConnect[id] = get_systime()
107 + g_bLoaded[id] = true
108 +
109 + if (!g_szAuth[id][0])
110 + return
111 +
112 + new safe[70]
113 + sqlEscape(g_szAuth[id], safe, charsmax(safe))
114 +
115 + new query[256]
116 + formatex(query, charsmax(query), "SELECT `kills`, `deaths`, `hs`, `damage`, `rounds`, `playtime` FROM `%s` WHERE `authid` = '%s'", TABLE, safe)
117 +
118 + new data[2]
119 + data[0] = id
120 + data[1] = get_user_userid(id)
121 +
122 + SQL_ThreadQuery(g_hTuple, "handlerLoad", query, data, sizeof(data))
123 +}
124 +
125 +public handlerLoad(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime)
126 +{
127 + if (failstate != TQUERY_SUCCESS)
128 + {
129 + log_amx("[CSB Rank] load failed (%d): %s", errnum, error)
130 + return
131 + }
132 +
133 + new id = data[0]
134 + if (!is_user_connected(id) || get_user_userid(id) != data[1])
135 + return
136 +
137 + if (!SQL_NumResults(query))
138 + return
139 +
140 + g_iStats[id][ST_KILLS] = SQL_ReadResult(query, 0)
141 + g_iStats[id][ST_DEATHS] = SQL_ReadResult(query, 1)
142 + g_iStats[id][ST_HS] = SQL_ReadResult(query, 2)
143 + g_iStats[id][ST_DAMAGE] = SQL_ReadResult(query, 3)
144 + g_iStats[id][ST_ROUNDS] = SQL_ReadResult(query, 4)
145 + g_iStats[id][ST_TIME] = SQL_ReadResult(query, 5)
146 +}
147 +
148 +public client_disconnected(id)
149 +{
150 + if (g_bLoaded[id])
151 + saveStats(id)
152 +
153 + resetSlot(id)
154 +}
155 +
156 +saveStats(id)
157 +{
158 + if (g_hTuple == Empty_Handle || !g_szAuth[id][0])
159 + return
160 +
161 + if (g_iConnect[id])
162 + {
163 + g_iStats[id][ST_TIME] += get_systime() - g_iConnect[id]
164 + g_iConnect[id] = get_systime()
165 + }
166 +
167 + new name[32], safeName[70], safeAuth[70]
168 + get_user_name(id, name, charsmax(name))
169 + sqlEscape(name, safeName, charsmax(safeName))
170 + sqlEscape(g_szAuth[id], safeAuth, charsmax(safeAuth))
171 +
172 + new score = scoreOf(id)
173 +
174 + new query[768]
175 + formatex(query, charsmax(query), "INSERT INTO `%s` (`authid`, `name`, `kills`, `deaths`, `hs`, `damage`, `rounds`, `playtime`, `score`, `last_seen`) VALUES ('%s', '%s', %d, %d, %d, %d, %d, %d, %d, %d) ON DUPLICATE KEY UPDATE `name` = VALUES(`name`), `kills` = VALUES(`kills`), `deaths` = VALUES(`deaths`), `hs` = VALUES(`hs`), `damage` = VALUES(`damage`), `rounds` = VALUES(`rounds`), `playtime` = VALUES(`playtime`), `score` = VALUES(`score`), `last_seen` = VALUES(`last_seen`)",
176 + TABLE, safeAuth, safeName,
177 + g_iStats[id][ST_KILLS], g_iStats[id][ST_DEATHS], g_iStats[id][ST_HS],
178 + g_iStats[id][ST_DAMAGE], g_iStats[id][ST_ROUNDS], g_iStats[id][ST_TIME],
179 + score, get_systime())
180 +
181 + SQL_ThreadQuery(g_hTuple, "handlerSimple", query)
182 +}
183 +
184 +resetSlot(id)
185 +{
186 + for (new i = 0; i < Stats; i++)
187 + g_iStats[id][i] = 0
188 +
189 + g_iConnect[id] = 0
190 + g_szAuth[id][0] = 0
191 + g_bLoaded[id] = false
192 +}
193 +
194 +/* ---------- gameplay hooks ---------- */
195 +
196 +public onDeath()
197 +{
198 + new killer = read_data(1)
199 + new victim = read_data(2)
200 + new headshot = read_data(3)
201 +
202 + if (victim >= 1 && victim <= 32 && g_bLoaded[victim])
203 + g_iStats[victim][ST_DEATHS]++
204 +
205 + if (killer >= 1 && killer <= 32 && killer != victim && g_bLoaded[killer])
206 + {
207 + g_iStats[killer][ST_KILLS]++
208 + if (headshot)
209 + g_iStats[killer][ST_HS]++
210 + }
211 +}
212 +
213 +public fw_TakeDamage(victim, inflictor, attacker, Float:damage, damagebits)
214 +{
215 + if (attacker < 1 || attacker > 32 || attacker == victim || !g_bLoaded[attacker])
216 + return HAM_IGNORED
217 +
218 + g_iStats[attacker][ST_DAMAGE] += floatround(damage)
219 + return HAM_IGNORED
220 +}
221 +
222 +public onNewRound()
223 +{
224 + for (new id = 1; id <= 32; id++)
225 + if (g_bLoaded[id])
226 + g_iStats[id][ST_ROUNDS]++
227 +}
228 +
229 +/* ---------- score / rank ---------- */
230 +
231 +scoreOf(id)
232 +{
233 + return g_iStats[id][ST_KILLS] * 2
234 + + g_iStats[id][ST_HS]
235 + + g_iStats[id][ST_DAMAGE] / 50
236 + + g_iStats[id][ST_ROUNDS]
237 + - g_iStats[id][ST_DEATHS]
238 +}
239 +
240 +public cmdRank(id)
241 +{
242 + if (!g_bLoaded[id])
243 + {
244 + client_print(id, print_chat, "[CSB] Your stats are not loaded yet.")
245 + return PLUGIN_HANDLED
246 + }
247 +
248 + new mins = g_iStats[id][ST_TIME] / 60
249 + if (g_iConnect[id])
250 + mins += (get_systime() - g_iConnect[id]) / 60
251 +
252 + client_print(id, print_chat,
253 + "[CSB] Kills: %d | Deaths: %d | HS: %d | Damage: %d | Rounds: %d | Time: %d min | Score: %d",
254 + g_iStats[id][ST_KILLS], g_iStats[id][ST_DEATHS], g_iStats[id][ST_HS],
255 + g_iStats[id][ST_DAMAGE], g_iStats[id][ST_ROUNDS], mins, scoreOf(id))
256 +
257 + /* resolve the exact ladder position with a threaded COUNT(*) */
258 + if (g_hTuple != Empty_Handle)
259 + {
260 + new query[192]
261 + formatex(query, charsmax(query), "SELECT COUNT(*) + 1 FROM `%s` WHERE `score` > %d", TABLE, scoreOf(id))
262 +
263 + new data[2]
264 + data[0] = id
265 + data[1] = get_user_userid(id)
266 +
267 + SQL_ThreadQuery(g_hTuple, "handlerPosition", query, data, sizeof(data))
268 + }
269 +
270 + return PLUGIN_HANDLED
271 +}
272 +
273 +public handlerPosition(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime)
274 +{
275 + if (failstate != TQUERY_SUCCESS)
276 + {
277 + log_amx("[CSB Rank] position query failed (%d): %s", errnum, error)
278 + return
279 + }
280 +
281 + new id = data[0]
282 + if (!is_user_connected(id) || get_user_userid(id) != data[1])
283 + return
284 +
285 + if (!SQL_NumResults(query))
286 + return
287 +
288 + new pos = SQL_ReadResult(query, 0)
289 + client_print_color(id, print_team_default,
290 + "^x04[CSB]^x03 Your ladder position is^x04 #%d^x01.", pos)
291 +}
292 +
293 +public handlerSimple(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime)
294 +{
295 + if (failstate != TQUERY_SUCCESS)
296 + log_amx("[CSB Rank] query failed (%d): %s", errnum, error)
297 +}
298 +
299 +/* ---------- helpers ---------- */
300 +
301 +stock sqlEscape(const input[], output[], len)
302 +{
303 + new i = 0, j = 0, c
304 +
305 + while (input[i] != 0 && j < len)
306 + {
307 + c = input[i]
308 +
309 + if (c == 39 || c == 34 || c == 92 || c == 59 || c == 37 || c == 96 || c < 32)
310 + {
311 + i++
312 + continue
313 + }
314 +
315 + output[j++] = c
316 + i++
317 + }
318 +
319 + output[j] = 0
320 +}
+57 -0 csb.json A
@@ -0,0 +1,57 @@
1 +{
2 + "name": "CSB MySQL Ranks",
3 + "category": "stats",
4 + "tags": "rank, mysql, sqlx",
5 + "image": "",
6 + "original_author": "PsychoStats",
7 + "original_url": "",
8 + "license": "GPL-3.0",
9 + "title": "CSB MySQL Ranks",
10 + "short_desc": "The same stats model as the nVault ranks, stored in MySQL with threaded SQLx queries, so several servers share one ladder the website can read.",
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. Put csb_rank_mysql.amxx in addons/amxmodx/plugins/\n2. Add it to plugins.ini\n3. Make sure the sqlx and hamsandwich modules are enabled\n4. Set csb_rank_host/user/pass/db (e.g. in amxx.cfg or a server cfg)\n5. The table is created automatically, or import csb_ranks.sql\n6. Uses client_print_color, so AMX Mod X 1.9+ is required",
21 + "known_conflicts": "Run only one CSB rank plugin (nVault or MySQL), not both.",
22 + "cvars": [
23 + {
24 + "name": "csb_rank_host",
25 + "default": "127.0.0.1",
26 + "description": "MySQL host."
27 + },
28 + {
29 + "name": "csb_rank_user",
30 + "default": "csb",
31 + "description": "MySQL user."
32 + },
33 + {
34 + "name": "csb_rank_pass",
35 + "default": "",
36 + "description": "MySQL password."
37 + },
38 + {
39 + "name": "csb_rank_db",
40 + "default": "csb",
41 + "description": "MySQL database name."
42 + }
43 + ],
44 + "commands": [
45 + {
46 + "name": "say /rank",
47 + "access": "",
48 + "description": "Show your stats, score and exact ladder position."
49 + },
50 + {
51 + "name": "say /stats",
52 + "access": "",
53 + "description": "Alias of /rank."
54 + }
55 + ],
56 + "main": "addons/amxmodx/scripting/csb_rank_mysql.sma"
57 +}
0 58