PDA

View Full Version : player command am i going in the right direction



Blutus
21st August 2023, 19:06
i have been trying to write some code.
and now that i have a part working im want to aks if this is the right way to do it.

the code:



checkIfExist(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getentitynumber() == Int(number))
return true;
}
return false;
}

thirdArgument(args)
{
text = "";
for(i=3; i<args.size; i++)
text += args[i] + " ";
return text;
}

getPlayerNameById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getentitynumber() == Int(number))
return players[i].name;
}
return false;
}

getPlayerById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getEntityNumber() == Int(number))
return players[i];
}
return false;
}

tellMessage(message)
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7[^3PM^7]: " + message + "\"");
}


tellMessageWorld(message)
{
sendgameservercommand(-1, "h \"^2Nanny^7:" + message + "\"");
}

isint(string)
{
return ((int(string) + "") == string + "");
}


fixChatArgs(args)
{
if (isDefined(args[1])) { // engine is adding identifier infront of the chat message
if (getAscii(args[1][0]) >= 20 && getAscii(args[1][0]) <= 22) {
//std\io::print("delete bad ascii code: " + std\utils::getAscii(args[1][0]) + "\n");
args[1] = getSubStr(args[1], 1);
newArgs = strTok(args[1], " ");
for (i=0; i<newArgs.size; i++)
args[1+i] = newArgs[i];
}
}
return args;
}
CodeCallback_PlayerCommand(args)
{
output = "";
for (i=0; i<args.size; i++)
output += args[i] + ", ";

//std\io::print("PLAYER COMMAND! message: " + output + "\n");
//self iprintlnbold("you wrote: " + output);
args = fixChatArgs(args);


if (args[0] == "say" && isDefined(args[1]) && args[1][0] == "!")
{


switch (getSubStr(args[1], 1))
{
case "pm":

if (isDefined(args[2]) && isint(args[2]) && isDefined(args[3]))
{
args[2] = int(args[2]);
if(!checkIfExist(args[2]))
{
self iprintlnbold("Couldn\'t find a player with this number.");
return;
}



pmmessage = "";
for (i=3; i<args.size; i++)

pmmessage += args[i] + " ";
targetnum = getPlayerById(args[2]);
targetname = getPlayerNameById(args[2]);
sendname = self.name;
sendid = self getentitynumber();

sendgameservercommand(self getentitynumber(), "h \"^2Nanny:^7" + " ^3PM to ^7" + targetname + "^7: " + pmmessage + "\"");
sendgameservercommand(targetnum getentitynumber(), "h \"^2Nanny:^7" + " ^3PM from ^7" + sendname + "^3[^7" + sendid + "^3]^7" + "^7: " + pmmessage + "\"");

return;
}
else if (!isDefined(args[2]))
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7:" + " ^6Use the clientid to send a private message " + "\"");

return;
}

case "fps":

if (isDefined(args[2]) && isint(args[2]))
{
firstmessage = "";
for (i=1; i<args.size; i++)
firstmessage += args[i] + " ";

self sayall(firstmessage);

args[2] = int(args[2]);
if(!checkIfExist(args[2]))
{
self iprintlnbold("Couldn\'t find a player with this number.");
return;
}



targetnum = getPlayerById(args[2]);
targetname = getPlayerNameById(args[2]);
fpstarget = targetnum getFps();

tellMessageWorld(targetname + " FPS: " + fpstarget);
return;
}

if (!isDefined(args[2]))
{
firstmessage = "";
for (i=1; i<args.size; i++)
firstmessage += args[i] + " ";

self sayall(firstmessage);

fpsself = self getFps();
sendname = self.name;
tellMessageWorld(sendname + " FPS: " + fpsself);
return;
}
else
{
tellMessage("no name found");
return;
}

}
}

self ClientCommand();
}



whole script _callbacksetup.gsc


// Callback Setup
// This script provides the hooks from code into script for the gametype callback functions.

//================================================== ===========================
// Code Callback functions

/*================
Called by code after the level's main script function has run.
================*/
CodeCallback_StartGameType()
{
// If the gametype has not beed started, run the startup
if(!isDefined(level.gametypestarted) || !level.gametypestarted)
{
//disableGlobalPlayerCollision(); // libcod: disable player collisions
thread consoleName();
thread sayw();
[[level.callbackStartGameType]]();
level.gametypestarted = true; // so we know that the gametype has been started up
}
}

