Initial release 1.0.0
Browse Source1.0.0 - Initial release.
4 changed files
with 435 additions and 0 deletions
A
CHANGELOG.md
+3 -0
A
README.md
+1 -0
A
addons/amxmodx/scripting/csb_mix_elo.sma
+354 -0
A
csb.json
+77 -0
CHANGELOG.md
A
| @@ -0,0 +1,3 @@ | ||
| 1 | +## 1.0.0 (2026-04-02) | |
| 2 | + | |
| 3 | +1.0.0 - Initial release. |
README.md
A
| @@ -0,0 +1 @@ | ||
| 1 | +<p><strong>CSB Mix ELO</strong> gives mix/match play a proper skill rating. Ratings load asynchronously on authorise; after each match <code>amx_elo_result <T|CT></code> applies the standard ELO update - winners gain and losers lose points scaled by the rating difference and a configurable K factor - and writes back through threaded SQLx. Players check their rating with <code>/elo</code>, the ladder with <code>/topelo</code>, and admins can print ELO-balanced teams with <code>amx_elobalance</code>.</p><h2>Features</h2><ul><li>Async load on authorise, threaded write-back after each match.</li><li>Standard ELO formula with a configurable K factor.</li><li><code>/elo</code> shows your rating; <code>/topelo</code> shows the ladder.</li><li><code>amx_elobalance</code> greedily splits players into balanced teams.</li></ul><h2>Installation</h2><ol><li>Enable the <code>sqlx</code> module in <code>modules.ini</code>.</li><li>Set the <code>csb_elo_*</code> cvars to your database credentials.</li><li>Compile <code>csb_mix_elo.sma</code>, copy it to <code>addons/amxmodx/plugins/</code> and add it to <code>plugins.ini</code>.</li><li>Change the map. Call <code>amx_elo_result</code> with the winning side at match end.</li></ol> |
addons/amxmodx/scripting/csb_mix_elo.sma
A
| @@ -0,0 +1,354 @@ | ||
| 1 | +/* | |
| 2 | + * CSB Mix ELO | |
| 3 | + * Copyright (C) 2026 counter-strike-boost.com | |
| 4 | + * | |
| 5 | + * ELO rating for mix/match play stored in MySQL. Ratings load asynchronously on | |
| 6 | + * authorise; after a match amx_elo_result <T|CT> applies the standard ELO update | |
| 7 | + * (winners gain, losers lose, scaled by the rating gap and K factor) and writes | |
| 8 | + * back through threaded SQLx. /elo shows your rating, /topelo the ladder, and | |
| 9 | + * amx_elobalance prints ELO-balanced teams. | |
| 10 | + * | |
| 11 | + * This program is free software: you can redistribute it and/or modify it under | |
| 12 | + * the terms of the GNU General Public License as published by the Free Software | |
| 13 | + * Foundation, either version 3 of the License, or (at your option) any later | |
| 14 | + * version. It is distributed in the hope that it will be useful, but WITHOUT | |
| 15 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 16 | + * FOR A PARTICULAR PURPOSE. See <https://www.gnu.org/licenses/> for details. | |
| 17 | + */ | |
| 18 | + | |
| 19 | +#include <amxmodx> | |
| 20 | +#include <amxmisc> | |
| 21 | +#include <sqlx> | |
| 22 | + | |
| 23 | +new const PLUGIN[] = "CSB Mix ELO" | |
| 24 | +new const VERSION[] = "1.0.0" | |
| 25 | +new const AUTHOR[] = "counter-strike-boost.com" | |
| 26 | + | |
| 27 | +new const TABLE[] = "csb_elo" | |
| 28 | + | |
| 29 | +new g_pHost, g_pUser, g_pPass, g_pDb, g_pK, g_pStart | |
| 30 | +new Handle:g_hTuple = Empty_Handle | |
| 31 | + | |
| 32 | +new g_iRating[33] | |
| 33 | +new bool:g_bLoaded[33] | |
| 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_elo_host", "127.0.0.1") | |
| 41 | + g_pUser = register_cvar("csb_elo_user", "csb") | |
| 42 | + g_pPass = register_cvar("csb_elo_pass", "") | |
| 43 | + g_pDb = register_cvar("csb_elo_db", "csb") | |
| 44 | + g_pK = register_cvar("csb_elo_k", "32") | |
| 45 | + g_pStart = register_cvar("csb_elo_start", "1000") | |
| 46 | + | |
| 47 | + register_clcmd("say /elo", "cmdElo") | |
| 48 | + register_clcmd("say_team /elo", "cmdElo") | |
| 49 | + register_clcmd("say /topelo", "cmdTop") | |
| 50 | + register_clcmd("say_team /topelo", "cmdTop") | |
| 51 | + | |
| 52 | + register_concmd("amx_elo_result", "cmdResult", ADMIN_BAN, "<T|CT> - apply match result to ELO") | |
| 53 | + register_concmd("amx_elobalance", "cmdBalance", ADMIN_BAN, "- print ELO-balanced teams") | |
| 54 | +} | |
| 55 | + | |
| 56 | +public plugin_cfg() | |
| 57 | +{ | |
| 58 | + new host[64], user[64], pass[64], db[64] | |
| 59 | + get_pcvar_string(g_pHost, host, charsmax(host)) | |
| 60 | + get_pcvar_string(g_pUser, user, charsmax(user)) | |
| 61 | + get_pcvar_string(g_pPass, pass, charsmax(pass)) | |
| 62 | + get_pcvar_string(g_pDb, db, charsmax(db)) | |
| 63 | + | |
| 64 | + if (host[0] && db[0]) | |
| 65 | + { | |
| 66 | + g_hTuple = SQL_MakeDbTuple(host, user, pass, db) | |
| 67 | + | |
| 68 | + new q[512] | |
| 69 | + formatex(q, charsmax(q), "CREATE TABLE IF NOT EXISTS `%s` (`authid` VARCHAR(34) NOT NULL PRIMARY KEY, `name` VARCHAR(32) NOT NULL DEFAULT '', `rating` INT NOT NULL DEFAULT 1000, `wins` INT UNSIGNED NOT NULL DEFAULT 0, `losses` INT UNSIGNED NOT NULL DEFAULT 0, `games` INT UNSIGNED NOT NULL DEFAULT 0, KEY `k_rating` (`rating`)) ENGINE=InnoDB", TABLE) | |
| 70 | + | |
| 71 | + SQL_ThreadQuery(g_hTuple, "handlerSimple", q) | |
| 72 | + } | |
| 73 | +} | |
| 74 | + | |
| 75 | +public plugin_end() | |
| 76 | +{ | |
| 77 | + if (g_hTuple != Empty_Handle) | |
| 78 | + SQL_FreeHandle(g_hTuple) | |
| 79 | +} | |
| 80 | + | |
| 81 | +public client_authorized(id) | |
| 82 | +{ | |
| 83 | + g_bLoaded[id] = false | |
| 84 | + g_iRating[id] = get_pcvar_num(g_pStart) | |
| 85 | + g_szAuth[id][0] = 0 | |
| 86 | + | |
| 87 | + if (is_user_bot(id) || is_user_hltv(id) || g_hTuple == Empty_Handle) | |
| 88 | + return | |
| 89 | + | |
| 90 | + get_user_authid(id, g_szAuth[id], charsmax(g_szAuth[])) | |
| 91 | + if (!g_szAuth[id][0]) | |
| 92 | + return | |
| 93 | + | |
| 94 | + new safe[70] | |
| 95 | + sqlEscape(g_szAuth[id], safe, charsmax(safe)) | |
| 96 | + | |
| 97 | + new q[192] | |
| 98 | + formatex(q, charsmax(q), "SELECT `rating` FROM `%s` WHERE `authid` = '%s'", TABLE, safe) | |
| 99 | + | |
| 100 | + new data[2] | |
| 101 | + data[0] = id | |
| 102 | + data[1] = get_user_userid(id) | |
| 103 | + SQL_ThreadQuery(g_hTuple, "handlerLoad", q, data, sizeof(data)) | |
| 104 | +} | |
| 105 | + | |
| 106 | +public handlerLoad(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime) | |
| 107 | +{ | |
| 108 | + if (failstate != TQUERY_SUCCESS) | |
| 109 | + { | |
| 110 | + log_amx("[CSB ELO] load failed (%d): %s", errnum, error) | |
| 111 | + return | |
| 112 | + } | |
| 113 | + | |
| 114 | + new id = data[0] | |
| 115 | + if (!is_user_connected(id) || get_user_userid(id) != data[1]) | |
| 116 | + return | |
| 117 | + | |
| 118 | + if (SQL_NumResults(query)) | |
| 119 | + g_iRating[id] = SQL_ReadResult(query, 0) | |
| 120 | + | |
| 121 | + g_bLoaded[id] = true | |
| 122 | +} | |
| 123 | + | |
| 124 | +public cmdElo(id) | |
| 125 | +{ | |
| 126 | + client_print(id, print_chat, "[CSB] Your ELO rating is %d.", g_iRating[id]) | |
| 127 | + return PLUGIN_HANDLED | |
| 128 | +} | |
| 129 | + | |
| 130 | +public cmdTop(id) | |
| 131 | +{ | |
| 132 | + if (g_hTuple == Empty_Handle) | |
| 133 | + { | |
| 134 | + client_print(id, print_chat, "[CSB] The ELO database is not configured.") | |
| 135 | + return PLUGIN_HANDLED | |
| 136 | + } | |
| 137 | + | |
| 138 | + new data[2] | |
| 139 | + data[0] = id | |
| 140 | + data[1] = get_user_userid(id) | |
| 141 | + SQL_ThreadQuery(g_hTuple, "handlerTop", "SELECT `name`, `rating` FROM `csb_elo` ORDER BY `rating` DESC LIMIT 10", data, sizeof(data)) | |
| 142 | + return PLUGIN_HANDLED | |
| 143 | +} | |
| 144 | + | |
| 145 | +public handlerTop(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime) | |
| 146 | +{ | |
| 147 | + if (failstate != TQUERY_SUCCESS) | |
| 148 | + { | |
| 149 | + log_amx("[CSB ELO] top query failed (%d): %s", errnum, error) | |
| 150 | + return | |
| 151 | + } | |
| 152 | + | |
| 153 | + new id = data[0] | |
| 154 | + if (!is_user_connected(id) || get_user_userid(id) != data[1]) | |
| 155 | + return | |
| 156 | + | |
| 157 | + client_print(id, print_chat, "[CSB] ===== Top ELO =====") | |
| 158 | + | |
| 159 | + new rank = 0 | |
| 160 | + while (SQL_MoreResults(query)) | |
| 161 | + { | |
| 162 | + rank++ | |
| 163 | + new name[32] | |
| 164 | + SQL_ReadResult(query, 0, name, charsmax(name)) | |
| 165 | + new rating = SQL_ReadResult(query, 1) | |
| 166 | + client_print(id, print_chat, "[CSB] %d. %s - %d", rank, name, rating) | |
| 167 | + SQL_NextRow(query) | |
| 168 | + } | |
| 169 | + | |
| 170 | + if (!rank) | |
| 171 | + client_print(id, print_chat, "[CSB] No rated players yet.") | |
| 172 | +} | |
| 173 | + | |
| 174 | +public cmdResult(id, level, cid) | |
| 175 | +{ | |
| 176 | + if (!cmd_access(id, level, cid, 2)) | |
| 177 | + return PLUGIN_HANDLED | |
| 178 | + | |
| 179 | + new arg[12] | |
| 180 | + read_argv(1, arg, charsmax(arg)) | |
| 181 | + | |
| 182 | + new winnerTeam | |
| 183 | + if (equali(arg, "CT")) | |
| 184 | + winnerTeam = 2 | |
| 185 | + else if (equali(arg, "T") || equali(arg, "TERRORIST")) | |
| 186 | + winnerTeam = 1 | |
| 187 | + else | |
| 188 | + { | |
| 189 | + console_print(id, "[CSB] Usage: amx_elo_result <T|CT>") | |
| 190 | + return PLUGIN_HANDLED | |
| 191 | + } | |
| 192 | + | |
| 193 | + applyResult(winnerTeam) | |
| 194 | + console_print(id, "[CSB] ELO updated for the match.") | |
| 195 | + return PLUGIN_HANDLED | |
| 196 | +} | |
| 197 | + | |
| 198 | +applyResult(winnerTeam) | |
| 199 | +{ | |
| 200 | + new players[32], num | |
| 201 | + get_players(players, num, "ch") | |
| 202 | + | |
| 203 | + new sumW = 0, cntW = 0, sumL = 0, cntL = 0 | |
| 204 | + | |
| 205 | + for (new i = 0; i < num; i++) | |
| 206 | + { | |
| 207 | + new t = get_user_team(players[i]) | |
| 208 | + if (t == winnerTeam) | |
| 209 | + { | |
| 210 | + sumW += g_iRating[players[i]] | |
| 211 | + cntW++ | |
| 212 | + } | |
| 213 | + else if (t == 1 || t == 2) | |
| 214 | + { | |
| 215 | + sumL += g_iRating[players[i]] | |
| 216 | + cntL++ | |
| 217 | + } | |
| 218 | + } | |
| 219 | + | |
| 220 | + if (!cntW || !cntL) | |
| 221 | + { | |
| 222 | + client_print(0, print_chat, "[CSB] Both teams need players to rate a result.") | |
| 223 | + return | |
| 224 | + } | |
| 225 | + | |
| 226 | + new Float:avgW = float(sumW) / float(cntW) | |
| 227 | + new Float:avgL = float(sumL) / float(cntL) | |
| 228 | + new Float:k = get_pcvar_float(g_pK) | |
| 229 | + | |
| 230 | + new Float:expW = 1.0 / (1.0 + floatpower(10.0, (avgL - avgW) / 400.0)) | |
| 231 | + new Float:expL = 1.0 / (1.0 + floatpower(10.0, (avgW - avgL) / 400.0)) | |
| 232 | + | |
| 233 | + new deltaW = floatround(k * (1.0 - expW)) | |
| 234 | + new deltaL = floatround(k * (0.0 - expL)) | |
| 235 | + | |
| 236 | + for (new i = 0; i < num; i++) | |
| 237 | + { | |
| 238 | + new pid = players[i] | |
| 239 | + new t = get_user_team(pid) | |
| 240 | + | |
| 241 | + if (t != 1 && t != 2) | |
| 242 | + continue | |
| 243 | + | |
| 244 | + new bool:won = (t == winnerTeam) | |
| 245 | + new delta = won ? deltaW : deltaL | |
| 246 | + | |
| 247 | + g_iRating[pid] += delta | |
| 248 | + if (g_iRating[pid] < 100) | |
| 249 | + g_iRating[pid] = 100 | |
| 250 | + | |
| 251 | + saveRating(pid, won) | |
| 252 | + | |
| 253 | + client_print(pid, print_chat, "[CSB] Match %s! Your ELO is now %d (%s%d).", | |
| 254 | + won ? "won" : "lost", g_iRating[pid], delta >= 0 ? "+" : "", delta) | |
| 255 | + } | |
| 256 | +} | |
| 257 | + | |
| 258 | +saveRating(id, bool:won) | |
| 259 | +{ | |
| 260 | + if (g_hTuple == Empty_Handle || !g_szAuth[id][0]) | |
| 261 | + return | |
| 262 | + | |
| 263 | + new name[32], safeName[70], safeAuth[70] | |
| 264 | + get_user_name(id, name, charsmax(name)) | |
| 265 | + sqlEscape(name, safeName, charsmax(safeName)) | |
| 266 | + sqlEscape(g_szAuth[id], safeAuth, charsmax(safeAuth)) | |
| 267 | + | |
| 268 | + new q[600] | |
| 269 | + formatex(q, charsmax(q), "INSERT INTO `%s` (`authid`, `name`, `rating`, `wins`, `losses`, `games`) VALUES ('%s', '%s', %d, %d, %d, 1) ON DUPLICATE KEY UPDATE `name` = VALUES(`name`), `rating` = %d, `wins` = `wins` + %d, `losses` = `losses` + %d, `games` = `games` + 1", | |
| 270 | + TABLE, safeAuth, safeName, g_iRating[id], won ? 1 : 0, won ? 0 : 1, | |
| 271 | + g_iRating[id], won ? 1 : 0, won ? 0 : 1) | |
| 272 | + | |
| 273 | + SQL_ThreadQuery(g_hTuple, "handlerSimple", q) | |
| 274 | +} | |
| 275 | + | |
| 276 | +public cmdBalance(id, level, cid) | |
| 277 | +{ | |
| 278 | + if (!cmd_access(id, level, cid, 1)) | |
| 279 | + return PLUGIN_HANDLED | |
| 280 | + | |
| 281 | + new players[32], num | |
| 282 | + get_players(players, num, "ch") | |
| 283 | + | |
| 284 | + /* insertion sort by rating, descending */ | |
| 285 | + for (new i = 1; i < num; i++) | |
| 286 | + { | |
| 287 | + new key = players[i] | |
| 288 | + new j = i - 1 | |
| 289 | + while (j >= 0 && g_iRating[players[j]] < g_iRating[key]) | |
| 290 | + { | |
| 291 | + players[j + 1] = players[j] | |
| 292 | + j-- | |
| 293 | + } | |
| 294 | + players[j + 1] = key | |
| 295 | + } | |
| 296 | + | |
| 297 | + new teamA[512], teamB[512] | |
| 298 | + teamA[0] = 0 | |
| 299 | + teamB[0] = 0 | |
| 300 | + new sumA = 0, sumB = 0 | |
| 301 | + | |
| 302 | + for (new i = 0; i < num; i++) | |
| 303 | + { | |
| 304 | + new pid = players[i] | |
| 305 | + new name[32] | |
| 306 | + get_user_name(pid, name, charsmax(name)) | |
| 307 | + | |
| 308 | + new entry[48] | |
| 309 | + formatex(entry, charsmax(entry), "%s(%d) ", name, g_iRating[pid]) | |
| 310 | + | |
| 311 | + if (sumA <= sumB) | |
| 312 | + { | |
| 313 | + add(teamA, charsmax(teamA), entry) | |
| 314 | + sumA += g_iRating[pid] | |
| 315 | + } | |
| 316 | + else | |
| 317 | + { | |
| 318 | + add(teamB, charsmax(teamB), entry) | |
| 319 | + sumB += g_iRating[pid] | |
| 320 | + } | |
| 321 | + } | |
| 322 | + | |
| 323 | + console_print(id, "[CSB ELO] Team A (total %d): %s", sumA, teamA) | |
| 324 | + console_print(id, "[CSB ELO] Team B (total %d): %s", sumB, teamB) | |
| 325 | + client_print(id, print_chat, "[CSB] ELO-balanced teams printed to your console.") | |
| 326 | + return PLUGIN_HANDLED | |
| 327 | +} | |
| 328 | + | |
| 329 | +public handlerSimple(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime) | |
| 330 | +{ | |
| 331 | + if (failstate != TQUERY_SUCCESS) | |
| 332 | + log_amx("[CSB ELO] query failed (%d): %s", errnum, error) | |
| 333 | +} | |
| 334 | + | |
| 335 | +stock sqlEscape(const input[], output[], len) | |
| 336 | +{ | |
| 337 | + new i = 0, j = 0, c | |
| 338 | + | |
| 339 | + while (input[i] != 0 && j < len) | |
| 340 | + { | |
| 341 | + c = input[i] | |
| 342 | + | |
| 343 | + if (c == 39 || c == 34 || c == 92 || c == 59 || c == 37 || c == 96 || c < 32) | |
| 344 | + { | |
| 345 | + i++ | |
| 346 | + continue | |
| 347 | + } | |
| 348 | + | |
| 349 | + output[j++] = c | |
| 350 | + i++ | |
| 351 | + } | |
| 352 | + | |
| 353 | + output[j] = 0 | |
| 354 | +} |
csb.json
A
| @@ -0,0 +1,77 @@ | ||
| 1 | +{ | |
| 2 | + "name": "CSB Mix ELO", | |
| 3 | + "category": "match", | |
| 4 | + "tags": "elo, ranking, mix", | |
| 5 | + "image": "", | |
| 6 | + "original_author": "", | |
| 7 | + "original_url": "", | |
| 8 | + "license": "GPL-3.0", | |
| 9 | + "title": "CSB Mix ELO", | |
| 10 | + "short_desc": "ELO rating for mix/match play in MySQL: winners gain and losers lose points by the rating gap, with /elo, /topelo and ELO-balanced teams.", | |
| 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": "sqlx", | |
| 20 | + "install_notes": "1. Enable the sqlx module in addons/amxmodx/configs/modules.ini\n2. Set the csb_elo_* cvars to your DB credentials\n3. Put csb_mix_elo.amxx in addons/amxmodx/plugins/ and add it to plugins.ini\n4. Change map", | |
| 21 | + "known_conflicts": "Other ranking plugins that use /elo or /topelo chat triggers will collide.", | |
| 22 | + "cvars": [ | |
| 23 | + { | |
| 24 | + "name": "csb_elo_host", | |
| 25 | + "default": "127.0.0.1", | |
| 26 | + "description": "MySQL host." | |
| 27 | + }, | |
| 28 | + { | |
| 29 | + "name": "csb_elo_user", | |
| 30 | + "default": "csb", | |
| 31 | + "description": "MySQL user." | |
| 32 | + }, | |
| 33 | + { | |
| 34 | + "name": "csb_elo_pass", | |
| 35 | + "default": "", | |
| 36 | + "description": "MySQL password." | |
| 37 | + }, | |
| 38 | + { | |
| 39 | + "name": "csb_elo_db", | |
| 40 | + "default": "csb", | |
| 41 | + "description": "MySQL database name." | |
| 42 | + }, | |
| 43 | + { | |
| 44 | + "name": "csb_elo_k", | |
| 45 | + "default": "32", | |
| 46 | + "description": "ELO K factor (points at stake)." | |
| 47 | + }, | |
| 48 | + { | |
| 49 | + "name": "csb_elo_start", | |
| 50 | + "default": "1000", | |
| 51 | + "description": "Starting rating for new players." | |
| 52 | + } | |
| 53 | + ], | |
| 54 | + "commands": [ | |
| 55 | + { | |
| 56 | + "name": "say /elo", | |
| 57 | + "access": "", | |
| 58 | + "description": "Show your ELO rating." | |
| 59 | + }, | |
| 60 | + { | |
| 61 | + "name": "say /topelo", | |
| 62 | + "access": "", | |
| 63 | + "description": "Show the ELO ladder (top 10)." | |
| 64 | + }, | |
| 65 | + { | |
| 66 | + "name": "amx_elo_result", | |
| 67 | + "access": "d (ADMIN_BAN)", | |
| 68 | + "description": "amx_elo_result <T|CT> - apply the match result." | |
| 69 | + }, | |
| 70 | + { | |
| 71 | + "name": "amx_elobalance", | |
| 72 | + "access": "d (ADMIN_BAN)", | |
| 73 | + "description": "Print ELO-balanced teams." | |
| 74 | + } | |
| 75 | + ], | |
| 76 | + "main": "addons/amxmodx/scripting/csb_mix_elo.sma" | |
| 77 | +} | |
| 0 | 78 |









