Hello. My server is crashing once a day, sometimes twice.
Afeter crash plugin psychoingame can't connect with database. Do You think that crash is created by psychoingame plugin? AMX Bans can connect with DB after crash...
Code:
/*
* Script is created for PsychoStats 3.1 by lame coder from amxmodx.pl :D
* It has such futures like:
* - Colored rank from PS database.
* - Join and leave annoucment with ranks and skill information.
* - TOP15 and RANKSTATS from DB.
* - Registration system from game.
* - It checks STEAM_ID and create an account for player.
* - Password is MD5 hashed
* - It has system for prevent double logins and accounts
* 1 account per 1 STEAM_ID
* ID from WWW account is automaticly sat to Game Profile
*
* Plugin Created by TRAMP
* You can find me:
* [email protected]
* http://ster-gaming.pl
* http://amxmodx.pl
* http://g4g.pl
*
* CHANELOG
*
* 0.1
* I added join and leave annoucment + colored RANK
*
* 0.2
* TOP15, rankstats and registry system added.
*
* 0.3
* Fixed some bugs
*
* 0.4
* Fixed more bugs
* Optimized code
* Fixed SQL functions
*
* TO DO:
* - Real time statistics
* - Clear code from mess and optimize it :-)
*
*
* It can be share only on psychostats.com and amxmodx.pl
* You are not allowed to spread it to other sites.
*
*
* */
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
#define PLUGIN "InGame PS 3.1"
#define AUTHOR "Tramp/FiFiX"
#define VERSION "0.4"
#define MAXSLOTS 32
#define MAX_BUFFER_LENGTH 2047
#define MAX_NAME_LENGTH 31
new Handle:g_SqlTuple;
new Handle:g_SqlConnection;
new g_psprefix[64];
new g_site[64];
new g_sBuffer[MAX_BUFFER_LENGTH + 1] = ""
new t_sName[MAX_NAME_LENGTH + 1] = ""
enum Color
{
YELLOW = 1, // Yellow
GREEN, // Green Color
TEAM_COLOR, // Red, grey, blue
GREY, // grey
RED, // Red
BLUE, // Blue
}
new TeamInfo;
new SayText;
new MaxSlots;
new TeamName[][] =
{
"",
"TERRORIST",
"CT",
"SPECTATOR"
}
new bool:IsConnected[MAXSLOTS + 1];
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR);
register_cvar("tramp_ps_version", VERSION, FCVAR_SERVER|FCVAR_SPONLY);
TeamInfo = get_user_msgid("TeamInfo");
SayText = get_user_msgid("SayText");
MaxSlots = get_maxplayers();
register_clcmd("say /statsme", "show_rank", 0, "- display your rank");
register_clcmd("say /rank", "show_rank", 0, "- display your rank");
register_clcmd("say /top15", "cmdTop15", 0, "- display top 15 players (MOTD)")
register_clcmd("say /rankstats", "cmdRankStats", 0, "- display your server stats (MOTD)")
register_clcmd("say_team /statsme", "show_rank", 0, "- display your rank");
register_clcmd("say_team /rank", "show_rank", 0, "- display your rank");
register_clcmd("say_team /top15", "cmdTop15", 0, "- display top 15 players (MOTD)")
register_clcmd("say_team /rankstats", "cmdRankStats", 0, "- display your server stats (MOTD)")
register_concmd("ps_reg", "register", 0, "username password ")
register_cvar("ps_host","",FCVAR_PROTECTED);
register_cvar("ps_user","",FCVAR_PROTECTED);
register_cvar("ps_pass","",FCVAR_PROTECTED);
register_cvar("ps_db","",FCVAR_PROTECTED);
register_cvar("ps_prefix", "")
register_cvar("ps_site", "")
register_dictionary("ps31.txt")
set_task(360.0, "ads", 7777, "", 0, "b");
new Host[64],User[64],Pass[64],Db[64],error[255];
get_cvar_string("ps_host",Host,63);
get_cvar_string("ps_user",User,63);
get_cvar_string("ps_pass",Pass,63);
get_cvar_string("ps_db",Db,63);
get_cvar_string("ps_prefix", g_psprefix, 63);
get_cvar_string("ps_site", g_site, 63);
new configsDir[64] ;
get_configsdir(configsDir, 63);
server_cmd("exec %s/sql.cfg", configsDir)
g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db);
new errorcode;
g_SqlConnection = SQL_Connect(g_SqlTuple,errorcode,error,254);
if (!g_SqlConnection)
{
log_amx("[PS3.1] Server cannot connect to database.");
log_amx("[PS3.1] Error(%d): %s",errorcode,error);
return;
}
log_amx("[PS3.1] Connection Succesfull!");
}
public ads()
{
for (new i = 1; i <= 32; i++)
{
if (is_user_connected(i))
{
ColorChat(i, GREY, "%L", LANG_SERVER, "PS_ADS1", g_site);
ColorChat(i, GREY, "%L", LANG_SERVER, "PS_ADS2");
}
}
}
public client_putinserver(player)
{
IsConnected[player] = true;
}
////////////////////////////////////
//Wait 10 seconds and run show_skill
////////////////////////////////////
public client_authorized(id)
{
set_task(10.0, "show_skill", id);
}
/////////////////////////////////////////////////////////////
//Give information about player who has just disconnected.
/////////////////////////////////////////////////////////////
public client_disconnect(player)
{
IsConnected[player] = false;
new authid[32]
new name[32]
new skill;
new rank;
get_user_authid(player,authid,31);
get_user_name(player,name,31);
new Handle:sery = SQL_PrepareQuery(g_SqlConnection,"SELECT rank, skill FROM %splr WHERE uniqueid='%s'", g_psprefix, authid);
SQL_Execute(sery);
if (SQL_AffectedRows(sery))
{
rank = SQL_ReadResult(sery, 0);
skill = SQL_ReadResult(sery, 1);
//client_print(0,print_chat,"Twoj skill : %u ", StoreOther);
ColorChat(0, YELLOW, "%L", LANG_SERVER, "PS_DISC", name, authid, skill, rank);
}
SQL_FreeHandle(sery);
}
/////////////////////////////////////////////////////////////
//Give information about player who has just joined the game.
/////////////////////////////////////////////////////////////
public show_skill(id)
{
new authid[32]
new name[32]
new StoreOther;
new rank;
get_user_authid(id,authid,31);
get_user_name(id,name,31);
new Handle:Query = SQL_PrepareQuery(g_SqlConnection,"SELECT rank, skill FROM %splr WHERE uniqueid='%s'", g_psprefix, authid);
SQL_Execute(Query);
if (SQL_AffectedRows(Query))
{
rank = SQL_ReadResult(Query, 0);
StoreOther = SQL_ReadResult(Query, 1);
//client_print(0,print_chat,"Twoj skill : %u ", StoreOther);
ColorChat(0, YELLOW, "%L", LANG_SERVER, "PS_JOIN", name, authid, StoreOther, rank);
}
SQL_FreeHandle(Query);
}
//////////////////////
//Colored RANK in chat
//////////////////////
public show_rank(id)
{
new authid[32]
new iRank, iRanked;
new iKills, iDeaths, iHits, fSkill;
//new Float:fSkill;
new fAccuracy;
//new name[32]
get_user_authid(id,authid,31);
//get_user_name(id,name,31)
new Handle:kery = SQL_PrepareQuery(g_SqlConnection,"SELECT plr.rank, (SELECT COUNT( * ) FROM %splr plr WHERE plr.allowrank = 1) AS ranked, d.kills, d.deaths, d.hits, plr.skill, d.accuracy FROM %sc_plr_data as d, %splr as plr, %splr_profile as pp WHERE plr.plrid=d.plrid AND plr.uniqueid=pp.uniqueid AND pp.uniqueid='%s' AND plr.rank > 0 AND plr.allowrank = 1 ORDER BY plr.rank ASC LIMIT 1", g_psprefix, g_psprefix, g_psprefix, g_psprefix, authid);
SQL_Execute(kery);
if (SQL_AffectedRows(kery))
{
iRank = SQL_ReadResult(kery, 0);
iRanked = SQL_ReadResult(kery, 1);
iKills = SQL_ReadResult(kery, 2);
iDeaths = SQL_ReadResult(kery, 3);
iHits = SQL_ReadResult(kery, 4);
//SQL_ReadResult(kery, 5, fSkill);
fSkill = SQL_ReadResult(kery, 5);
fAccuracy = SQL_ReadResult(kery, 6);
//SQL_ReadResult(kery, 6, fAccuracy);
ColorChat(id, BLUE, "%L ^x04[%s]", LANG_SERVER, "PS_RAN1", g_site);
ColorChat(id, GREY, "%L", LANG_SERVER, "PS_RAN2", iRank, iRanked, fSkill, iKills, iDeaths, iHits, fAccuracy);
}
SQL_FreeHandle(kery);
}
///////////////
//TOP 15///////
///////////////
public cmdTop15(id)
{
ColorChat(id, GREY, "%L", LANG_SERVER, "PS_TOP1");
format_top15(g_sBuffer);
show_motd(id, g_sBuffer, "Top 15");
return PLUGIN_CONTINUE
}
///////////////
//TOP 15///////
///////////////
format_top15(sBuffer[MAX_BUFFER_LENGTH + 1])
{
new iLen = 0;
new sName[32];
new iRank, iKills, iDeaths, iHits, iShots, iHeadshotkills;
new Float:fSkill, Float:fAccuracy;
new Kills[16], Deaths[16], Hits[16], Shots[16];
format(Kills, 15, "%L", LANG_SERVER, "PS_KILL");
format(Deaths, 15, "%L", LANG_SERVER, "PS_DEAT");
format(Hits, 15, "%L", LANG_SERVER, "PS_HITS");
format(Shots, 15, "%L", LANG_SERVER, "PS_SHOT");
iLen = format(sBuffer, MAX_BUFFER_LENGTH, "<body bgcolor=#000000><font color=#FFB000><pre>");
iLen += format(sBuffer[iLen], MAX_BUFFER_LENGTH - iLen, "%2s %-22.22s %6s %6s %6s %6s %4s %4s %4s^n", "#", "Nick", Kills, Deaths, Hits, Shots, "HS", "Skill", "Acc");
new Handle:top = SQL_PrepareQuery(g_SqlConnection,"SELECT d.rank, m.name, k.kills, k.deaths, k.hits, k.shots, k.headshotkills, d.skill, k.accuracy FROM %splr AS d, %splr_profile AS m, %sc_plr_data AS k WHERE d.allowrank =1 AND d.uniqueid = m.uniqueid AND d.plrid = k.plrid ORDER BY d.skill DESC LIMIT 15", g_psprefix, g_psprefix, g_psprefix);
SQL_Execute(top);
if (SQL_AffectedRows(top))
for (new i = 1; i < 16; i++)
{
iRank = SQL_ReadResult(top, 0);
SQL_ReadResult(top, 1, sName, 31);
iKills = SQL_ReadResult(top, 2);
iDeaths = SQL_ReadResult(top, 3);
iHits = SQL_ReadResult(top, 4);
iShots = SQL_ReadResult(top, 5);
iHeadshotkills = SQL_ReadResult(top, 6);
SQL_ReadResult(top, 7, fSkill);
SQL_ReadResult(top, 8, fAccuracy);
iLen += format(sBuffer[iLen], MAX_BUFFER_LENGTH - iLen, "%2d %-22.22s %6d %6d %6d %6d %4d %3.0f%% %3.0f%%^n", iRank, sName, iKills, iDeaths, iHits, iShots, iHeadshotkills, fSkill, fAccuracy )
//Goes to next row in DB and get data
SQL_NextRow(top);
}
SQL_FreeHandle(top);
}
///////////////////////
//Starter for rankstats
///////////////////////
public cmdRankStats(id)
{
format_rankstats(id, g_sBuffer);
get_user_name(id, t_sName, MAX_NAME_LENGTH);
show_motd(id, g_sBuffer, t_sName);
return PLUGIN_CONTINUE
}
////////////////////
//That is /rankstats
////////////////////
format_rankstats(id, sBuffer[MAX_BUFFER_LENGTH + 1])
{
new iLen = 0;
new iRank, iRanked, iKills, iDeaths, iHits, iShots, iHeadshotkills, iDamage;
new Float:fAccuracy;
new authid[32];
get_user_authid(id,authid,31);
//new lKills[16], lDeaths[16], lHits[16], lShots[16], lSkill[16], lEff[16], lAcc[16];
//format(lKills, 15, "%L", LANG_SERVER, "KILLS");
//format(lDeaths, 15, "%L", LANG_SERVER, "DEATHS");
//format(lHits, 15, "%L", LANG_SERVER, "HITS");
//format(lShots, 15, "%L", LANG_SERVER, "SHOTS");
//format(lSkill, 15, "%L", LANG_SERVER, "SHOTS");
//format(lAcc, 15, "%L", LANG_SERVER, "ACC");
iLen = format(sBuffer, MAX_BUFFER_LENGTH, "<body bgcolor=#000000><font color=#FFB000><pre>");
//iLen += format(sBuffer[iLen], MAX_BUFFER_LENGTH - iLen, "%2s %-22.22s %6s %6s %6s %6s %4s %4s %4s^n", "#", "Nick", "Kills", "Deaths", "Hits", "Shots", "HS", "Skill", "Acc");
new Handle:rs = SQL_PrepareQuery(g_SqlConnection,"SELECT d.plrid FROM %splr AS d ORDER BY d.plrid DESC LIMIT 1", g_psprefix);
new Handle:rs2 = SQL_PrepareQuery(g_SqlConnection,"SELECT d.rank, k.kills, k.headshotkills, k.deaths, k.hits, k.shots, k.damage, k.accuracy FROM %splr AS d, %sc_plr_data AS k WHERE d.allowrank =1 AND d.plrid = k.plrid AND d.uniqueid = '%s'", g_psprefix, g_psprefix, authid);
SQL_Execute(rs);
SQL_Execute(rs2);
if (SQL_AffectedRows(rs2))
{
iRank = SQL_ReadResult(rs2, 0);
iRanked = SQL_ReadResult(rs, 0);
iKills = SQL_ReadResult(rs2, 1);
iHeadshotkills = SQL_ReadResult(rs2, 2);
iDeaths = SQL_ReadResult(rs2, 3);
iHits = SQL_ReadResult(rs2, 4);
iShots = SQL_ReadResult(rs2, 5);
iDamage = SQL_ReadResult(rs2, 6);
SQL_ReadResult(rs2, 7, fAccuracy)
//iLen += format(sBuffer[iLen], MAX_BUFFER_LENGTH - iLen, "%2d %-22.22s %6d %6d %6d %6d %4d %3.0f%% %3.0f%%^n", iRank, sName, iKills, iDeaths, iHits, iShots, iHeadshotkills, fSkill, fAccuracy );
iLen += format(sBuffer[iLen], MAX_BUFFER_LENGTH - iLen, "%L", LANG_SERVER, "PS_STAT", iRank, iRanked, iKills, iHeadshotkills, iDeaths, iHits, iShots, iDamage, fAccuracy);
}
SQL_FreeHandle(rs);
SQL_FreeHandle(rs2);
}
public register(id, level, cid)
{
//Check all data
if (!cmd_access(id, level, cid, 3))
{
console_print(id, "%L", LANG_SERVER, "PS_REG1");
return PLUGIN_HANDLED
}
new Arg1[16];
new Arg2[16];
new authid[32];
get_user_authid(id,authid,31);
read_argv(1, Arg1, 15); //Get username
read_argv(2, Arg2, 15); //Get password
new lr1 = strlen(Arg1); //Count chars in username
new lr2 = strlen(Arg2); //Cound chars in password
//////////////////////////////////////////////////////////
//Check chars for username and password if bad stop plugin
//////////////////////////////////////////////////////////
if (lr1 <= 4 || lr1 >= 15)
{
console_print(id, "%L", LANG_SERVER, "PS_REG2");
return PLUGIN_HANDLED
}
if (lr2 <= 4 || lr2 >= 15)
{
console_print(id, "%L", LANG_SERVER, "PS_REG3");
return PLUGIN_HANDLED
}
/////////////////////////////////////
//Prevent double username in database
/////////////////////////////////////
new Handle:duble = SQL_PrepareQuery(g_SqlConnection,"SELECT username = '%s' FROM %suser LIMIT 1", Arg1, g_psprefix);
SQL_Execute(duble);
if (SQL_AffectedRows(duble))
{
console_print(id, "%L", LANG_SERVER, "PS_REG4");
return PLUGIN_HANDLED
}
SQL_FreeHandle(duble);
/////////////////////////////////
//We are going to register player.
/////////////////////////////////
//How many users we have in database. Needed to set good ID in ps3_user table
new Handle:us = SQL_PrepareQuery(g_SqlConnection,"SELECT userid FROM %suser ORDER BY userid DESC LIMIT 1", g_psprefix);
//Is user ranked? Only ranked users can register an account
new Handle:us2 = SQL_PrepareQuery(g_SqlConnection,"SELECT allowrank FROM %splr WHERE uniqueid = '%s'", g_psprefix, authid);
//If get NULL user is unregistered, so it will allow to make register query to SQL
new Handle:us3 = SQL_PrepareQuery(g_SqlConnection,"SELECT userid FROM %splr_profile WHERE uniqueid = '%s'", g_psprefix, authid);
SQL_Execute(us);
SQL_Execute(us2);
SQL_Execute(us3);
if (SQL_AffectedRows(us))
{
new users = SQL_ReadResult(us, 0);
new alow = SQL_ReadResult(us2, 0);
new reg = SQL_ReadResult(us3, 0);
//ColorChat(id, GREY, "We have %d users in database", users);
//ColorChat(id, GREY, "Masz: %d", alow);
//ColorChat(id, GREY, "Jestes: %d", reg);
SQL_FreeHandle(us);
SQL_FreeHandle(us2);
SQL_FreeHandle(us3);
//Check if player is ranked and unregisted
if (alow == 1 && reg == 0)
{
new dbid = users + 1;
new Handle:hasz = SQL_PrepareQuery(g_SqlConnection,"INSERT INTO %suser (userid, username, password, session_last, session_login_key, lastvisit, accesslevel, confirmed) VALUES (%d, '%s', MD5('%s'), 0, NULL, 0, 2, 1)", g_psprefix, dbid, Arg1, Arg2);
new Handle:hasz2 = SQL_PrepareQuery(g_SqlConnection,"UPDATE %splr_profile SET userid = %d WHERE uniqueid = '%s'", g_psprefix, dbid, authid);
SQL_Execute(hasz);
SQL_Execute(hasz2);
SQL_FreeHandle(hasz);
SQL_FreeHandle(hasz2);
console_print(id, "%L", LANG_SERVER, "PS_REG5", g_site);
}
console_print(id, "%L", LANG_SERVER, "PS_REG6");
}
else
{
new users = 0;
new alow = SQL_ReadResult(us2, 0);
new reg = SQL_ReadResult(us3, 0);
//ColorChat(id, GREY, "We have %d users in database", users);
//ColorChat(id, GREY, "Masz: %d", alow);
//ColorChat(id, GREY, "Jestes: %d", reg);
SQL_FreeHandle(us);
SQL_FreeHandle(us2);
SQL_FreeHandle(us3);
//Check if player is ranked and unregisted
if (alow == 1 && reg == 0)
{
new dbid = users + 1;
new Handle:hasz = SQL_PrepareQuery(g_SqlConnection,"INSERT INTO %suser (userid, username, password, session_last, session_login_key, lastvisit, accesslevel, confirmed) VALUES (%d, '%s', MD5('%s'), 0, NULL, 0, 2, 1)", g_psprefix, dbid, Arg1, Arg2);
new Handle:hasz2 = SQL_PrepareQuery(g_SqlConnection,"UPDATE %splr_profile SET userid = %d WHERE uniqueid = '%s'", g_psprefix, dbid, authid);
SQL_Execute(hasz);
SQL_Execute(hasz2);
SQL_FreeHandle(hasz);
SQL_FreeHandle(hasz2);
console_print(id, "%L", LANG_SERVER, "PS_REG5", g_site);
}
console_print(id, "%L", LANG_SERVER, "PS_REG6");
}
return PLUGIN_HANDLED
}
/////////////////CHAT_COLORS//////////////////////////////////////
public ColorChat(id, Color:type, const msg[], {Float,Sql,Result,_}:...)
{
static message[256];
switch(type)
{
case YELLOW: // Yellow
{
message[0] = 0x01;
}
case GREEN: // Green
{
message[0] = 0x04;
}
default: // White, Red, Blue
{
message[0] = 0x03;
}
}
vformat(message[1], 251, msg, 4);
// Make sure message is not longer than 192 character. Will crash the server.
message[192] = '^0';
new team, ColorChange, index, MSG_Type;
if(!id)
{
index = FindPlayer();
MSG_Type = MSG_ALL;
} else {
MSG_Type = MSG_ONE;
index = id;
}
team = get_user_team(index);
ColorChange = ColorSelection(index, MSG_Type, type);
ShowColorMessage(index, MSG_Type, message);
if(ColorChange)
{
Team_Info(index, MSG_Type, TeamName[team]);
}
}
ShowColorMessage(id, type, message[])
{
message_begin(type, SayText, _, id);
write_byte(id)
write_string(message);
message_end();
}
Team_Info(id, type, team[])
{
message_begin(type, TeamInfo, _, id);
write_byte(id);
write_string(team);
message_end();
return 1;
}
ColorSelection(index, type, Color:Type)
{
switch(Type)
{
case RED:
{
return Team_Info(index, type, TeamName[1]);
}
case BLUE:
{
return Team_Info(index, type, TeamName[2]);
}
case GREY:
{
return Team_Info(index, type, TeamName[0]);
}
}
return 0;
}
FindPlayer()
{
new i = -1;
while(i <= MaxSlots)
{
if(IsConnected[++i])
{
return i;
}
}
return -1;
}