New Member
|
04-17-2017
, 06:46
Re: [REQ/CSGO] Player name changer basted on SteamID
|
#6
|
Quote:
Originally Posted by Headline
PHP Code:
/* [CS:GO] Name Enforcer for esportdk
*
* Copyright (C) 2016 Michael Flaherty // michaelwflaherty.com // [email protected]
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see http://www.gnu.org/licenses/.
*
* Thanks to ThatOneGuy for SQL snippets
*/
#include <sourcemod>
#include <sdktools>
#pragma newdecls required
#pragma semicolon 1
#define PLUGIN_VERSION "1.1"
Database myDatabase = null;
char ga_sSteamID[MAXPLAYERS + 1][32];
char ga_sForcedName[MAXPLAYERS + 1][MAX_NAME_LENGTH];
bool ga_bLoaded[MAXPLAYERS + 1] = {false, ...};
bool g_bLateLoad;
public APLRes AskPluginLoad2(Handle hMyself, bool bLate, char[] sError, int err_max)
{
g_bLateLoad = bLate;
return APLRes_Success;
}
public Plugin myinfo =
{
name = "[CS:GO] Name Enforcer",
author = "Headline",
description = "A simple MySQL name enforcer",
version = PLUGIN_VERSION,
url = "http://www.michaelwflaherty.com"
}
public void OnPluginStart()
{
HookEvent("player_changename", Event_NameChange);
ConnectDB();
}
/* Configs are good! We can load in */
public void OnAutoConfigsBuffered()
{
ConnectDB();
}
public void OnConfigsExecuted()
{
/* Database has not been set yet, so do it. */
if (myDatabase == null)
{
ConnectDB();
}
/* If the plugin was loaded in late, we want to load in every client now. */
if (g_bLateLoad)
{
for (int i = 1; i <= MaxClients; i++)
{
if (IsValidClient(i))
{
GetClientAuthId(i, AuthId_Steam2, ga_sSteamID[i], sizeof(ga_sSteamID[]));
ReplaceString(ga_sSteamID[i], sizeof(ga_sSteamID[]), "STEAM_1", "STEAM_0");
if (StrContains(ga_sSteamID[i], "STEAM_", true) != -1) // valid
{
LoadClient(i);
}
else // invalid try again
{
CreateTimer(10.0, RefreshSteamID, GetClientUserId(i), TIMER_FLAG_NO_MAPCHANGE);
}
}
}
}
}
public void OnClientConnected(int client)
{
ResetClientVars(client);
}
public void OnClientDisconnect(int client)
{
ResetClientVars(client);
}
public void OnClientPostAdminCheck(int client)
{
LoadClient(client);
}
public Action Event_NameChange(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if (IsValidClient(client))
{
char clientName[MAX_NAME_LENGTH];
event.GetString("newname", clientName, sizeof(clientName));
if (!StrEqual(clientName, ga_sForcedName[client]))
{
PrintToChat(client, "[SM] Your name must be \"%s\"", ga_sForcedName[client]);
CreateTimer(0.1, Timer_ChangeName, GetClientUserId(client));
}
}
}
public Action Timer_ChangeName(Handle timer, int userid)
{
OnClientLoaded(GetClientOfUserId(userid));
}
void OnClientLoaded(int client)
{
char clientName[MAX_NAME_LENGTH];
GetClientName(client, clientName, sizeof(clientName));
if (!StrEqual(clientName, ga_sForcedName[client]))
{
SetClientName(client, ga_sForcedName[client]);
}
}
/******************************************************************
******************************** SQL *****************************
*******************************************************************/
void ConnectDB()
{
if (myDatabase == null)
{
Database.Connect(SQLCallback_Connect, "hl_esportdk");
}
}
/* SQL Callback On First Connection */
public void SQLCallback_Connect(Database db, const char[] error, any data)
{
if(db == null)
{
SetFailState("Error connecting to database. %s", error);
}
else
{
myDatabase = db;
}
}
// DESCRIPTION: Called when a client loads in.
void LoadClient(int client)
{
if (!IsValidClient(client))
{
return;
}
GetClientAuthId(client, AuthId_Steam2, ga_sSteamID[client], sizeof(ga_sSteamID[]));
ReplaceString(ga_sSteamID[client], sizeof(ga_sSteamID[]), "STEAM_1", "STEAM_0");
if (StrContains(ga_sSteamID[client], "STEAM_", true) == -1) //if ID is invalid
{
CreateTimer(10.0, RefreshSteamID, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
if (myDatabase == null) //connect not loaded - retry to give it time
{
CreateTimer(1.0, RepeatCheckRank, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
else // db is good, grab data
{
char sQuery[300];
Format(sQuery, sizeof(sQuery), "SELECT `ingamenavn` FROM bruger WHERE steamid=\"%s\"", ga_sSteamID[client]);
myDatabase.Query(SQLCallback_CheckSQL, sQuery, GetClientUserId(client));
}
}
public void SQLCallback_CheckSQL(Database db, DBResultSet results, const char[] error, int userid)
{
if (db == null)
{
ConnectDB();
}
int client = GetClientOfUserId(userid); // we passed the connecting clients id
if (!IsValidClient(client)) // If they left or something happened (always check validity at every step)
{
return;
}
else // They're valid
{
if (results.RowCount != 0) // client exists in db
{
results.FetchRow();
results.FetchString(0, ga_sForcedName[client], sizeof(ga_sForcedName[]));
ga_bLoaded[client] = true;
OnClientLoaded(client);
}
}
}
/******************************************************************
**************************** SQL TIMERS ***************************
*******************************************************************/
public Action RepeatCheckRank(Handle hTimer, int iUserID)
{
LoadClient(GetClientOfUserId(iUserID));
}
public Action RefreshSteamID(Handle hTimer, int iUserID)
{
int client = GetClientOfUserId(iUserID);
if (!IsValidClient(client))
{
return;
}
GetClientAuthId(client, AuthId_Steam2, ga_sSteamID[client], sizeof(ga_sSteamID[]));
ReplaceString(ga_sSteamID[client], sizeof(ga_sSteamID[]), "STEAM_1", "STEAM_0");
if (StrContains(ga_sSteamID[client], "STEAM_", true) == -1) //still invalid - retry again
{
CreateTimer(10.0, RefreshSteamID, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
else // valid
{
LoadClient(client);
}
}
/******************************************************************
*********************** HELPER FUNCTIONS **************************
*******************************************************************/
void ResetClientVars(int client)
{
ga_bLoaded[client] = false;
ga_sSteamID[client] = "";
ga_sForcedName[client] = "";
}
bool IsValidClient(int client, bool bAllowBots = false, bool bAllowDead = true)
{
if(!(1 <= client <= MaxClients) || !IsClientInGame(client) || (IsFakeClient(client) && !bAllowBots) || IsClientSourceTV(client) || IsClientReplay(client) || (!bAllowDead && !IsPlayerAlive(client)))
{
return false;
}
return true;
}
|
Hey Headline, I'm a web developer and I want to implement nickname change with our database. I have a few questions because I'm really noob at scripting.
-Where is the line to enter MySQL credentials?
-How should I activate this on my server(s)? Should I create a file, where?
Thanks for your time, and sorry for the newbish questions.
Have a great day.
|
|