/*================
Called when a player begins connecting to the server.
Called again for every map change or tournement restart.

Return undefined if the client should be allowed, otherwise return
a string with the reason for denial.

Otherwise, the client will be sent the current gamestate
and will eventually get to ClientBegin.

firstTime will be qtrue the very first time a client connects
to the server machine, but qfalse on map changes and tournement
restarts.
================*/
CodeCallback_PlayerConnect()
{
self endon("disconnect");
[[level.callbackPlayerConnect]]();
}

/*================
Called when a player drops from the server.
Will not be called between levels.
self is the player that is disconnecting.
================*/
CodeCallback_PlayerDisconnect()
{
self notify("disconnect");
[[level.callbackPlayerDisconnect]]();
}

/*================
Called when a player has taken damage.
self is the player that took damage.
================*/
CodeCallback_PlayerDamage(eInflictor, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir, sHitLoc, timeOffset)
{
self endon("disconnect");
[[level.callbackPlayerDamage]](eInflictor, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir, sHitLoc, timeOffset);
}

/*================
Called when a player has been killed.
self is the player that was killed.
================*/
CodeCallback_PlayerKilled(eInflictor, eAttacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitLoc, timeOffset, deathAnimDuration)
{
self endon("disconnect");
[[level.callbackPlayerKilled]](eInflictor, eAttacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitLoc, timeOffset, deathAnimDuration);
}

//================================================== ===========================

/*================
Setup any misc callbacks stuff like defines and default callbacks
================*/
SetupCallbacks()
{

SetDefaultCallbacks();
// Set defined for damage flags used in the playerDamage callback
level.iDFLAGS_RADIUS = 1;
level.iDFLAGS_NO_ARMOR = 2;
level.iDFLAGS_NO_KNOCKBACK = 4;
level.iDFLAGS_NO_TEAM_PROTECTION = 8;
level.iDFLAGS_NO_PROTECTION = 16;
level.iDFLAGS_PASSTHRU = 32;
}

/*================
Called from the gametype script to store off the default callback functions.
This allows the callbacks to be overridden by level script, but not lost.
================*/
SetDefaultCallbacks()
{
level.default_CallbackStartGameType = level.callbackStartGameType;
level.default_CallbackPlayerConnect = level.callbackPlayerConnect;
level.default_CallbackPlayerDisconnect = level.callbackPlayerDisconnect;
level.default_CallbackPlayerDamage = level.callbackPlayerDamage;
level.default_CallbackPlayerKilled = level.callbackPlayerKilled;
}

/*================
Called when a gametype is not supported.
================*/
AbortLevel()
{
println("Aborting level - gametype is not supported");
level.callbackStartGameType = ::callbackVoid;
level.callbackPlayerConnect = ::callbackVoid;
level.callbackPlayerDisconnect = ::callbackVoid;
level.callbackPlayerDamage = ::callbackVoid;
level.callbackPlayerKilled = ::callbackVoid;
setcvar("g_gametype", "dm");
exitLevel(false);
}

/*================
================*/
callbackVoid()
{
}

consoleName()
{
for (;;)
{
if (getCvar("consoleName") != "")
{
level.consoleName = getCvar("consoleName");
}
wait 0.05;
}
}

sayw()
{
setcvar("sayw", "");
for (;;)
{
if (getCvar("sayw") != "")
{
message = getCvar("sayw");
sendGameServerCommand(-1, "h \"" + level.consoleName + "^7: " + message + "\"");
setcvar("sayw", "");
}
wait 0.05;
}
}

checkIfExist(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getentitynumber() == Int(number))
return true;
}
return false;
}

thirdArgument(args)
{
text = "";
for(i=3; i<args.size; i++)
text += args[i] + " ";
return text;
}

getPlayerNameById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getentitynumber() == Int(number))
return players[i].name;
}
return false;
}

getPlayerById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getEntityNumber() == Int(number))
return players[i];
}
return false;
}

tellMessage(message)
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7[^3PM^7]: " + message + "\"");
}


tellMessageWorld(message)
{
sendgameservercommand(-1, "h \"^2Nanny^7:" + message + "\"");
}

isint(string)
{
return ((int(string) + "") == string + "");
}


