Kod:
#include <amxmodx>
#include <amxmisc>
#include <csstats>
#include <sqlx>
#define HUD_INTERVAL 1.0
#define MAXRANKS 17
new Handle:g_SqlTuple
new g_szQuery[512]
new PlayerRank[33]
new g_szSteamID[33][35]
enum _:RankInfo {
RANK_ONBASI,
RANK_CAVUS,
RANK_UZMAN,
RANK_ASTSUBAY,
RANK_ASTEGMEN,
RANK_TEGMEN,
RANK_USTEGMEN,
RANK_YUZBASI,
RANK_BINBASI,
RANK_YARBAY,
RANK_ALBAY,
RANK_TUGGENERAL,
RANK_TUMGENERAL,
RANK_KORGENERAL,
RANK_ORGENERAL,
RANK_GENELKURMAY,
RANK_MARESAL
}
new const rankNames[MAXRANKS][] = {
"Onbasi",
"Cavus",
"Uzman",
"Astsubay",
"Astegmen",
"Tegmen",
"Ustegmen",
"Yuzbasi",
"Binbasi",
"Yarbay",
"Albay",
"Tuggeneral",
"Tumgeneral",
"Korgeneral",
"Orgeneral",
"GenelKurmay Baskani",
"Maresal"
}
new const rankXP[MAXRANKS] = {
0,
100,
200,
400,
800,
1000,
2000,
4000,
8000,
10000,
12000,
15000,
20000,
40000,
50000,
80000,
100000
}
new const g_ColorsCodes[][3] = {
{255, 215, 0}, // Altın
{192, 192, 192}, // Gümüş
{205, 127, 50}, // Bronz
{255, 69, 0}, // Kırmızı-Turuncu
{138, 43, 226} // Mor
}
public plugin_init() {
register_plugin("Rank System", "2.0", "Author")
register_event("DeathMsg", "EventDeath", "a")
init_database()
}
init_database() {
g_SqlTuple = SQL_MakeDbTuple("localhost", "root", "", "cs_ranks")
formatex(g_szQuery, charsmax(g_szQuery), "\
CREATE TABLE IF NOT EXISTS player_ranks (\
steamid VARCHAR(35) PRIMARY KEY,\
kills INT DEFAULT 0,\
rank INT DEFAULT 0,\
last_seen INT DEFAULT 0)")
SQL_ThreadQuery(g_SqlTuple, "HandleQuery", g_szQuery)
}
public client_authorized(id) {
get_user_authid(id, g_szSteamID[id], charsmax(g_szSteamID[]))
load_player_data(id)
set_task(HUD_INTERVAL, "UpdateHUD", id)
}
load_player_data(id) {
formatex(g_szQuery, charsmax(g_szQuery), "\
SELECT kills, rank FROM player_ranks WHERE steamid = '%s'",
g_szSteamID[id])
SQL_ThreadQuery(g_SqlTuple, "HandleLoadData", g_szQuery, id)
}
public HandleLoadData(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime) {
if(failstate != TQUERY_SUCCESS)
return
new id = data[0]
if(!is_user_connected(id))
return
if(SQL_NumResults(query) > 0) {
new kills = SQL_ReadResult(query, 0)
PlayerRank[id] = SQL_ReadResult(query, 1)
set_user_stats(id, kills, 0)
}
}
public EventDeath() {
new killer = read_data(1)
if(!is_user_connected(killer))
return
update_stats(killer)
}
update_stats(id) {
static stats[8], hits[8]
get_user_stats(id, stats, hits)
new old_rank = PlayerRank[id]
new new_rank = calculate_rank(stats[0])
if(new_rank > old_rank) {
PlayerRank[id] = new_rank
show_rank_up(id, old_rank, new_rank)
}
formatex(g_szQuery, charsmax(g_szQuery), "\
INSERT INTO player_ranks (steamid, kills, rank, last_seen) \
VALUES ('%s', %d, %d, %d) \
ON DUPLICATE KEY UPDATE \
kills = %d, rank = %d, last_seen = %d",
g_szSteamID[id], stats[0], new_rank, get_systime(),
stats[0], new_rank, get_systime())
SQL_ThreadQuery(g_SqlTuple, "HandleQuery", g_szQuery)
}
calculate_rank(kills) {
new rank = 0
for(new i = 0; i < MAXRANKS - 1; i++) {
if(kills >= rankXP[i + 1])
rank = i + 1
else
break
}
return rank
}
show_rank_up(id, old_rank, new_rank) {
new name[32]
get_user_name(id, name, charsmax(name))
new color[3]
color[0] = g_ColorsCodes[new_rank % sizeof(g_ColorsCodes)][0]
color[1] = g_ColorsCodes[new_rank % sizeof(g_ColorsCodes)][1]
color[2] = g_ColorsCodes[new_rank % sizeof(g_ColorsCodes)][2]
set_hudmessage(color[0], color[1], color[2], -1.0, 0.2, 0, 6.0, 3.0, 0.1, 0.2, 4)
show_hudmessage(0, "[ %s ]^n%s -> %s", name, rankNames[old_rank], rankNames[new_rank])
}
public UpdateHUD(id) {
if(!is_user_connected(id))
return 0
static stats[8], hits[8], name[33]
get_user_stats(id, stats, hits)
get_user_name(id, name, 32)
new rank = PlayerRank[id]
new next_rank = (rank + 1 < MAXRANKS) ? rank + 1 : rank
new color[3]
color[0] = g_ColorsCodes[rank % sizeof(g_ColorsCodes)][0]
color[1] = g_ColorsCodes[rank % sizeof(g_ColorsCodes)][1]
color[2] = g_ColorsCodes[rank % sizeof(g_ColorsCodes)][2]
set_hudmessage(color[0], color[1], color[2], 0.02, 0.15, 0, 6.0, 1.1, 0.0, 0.0, 1)
show_hudmessage(id, "Rutbe: %s^nSonraki: %s^nKill: %d/%d",
rankNames[rank],
rankNames[next_rank],
stats[0], rankXP[next_rank])
set_task(HUD_INTERVAL, "UpdateHUD", id)
return PLUGIN_HANDLED
}
public HandleQuery(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime) {
return PLUGIN_HANDLED
}
public plugin_end() {
SQL_FreeHandle(g_SqlTuple)
}