I run no-steam servers!
Join Date: Feb 2010
Location: Edge of nowhere
|
10-22-2012
, 16:32
Re: Connections Counter
|
#164
|
Hello again guys. I have edited the @Bugsy version a just little bit ( i hope not to get in any troubles.). I make the code more friendly. And only SQL and STEAM.
Let's get to the point. I have a few new ideas fit perfectly in the plugin.
• This was some nice request. http://forums.alliedmods.net/showthread.php?t=191050
• I trying to implent this protection K.K.Lv
I know the plugin updates the MySQL on every disconnect from server, so some players maybe wanna to crash it or etc. also i don't like to have much mysql request ( don't ask me ).
• My Attemps Bellow:
I have trying with automatic MySQL system update on every 5 mins after player join the server but i don't like to make one Huge MySQL request in just one sec.
I also try to make loading system, when player connect to the server to have just one query that print his status, and with the Auto MySQL System to sync him late in MySQL.
And couple of days i have been thinking for best solution and i think i got it. To count the connections and if player have 3 successfull retry with the server to get more CC to get him banned for (1min or 30sec)
After this is done will continue to make small lite request and soon we will get some Advanced Plugin for Connections be sure, i got some crazy ideas that are quite funny and easy.
If is possible to optimize the plugin to the maximum. ( To Clear the code a little bit from errors and make it friendly
Right now i have problems with detection from @K.K.Lv
Also sometimes have problems when _putinserver sometimes shows 1587432days and 0 0 0. - maybe set_task will fix it. ( cannot catch it yet )
Changelog:
I give it same extra RAM [#pragma dynamic 16384]
3. Added Cvar to determine the time between messages to prevent spam messages. Fixed bug with e blank message.
2. Fixed problem with the first connection of the player, it shows that there are two connectors instead of one.
1. Concise code and Sqlx repaired query system is also modified by the cvars for sql host.
Code:
// *************************************************************************************************
// #### #### ###### #### ## ## ###### ## ## ######
// ## ## ## ## ## ## ## # ### ## ## ## ## ##
// ## ### ## ###### ## # ### ###### ### ##
// ## ## ## ## #### ## ## ## ## ## ######
// *************************************************************************************************
// THIS PLUGIN IS EDITED BY MAD.XayC • SKYPE: X3NOMORPF • MAIL: [email protected]
// *************************************************************************************************
// • MYSQL ONLY • STEAM ONLY
// Thnx:
// @Bugsy ==> He is the Author!
// @K.K.Lv ==> For his awesome plugin here >> https://forums.alliedmods.net/showpost.php?p=1763040&postcount=5
//
// Date: 2012.22.10
// *************************************************************************************************
#include <amxmodx>
#include <cstrike>
#include <fakemeta>
#include <sqlx>
#pragma dynamic 16384
#define fClearTrieTime 12.4
#define iConnectionBanCount 3
#define NAMEOF_DB "cs_db"
#define CREATE_DB "CREATE TABLE IF NOT EXISTS `playtime_cc` (`key` VARCHAR(128) NOT NULL, `data` VARCHAR(255) NOT NULL, `timestamp` INT(11) NOT NULL, PRIMARY KEY (`key`))"
#define SEARCH_DB "SELECT `data` , `timestamp` FROM `playtime_cc` WHERE `key`='%s' LIMIT 1"
#define INSERT_DB "REPLACE INTO `playtime_cc` (`key`,`data`,`timestamp`) VALUES ('%s','%s','%d')"
// *************************************************************************************************
// Bellow you can add your own Status (I open i door for you just look in too the code)
// *************************************************************************************************
enum ConnectionStatus {
Newbie,
SemiPro,
Pro,
/* UltraPro, */
Veteran
}
new const StatusDescription[ConnectionStatus][] = {
"Newbie",
"Semi-Pro",
"Pro",
/* "UltraPro", */
"Veteran"
}
enum StatusLevelRange {
Floor,
Ceiling
}
new const StatusLevels[ConnectionStatus][StatusLevelRange] = {
{ 0 , 100 },
{ 101 , 500 },
{ 501 , 750 },
{ 751 , 99999 }
/* { 751 , 1000 }, // UltraPro*/
/* { 1000 , 99999 } // Veteran*/
}
// =================================================================================================
new const Teams[ CsTeams ][] = {
"(Unassigned)",
"(Terrorist)",
"(Counter-Terrorist)",
"(Spectator)"
}
enum PlayerData {
Name[33],
AuthID[35],
Connections,
Status,
PlayTime,
LeaveMsg[33]
}
// =================================================================================================
const MAX_PLAYERS = 32
new Float:lastmsg[33]
new Handle:g_iSQLTuple
new g_szDBQueryCache[1024]
new g_PData[MAX_PLAYERS + 1][PlayerData]
new g_iConnectTime[MAX_PLAYERS + 1]
new bool:g_bIsBot[MAX_PLAYERS + 1]
new g_iMsgSayText
new g_iMaxPlayers
new g_pAllChat
new g_pAllChatS
new connection_host
new connection_user
new connection_pass
new Trie:ghTrie;
// =================================================================================================
public plugin_init() {
register_plugin("Connections and Time", "0.6", "bugsy")
// ======================================================
register_clcmd("say", "HookSay")
register_clcmd("say_team", "HookSay_Team")
// ==================================================
connection_host = register_cvar("connection_sql_host", "localhost")
connection_user = register_cvar("connection_sql_user", "root")
connection_pass = register_cvar("connection_sql_pass", "toosecretforyou", FCVAR_PROTECTED)
g_pAllChat = register_cvar("cat_allchat" , "0")
g_pAllChatS = register_cvar("cat_spam", "1.0")
g_iMsgSayText = get_user_msgid("SayText")
g_iMaxPlayers = get_maxplayers()
// ==================================================
register_forward(FM_ClientPutInServer, "onPost", true);
ghTrie = TrieCreate()
}
// =================================================================================================
bool:CheckSpam(const id) {
new Float:gametime = get_gametime()
if(is_user_connected(id)) {
if(lastmsg[id] < gametime) {
lastmsg[id] = gametime + get_pcvar_float(g_pAllChatS)
return false
}
}
return true
}
// =================================================================================================
public HookSay(id) {
if(CheckSpam(id)) { return PLUGIN_HANDLED; }
// ========================================
static szMessage[256], szMsgSaid[256]
new bool:bIsAlive , bool:bAllChat
read_args(szMsgSaid, charsmax(szMsgSaid))
remove_quotes(szMsgSaid)
if(equal(szMsgSaid, "")) { return PLUGIN_HANDLED; }
// leavemsg
if(ConnectionStatus:g_PData[id][Status] == Veteran) {
if(szMsgSaid[0] == '/' && szMsgSaid[2] == 'e' && szMsgSaid[4] == 'v' && szMsgSaid[6] == 'm') {
new Arg[64]
strbreak(szMsgSaid, szMsgSaid, 95, Arg, 63)
remove_quotes(Arg)
if(!Arg[0]) {
if(g_PData[id][LeaveMsg][0]) {
client_print(id, print_chat, "* Your current leave message is ^"%s^"", g_PData[id][LeaveMsg])
} else {
client_print(id ,print_chat, "* You haven't set a leave message yet. To do so, type /leavemsg ^"message^".")
}
}
else if(equali(Arg , "remove")) {
g_PData[id][LeaveMsg][0] = EOS
client_print(id, print_chat , "* Your leave message was removed.")
} else {
copy(g_PData[id][LeaveMsg], charsmax(g_PData[][LeaveMsg]), Arg)
client_print(id, print_chat, "* Your leave message has been set to ^"%s^"", Arg)
}
return PLUGIN_HANDLED
}
}
// playtime
if(szMsgSaid[0] == '/' && szMsgSaid[1] == 'p' && szMsgSaid[4] == 'y' && szMsgSaid[7] == 'm') {
new iHours, iDays, iMinutes, iCurTime
static szPlayTime[128], iPos
formatex(szMessage, charsmax(szMessage), "^x04*^x03 You currently have^x04 %d^x03 connections. [^x04Status: %s^x03]" , g_PData[id][Connections], StatusDescription[ConnectionStatus:g_PData[id][Status]])
Print(id, id, szMessage)
iCurTime = g_PData[id][PlayTime] + (get_systime() - g_iConnectTime[id])
iPos = 0
if(iCurTime >= 86400) {
iDays = iCurTime / 86400
iCurTime -= iDays * 86400
iPos = formatex(szPlayTime, charsmax(szPlayTime), "%d days, ", iDays)
}
if(iCurTime >= 3600) {
iHours = iCurTime / 3600
iCurTime -= iHours * 3600
iPos += formatex(szPlayTime[iPos], charsmax(szPlayTime) - iPos, "%d hour%s, ", iHours, iHours > 1 ? "s" : "")
}
if(iCurTime >= 60) {
iMinutes = iCurTime / 60
iCurTime -= iMinutes * 60
iPos += formatex(szPlayTime[iPos], charsmax(szPlayTime) - iPos, "%d minute%s, ", iMinutes, iMinutes > 1 ? "s" : "")
}
if(iCurTime) {
iPos += formatex(szPlayTime[iPos], charsmax(szPlayTime) - iPos, "%d second%s, ", iCurTime, iCurTime > 1 ? "s" : "")
}
szPlayTime[ iPos - 2 ] = EOS
formatex(szMessage, charsmax(szMessage), "^x04*^x03 You have played on this server for:^x04 %s", szPlayTime)
Print(id, id, szMessage)
return PLUGIN_HANDLED
}
bIsAlive = bool:!!is_user_alive(id)
bAllChat = bool:!!get_pcvar_num(g_pAllChat)
if(bIsAlive)
formatex(szMessage, charsmax(szMessage), "^x04[%s]^x03 %s^x01 : %s", StatusDescription[ConnectionStatus:g_PData[id][Status]] , g_PData[id][Name] , szMsgSaid)
else
formatex(szMessage, charsmax(szMessage), "^x04[%s]^x01 %s^x03 %s^x01 : %s", StatusDescription[ConnectionStatus:g_PData[id][Status]], (cs_get_user_team(id) != CS_TEAM_SPECTATOR) ? "*DEAD*" : "*SPEC*" , g_PData[id][Name], szMsgSaid)
for(new iPlayer = 1; iPlayer <= g_iMaxPlayers; iPlayer++)
if(is_user_connected(iPlayer) && (bAllChat || (bIsAlive || (!bIsAlive && !is_user_alive(iPlayer)))))
Print(iPlayer, id, szMessage)
return PLUGIN_HANDLED
}
// =================================================================================================
public HookSay_Team(id) {
if(CheckSpam(id)) { return PLUGIN_HANDLED; }
// ========================================
static szMessage[256], szMsgSaid[256]
new CsTeams:iTeam, bool:bIsAlive, bool:bAllChat
read_args(szMsgSaid, charsmax(szMsgSaid))
remove_quotes(szMsgSaid)
if(equal(szMsgSaid, "")) { return PLUGIN_HANDLED; }
iTeam = cs_get_user_team(id)
bIsAlive = bool:!!is_user_alive(id)
bAllChat = bool:!!get_pcvar_num(g_pAllChat)
if(bIsAlive || (iTeam == CS_TEAM_SPECTATOR))
formatex(szMessage, charsmax(szMessage), "^x04[%s]^x01 %s^x03 %s^x01 : %s", StatusDescription[ConnectionStatus:g_PData[id][Status]], Teams[iTeam], g_PData[id][Name], szMsgSaid)
else
formatex(szMessage, charsmax(szMessage), "^x04[%s]^x01 *DEAD* %s^x03 %s^x01 : %s", StatusDescription[ConnectionStatus:g_PData[id][Status]], Teams[iTeam], g_PData[id][Name], szMsgSaid)
for(new iPlayer = 1; iPlayer <= g_iMaxPlayers; iPlayer++ )
if((is_user_connected(iPlayer) && (cs_get_user_team(iPlayer) == iTeam)) && (bAllChat || (bIsAlive || (!bIsAlive && !is_user_alive(iPlayer)))))
Print(iPlayer, id, szMessage)
return PLUGIN_HANDLED
}
// =================================================================================================
Print(id, colorid, szMessage[]) {
message_begin(id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, g_iMsgSayText, {0,0,0}, id)
write_byte(colorid)
write_string(szMessage)
message_end()
}
public ShowJoinMsg(id) {
static szMessage[128]
formatex(szMessage, charsmax(szMessage), "^x04* ^x03%s has joined with^x04 %d^x03 connections. [^x04Status: %s^x03]", g_PData[id][Name], g_PData[id][Connections], StatusDescription[ConnectionStatus:g_PData[id][Status]])
Print(0, 1, szMessage)
}
// *************************************************************************************************
// This is the part ot the plugin that working the information with sql
// *************************************************************************************************
public plugin_cfg() {
new szHost[64], szUser[64], szPass[64]
get_pcvar_string(connection_host, szHost, charsmax(szHost))
get_pcvar_string(connection_user, szUser, charsmax(szUser))
get_pcvar_string(connection_pass, szPass, charsmax(szPass))
g_iSQLTuple = SQL_MakeDbTuple(szHost, szUser, szPass, NAMEOF_DB)
formatex(g_szDBQueryCache, 1023, CREATE_DB)
SQL_ThreadQuery(g_iSQLTuple, "DefaultHandle", g_szDBQueryCache)
}
public ClearValueFromTrie(params[]) {
static szAddress[32];
copy(szAddress, 31, params);
TrieDeleteKey(ghTrie , szAddress);
return PLUGIN_CONTINUE;
}
// =================================================================================================
public plugin_end() { SQL_FreeHandle(g_iSQLTuple); TrieClear(ghTrie); }
// =================================================================================================
public SearchHandle(FailState, Handle:Query, Error[], Errcode, Data[], DataSize) {
if(FailState == TQUERY_CONNECT_FAILED)
return set_fail_state("Could not connect to SQL database.")
else if(FailState == TQUERY_QUERY_FAILED)
return set_fail_state("SQL Query failed.")
if(Errcode)
return log_amx("SQL Error on query: %s", Error)
static szData[128], szConnections[5], szStatus[2], szPlayTime[8]
new id = Data[0]
if(SQL_MoreResults(Query)) {
SQL_ReadResult(Query, 0, szData, charsmax(szData))
parse(szData, g_PData[id][Name], charsmax(g_PData[][Name]), g_PData[id][AuthID], charsmax(g_PData[][AuthID]), szConnections, charsmax(szConnections), szStatus , charsmax(szStatus), szPlayTime, charsmax(szPlayTime), g_PData[id][LeaveMsg], charsmax(g_PData[][LeaveMsg]))
g_PData[id][Connections] = str_to_num(szConnections)
g_PData[id][PlayTime] = str_to_num(szPlayTime)
}
g_PData[id][Connections]++
g_iConnectTime[id] = get_systime()
for(new ConnectionStatus:i = Newbie; i < Veteran; i++) {
if(StatusLevels[i][Floor] <= g_PData[id][Connections] <= StatusLevels[i][Ceiling]) {
g_PData[id][Status] = _:i
break
}
}
get_user_name(id, g_PData[id][Name], charsmax(g_PData[][Name]))
set_task(6.0, "ShowJoinMsg", id)
return PLUGIN_CONTINUE
}
// =================================================================================================
public DefaultHandle(FailState, Handle:Query, Error[], Errcode, Data[], DataSize) {
if(FailState == TQUERY_CONNECT_FAILED)
return set_fail_state("Could not connect to SQL database.")
else if(FailState == TQUERY_QUERY_FAILED)
return set_fail_state("SQL Query failed.")
if(Errcode)
return log_amx("SQL Error on query: %s", Error)
return PLUGIN_CONTINUE
}
// =================================================================================================
public onPost(id, const szName[] , const szAddress[], const szRejectReason[128]) {
if (pev(id, pev_flags) & FL_FAKECLIENT) { return FMRES_IGNORED; }
if (szAddress[0] == 0) { return FMRES_IGNORED; }
static iConnectionCount;
static params[32];
if (!TrieGetCell(ghTrie, szAddress, iConnectionCount)) {
g_bIsBot[id] = bool:!!is_user_bot(id)
if(g_bIsBot[id]) return PLUGIN_CONTINUE
copy(params, 31, szAddress);
set_task(fClearTrieTime, "ClearValueFromTrie", 0, params, 32);
TrieSetCell( ghTrie , szAddress, 0 );
get_user_authid(id, g_PData[id][AuthID], charsmax(g_PData[][AuthID]))
new iData[1]
iData[0] = id
formatex(g_szDBQueryCache, 255, SEARCH_DB , g_PData[id][AuthID])
SQL_ThreadQuery(g_iSQLTuple, "SearchHandle", g_szDBQueryCache, iData, sizeof(iData))
return PLUGIN_CONTINUE
}
if (iConnectionCount == iConnectionBanCount) {
set_task(3.0,"fight_him", id)
return FMRES_HANDLED
}
TrieSetCell(ghTrie, szAddress, (iConnectionCount+1));
return FMRES_IGNORED;
}
public fight_him(id) {
new szName[32]
get_user_name(id, szName, charsmax(szName))
server_cmd("amx_ban %s 1 SPAM", szName);
}
// =================================================================================================
public client_infochanged(id) {
static szNewName[33]
get_user_info(id, "name", szNewName, 32)
if(!equal(g_PData[id][Name], szNewName))
copy(g_PData[id][Name], charsmax(g_PData[][Name]), szNewName)
}
// =================================================================================================
public client_disconnect(id) {
static szMessage[128]
if(g_bIsBot[id]) return PLUGIN_CONTINUE
remove_task(id)
g_PData[id][PlayTime] += (get_systime() - g_iConnectTime[id])
formatex(szMessage, charsmax(szMessage), "^"%s^" ^"%s^" ^"%d^" ^"%d^" ^"%d^" ^"%s^"", g_PData[id][Name], g_PData[id][AuthID], g_PData[id][Connections], g_PData[id][Status], g_PData[id][PlayTime], g_PData[id][LeaveMsg])
formatex(g_szDBQueryCache, 511, INSERT_DB, g_PData[id][AuthID], szMessage, get_systime())
SQL_ThreadQuery(g_iSQLTuple, "DefaultHandle" ,g_szDBQueryCache)
if(ConnectionStatus:g_PData[id][Status] == Veteran) {
formatex(szMessage, charsmax(szMessage), "^x04* ^x03Veteran: %s has left the server. [^x04Message: %s^x03]", g_PData[id][Name], g_PData[id][LeaveMsg])
Print(0, 1, szMessage)
}
g_PData[id][Name][0] = EOS
g_PData[id][AuthID][0] = EOS
g_PData[id][Connections] = 0
g_PData[id][Status] = 0
g_PData[id][PlayTime] = 0
g_PData[id][LeaveMsg][0] = EOS
g_iConnectTime[id] = 0
return PLUGIN_CONTINUE
}
// *************************************************************************************************
// #### #### ###### #### ## ## ###### ## ## ######
// ## ## ## ## ## ## ## # ### ## ## ## ## ##
// ## ### ## ###### ## # ### ###### ### ##
// ## ## ## ## #### ## ## ## ## ## ######
// *************************************************************************************************
Please do not using in public servers, this version is not working, it's in alpha stage for new update.
__________________
As soon as possible.
Last edited by xakintosh; 10-22-2012 at 17:03.
|
|