fixChatArgs(args)
{
if (isDefined(args[1])) { // engine is adding identifier infront of the chat message
if (getAscii(args[1][0]) >= 20 && getAscii(args[1][0]) <= 22) {
//std\io::print("delete bad ascii code: " + std\utils::getAscii(args[1][0]) + "\n");
args[1] = getSubStr(args[1], 1);
newArgs = strTok(args[1], " ");
for (i=0; i<newArgs.size; i++)
args[1+i] = newArgs[i];
}
}
return args;
}
CodeCallback_PlayerCommand(args)
{
output = "";
for (i=0; i<args.size; i++)
output += args[i] + ", ";

//std\io::print("PLAYER COMMAND! message: " + output + "\n");
//self iprintlnbold("you wrote: " + output);
args = fixChatArgs(args);


if (args[0] == "say" && isDefined(args[1]) && args[1][0] == "!")
{


switch (getSubStr(args[1], 1))
{
case "pm":

if (isDefined(args[2]) && isint(args[2]) && isDefined(args[3]))
{
args[2] = int(args[2]);
if(!checkIfExist(args[2]))
{
self iprintlnbold("Couldn\'t find a player with this number.");
return;
}



pmmessage = "";
for (i=3; i<args.size; i++)

pmmessage += args[i] + " ";
targetnum = getPlayerById(args[2]);
targetname = getPlayerNameById(args[2]);
sendname = self.name;
sendid = self getentitynumber();

sendgameservercommand(self getentitynumber(), "h \"^2Nanny:^7" + " ^3PM to ^7" + targetname + "^7: " + pmmessage + "\"");
sendgameservercommand(targetnum getentitynumber(), "h \"^2Nanny:^7" + " ^3PM from ^7" + sendname + "^3[^7" + sendid + "^3]^7" + "^7: " + pmmessage + "\"");

return;
}
else if (!isDefined(args[2]))
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7:" + " ^6Use the clientid to send a private message " + "\"");

return;
}

case "fps":

if (isDefined(args[2]) && isint(args[2]))
{
firstmessage = "";
for (i=1; i<args.size; i++)
firstmessage += args[i] + " ";

self sayall(firstmessage);

args[2] = int(args[2]);
if(!checkIfExist(args[2]))
{
self iprintlnbold("Couldn\'t find a player with this number.");
return;
}



targetnum = getPlayerById(args[2]);
targetname = getPlayerNameById(args[2]);
fpstarget = targetnum getFps();

tellMessageWorld(targetname + " FPS: " + fpstarget);
return;
}

if (!isDefined(args[2]))
{
firstmessage = "";
for (i=1; i<args.size; i++)
firstmessage += args[i] + " ";

self sayall(firstmessage);

fpsself = self getFps();
sendname = self.name;
tellMessageWorld(sendname + " FPS: " + fpsself);
return;
}
else
{
tellMessage("no name found");
return;
}

}
}

self ClientCommand();
}





i didnt write most parts myselft, but i put different codes together.
what im asking is:

- is the way to send the reply good?
- is the return statement good for the switch case or should it be break?
- should this be or stay in this script or should i write it in a seperate script and only call that script from the callbacksetup?

thanks

raphael
22nd August 2023, 12:16
Do you use Nanny? if so, why?

libcod has a getFps function allowing you to get the com_maxfps value of players?

Blutus
22nd August 2023, 16:18
the script is working.

im using libcod.
when players type !fps command the getfps() gets the actual fps value of the player or self.
What i am asking is if this is the way to do it, and what flaws i put in.

i am using nanny but it is thirdparty software and i wanted to write in in cod gsc script to make it send pm message and handle more message at the same time.
and because im just starting it isnt a lot of work to change before i start writing the rest of the code.

oh i see now because of the name Nanny, i just used that name for the console from the nanny code and still use that now.

IzNoGoD
22nd August 2023, 17:46
Dont return false with getplayerbyid, return undefined instead and check for that.

Dont getplayername or exists by id, just use the getplayerbyid function with proper checks.

fixchatargs is included in libcod already: https://github.com/voron00/libcod/blob/8e0dee9bf14510c8565e3633b7c0efdf6f9b8a11/libcod.cpp#L348-L348

I'd check for say_team as well as for say

The way you use the switch() won't work in any other language that gsc most likely, but if it works like this, it works.

Indentation on your for() loop to add the remaining args in pm is terrible

no need to use sayall if you got a clientcommand() at the end of it, that will handle the sayall anyway.

You got a fall-through case in your pm ifs where supplying it with a non-int args[2] will cause you to enter the fps thing (you can try this with !pm asdf)

Lots of duplicate code uisng thirdArgument-like function, while thirdArgument function remains unused

Here's a slightly revised version:



getPlayerNameById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getentitynumber() == Int(number))
return players[i].name;
}
return false;
}

tellMessage(message)
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7[^3PM^7]: " + message + "\"");
}

tellMessageWorld(message)
{
sendgameservercommand(-1, "h \"^2Nanny^7:" + message + "\"");
}

isint(string)
{
return ((int(string) + "") == string + "");
}

CodeCallback_PlayerCommand(args)
{
if ((args[0] == "say" || args[0] == "say_team") && isDefined(args[1]) && args[1][0] == "!")
{
switch (getSubStr(args[1], 1))
{
case "pm":
{
if (isDefined(args[2]) && isint(args[2]) && isDefined(args[3]))
{
player = getPlayerById(int(args[2]));
if(!isDefined(player))
{
self iprintlnbold("Couldn\'t find a player with this number.");
return;
}
pmmessage = "";
for (i=3; i<args.size; i++)
{
pmmessage += args[i] + " ";
}

sendgameservercommand(self getentitynumber(), "h \"^2Nanny:^7" + " ^3PM to ^7" + player.name + "^7: " + pmmessage + "\"");
sendgameservercommand(targetnum getentitynumber(), "h \"^2Nanny:^7" + " ^3PM from ^7" + self.name + "^3[^7" + self getentitynumber() + "^3]^7" + "^7: " + pmmessage + "\"");
return;
}
else if (!isDefined(args[2]))
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7:" + " ^6Use the clientid to send a private message " + "\"");
return;
}
else
{
//there was a fall-through case here
return; //todo: maybe add an explanation here that you need to put in a number?
}
}
case "fps":
{
if (isDefined(args[2]) && isint(args[2]))
{
player = getPlayerById(int(args[2]));
if(!isDefined(player))
{
self iprintlnbold("Couldn\'t find a player with this number.");
return;
}
self ClientCommand();
tellMessageWorld(player.name + " FPS: " + player getFps());
return;
}
else if(!isDefined(args[2]))
{
self ClientCommand();
tellMessageWorld(self.name + " FPS: " + self getFps());
return;
}
else
{
tellMessage("no name found");
return;
}
}
}
}
self ClientCommand();
}

Blutus
25th August 2023, 18:14
thanks for the tips i will use that to understand the script better.
i noticed the fall trough case with pm but wasnt sure why.
i think i updated the script already before i got awnsers but will use the tips to change the script.

Blutus
19th September 2023, 17:09
Script update



getPlayerNameById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getentitynumber() == Int(number))
return players[i].name;
}
return undefined;
}

getPlayerById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getEntityNumber() == Int(number))
return players[i];
}
return undefined;
}

getPlayerByName(name)
{
name = checkname(name);
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(tolower(stripcolors(players[i].name)) == name)
return players[i];
}
return undefined;

}

checkname(name)
{
name = tolower(stripcolors(name)); //strip colors, remove capitals
return name;
}

// strip a players name from color ^characters with numbers, and spaces
stripcolors(string)
{
gotcolors = true;

while(gotcolors)
{
gotcolors = false;
for(i = 0; i < string.size - 1; i++)
{
if(string[i] == "^" && isint(string[i + 1]))
{
newstring = "";
if(i > 0)
newstring += getsubstr(string, 0, i);
if(i < string.size - 2)
newstring += getsubstr(string, i + 2);
string = newstring;
gotcolors = true;
break;
}
}
}
gotspaces = true;

while(gotspaces)
{
gotspaces = false;
for(i = 0; i < string.size - 1; i++)
{
if(string[i] == " ")
{
newstring = "";
if(i > 0)
newstring += getsubstr(string, 0, i);
if(i < string.size - 2)
newstring += getsubstr(string, i + 1);
string = newstring;
gotspaces = true;
break;
}
}
}

return string;
}

// used to send a pm message to self player
tellMessage(message)
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7[^3PM^7]: " + message + "\"");
}

// to show color and characters of the players name in chat
chat(message)
{
sendgameservercommand(-1, "h \"" + self.name + "^7: " + message + "\"");
}

// should be a gameservercommand that replies in team chat with full name
chatteam(message)
{
self sayTeam(message);
}

// used to send a chat message to everyone
tellMessageWorld(message)
{
sendgameservercommand(-1, "h \"^2Nanny^7:" + message + "\"");
}

// used to send a PM message from player to player
tellMessageTarget(player, message)
{
sendgameservercommand(player getentitynumber(), "h \"^2Nanny^7:" + message + "\"");
}

// not sure but makes from an INT a string?
mijnstring(string)
{
return ((int(string) + "") == string + "");
}

isint(char)
{
asc = getAscii(char);
return asc <= 57 && asc >= 48;
}


CodeCallback_PlayerCommand(args)
{

if ((args[0] == "say" || args[0] == "say_team") && isDefined(args[1]) && args[1][0] == "!")
{
printchat = "";
for (i=1; i<args.size; i++)
{
printchat += args[i] + " ";
}

LogPrint(getLocalTime() + " " + self getIp() + " " + self getGuid() + " " + self.name + ": " + printchat + "\n");

switch (getSubStr(args[1], 1))
{

case "pm":
{
if (isDefined(args[2]) && mijnstring(args[2]))
{
player = getPlayerById(int(args[2]));
if(!isDefined(player))
{
tellMessage("^4Couldn\'t find a player with this number.");
return;
}
if(isDefined(player) && !isDefined(args[3]))
{
tellMessage("^4No message?");
return;
}

pmmessage = "";
for (i=3; i<args.size; i++)
{
pmmessage += args[i] + " ";
}

tellMessage("^3PM to ^7" + player.name + "^3[^7" + player getEntityNumber() + "^3]^7: " + pmmessage); //shows the player who you send it to and the message
tellMessageTarget(player, " ^3PM from ^7" + self.name + "^3[^7" + self getEntityNumber() + "^3]^7: " + pmmessage); //shows the name and clientID from the player who send the message to the receiver
return;
}

if (isDefined(args[2]) && !mijnstring(args[2]))
{
player = getPlayerByName(args[2]);
if(!isDefined(player))
{
tellMessage("^4Couldn\'t find a player with this name.");
return;
}
if(isDefined(player) && !isDefined(args[3]))
{
tellMessage("^4No message?");
return;
}

pmmessage = "";
for (i=3; i<args.size; i++)
{
pmmessage += args[i] + " ";
}

tellMessage("^3PM to ^7" + player.name + "^3[^7" + player getEntityNumber() + "^3]^7: " + pmmessage); //shows the player who you send it to and the message
tellMessageTarget(player, " ^3PM from ^7" + self.name + "^3[^7" + self getEntityNumber() + "^3]^7: " + pmmessage); //shows the name and clientID from the player who send the message to the receiver
return;
}

else if (!isDefined(args[2]))
{
tellMessage("^4Use clientId for private message");
return;
}
else
{
tellMessage("^4Use clientId, im working on using player name to send PM!");
return;
}
} // end pm


case "fps":
{
if (isDefined(args[2]) && mijnstring(args[2]))
{
player = getPlayerById(int(args[2]));
if(!isDefined(player))
{
tellMessage("^4Couldn\'t find a player with this number.");
return;
}
chat = "";
for (i=1; i<args.size; i++)
{
chat += args[i] + " ";
}

chat(chat);
tellMessageWorld(player.name + " FPS: " + player getFps());
return;
}

if (isDefined(args[2]) && !mijnstring(args[2]))
{
player = getPlayerByName(args[2]);
if(!isDefined(player))
{
tellMessage("^4Couldn\'t find a player with this name.");
return;
}
chat = "";
for (i=1; i<args.size; i++)
{
chat += args[i] + " ";
}

chat(chat);
tellMessageWorld(player.name + " FPS: " + player getFps());
return;
}
else if(!isDefined(args[2]))
{
chat = "";
for (i=1; i<args.size; i++)
{
chat += args[i] + " ";
}

chat(chat);
tellMessageWorld(self.name + " FPS: " + self getFps());
return;
}
else
{
tellMessage("no name found");
return;
}
} // end fps

case "id":
{
// Sends a list of al players + ID as pm to the player
if (isDefined(args[2]) && IsString(args[2]) && args[2] == "all")
{
tellMessage("id all comand" + args[2]);
players = getentarray("player", "classname");

for(i = 0; i < players.size; i++)
{
tellMessage(players[i].name + "^7 id: " + players[i] getEntityNumber());
wait(0.05);

}
tellMessage("That are all players");
return;
}

// shows the id of the searched player name
if (isDefined(args[2]) && !mijnstring(args[2]))
{
player = getPlayerByName(args[2]);
if(!isDefined(player))
{
tellMessage("^4Couldn\'t find a player with this name.");
return;
}

stripname = checkname(player.name);
tellMessage(player.name + "^7 id: " + player getEntityNumber() + "^7 stripped name: " + stripname);
return;
}

// shows the name of the searched id
if (isDefined(args[2]) && mijnstring(args[2]))
{
player = getPlayerById(int(args[2]));
if(!isDefined(player))
{
tellMessage("^4Couldn\'t find a player with this number.");
return;
}

stripname = checkname(player.name);
tellMessage(player.name + "^7 id: " + player getEntityNumber() + "^7 stripped name: " + stripname);
return;
}

// shows your name with id as chat message
if (!isDefined(args[2]))
{
tellMessage("Name: " + self.name + "^7 id: " + self getEntityNumber());
return;
}
else
{
tellMessage("command unknown");
return;
}


} // end id

} //switch
} // if say and sayteam !

// to make the names in chat show with colors and characters
if ((args[0] == "say") && isDefined(args[1]))
{
chat = "";
for (i=1; i<args.size; i++)
{
chat += args[i] + " ";
}
chat(chat);
LogPrint(chat + "\n");
return;
}

// to make the names in team chat show with colors and characters
// SAY_TEAM DOESTN WORK AS LIKE THAT
// NOT SURE IF SENDGAMESERVERCOMMAND CAN TALK IN TEAM CHAT
if ((args[0] == "say_team") && isDefined(args[1]))
{
chat = "";
for (i=1; i<args.size; i++)
{
chat += args[i] + " ";
}
chatteam(chat);
LogPrint(getLocalTime() + " " + self getIp() + " " + self getGuid() + " " + self.name + ": " + chat + "\n");
return;
}

self ClientCommand();
return;
} //CodeCallback_PlayerCommand



And is this a good way to check players for guid or ip to give rights to commands?



case "check":
{
playerip = getCvar("sv_ip_blutus");
playersubip = getSubstr
playerguidstring = getCvar("sv_guid_blutus");
//tellMessage("sv_ip_blutus = " + playerip);
checkip = self getIp();
//tellMessage("checkip = " + checkip);
checkguid = self getguid();
playerguid = int(playerguidstring);


players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getIp() == number)
return players[i];
}



chat = "";
for (i=1; i<args.size; i++)
{
chat += args[i] + " ";
}


if (!isDefined(playerip) && !isDefined(playerguid))
{
tellMessageworld("ip undefined and guid undefined");
LogPrint(getLocalTime() + " " + self getIp() + " " + self getGuid() + " " + self.name + ": " + chat + "\n");
return;
}
if (isDefined(playerip) && !isDefined(checkip))
{
tellMessageworld("check ip undefined player ip: " + playerip);
LogPrint(getLocalTime() + " " + self getIp() + " " + self getGuid() + " " + self.name + ": " + chat + "\n");
return;
}
if (isDefined(checkip) && checkip == playerip)
{
tellMessageworld("there is a match: " + playerip + " == " + checkip);
printf(self.name + "^7: " + "there is a match: " + playerip + " == " + checkip + "\n", "console" );
LogPrint(getLocalTime() + " " + self getIp() + " " + self getGuid() + " " + self.name + ": " + chat + "\n");

wait 5.05;
if (mapexists("mp_toujane"))
{
wait 5.05;
map("mp_toujane");
return;
}
tellMessageworld("map doesnt exist");
LogPrint(getLocalTime() + " " + self getIp() + " " + self getGuid() + " " + self.name + ": " + chat + "\n");
return;
}

Mitch
20th September 2023, 16:46
You could cache the stripped player name with this callback and on connect:



CodeCallback_UserInfoChanged(clientNum)
{
oldname = self.name;
name = self get_userinfo("name");

self clientuserinfochanged();
}


This is how I remove the color tags and spaces:


stripColorsAndSpaces(t)
{
if(!IsSubStr(t, "^") && !IsSubStr(t, " "))
return t; // colorless spaceless text

n = "";
for(i=0;i<t.size;i++)
{
if(t.size > i+3 && t[i] == "^" && t[i+1] == "^" && isInt(t[i+2]) && isInt(t[i+3]))
i += 3; // skip ^^xx
else if(t.size > i+1 && t[i] == "^" && isInt(t[i+1]))
i += 1; // skip ^x
else if(t[i] != " ")
n += t[i]; // append text
}

return n;
}

isInt(char)
{
asc = getAscii(char);
return asc >= 48 && asc <= 57;
}