PDA

View Full Version : renaming



IzNoGoD
13th January 2015, 20:48
Hey all
Found a way to rename players without touching the "name" cvar (thus no setclientcvar needed)

Also: blocking renaming of certain players
Also: blocking renaming of certain players + renaming those players from script anyway without touching the name cvar.

Coming soon to a libcod installation near you

voron00
13th January 2015, 22:32
That's great, thanks. I wish it would be also possible to rename the server itself, i mean the "console" thing in chat.

Mitch
13th January 2015, 22:38
That's great, thanks. I wish it would be also possible to rename the server itself, i mean the "console" thing in chat.

You can already sent custom messages with sendGameServerCommand. But i think renaming "console:" to "...." is possible.

IzNoGoD
13th January 2015, 22:49
You can already sent custom messages with sendGameServerCommand. But i think renaming "console:" to "...." is possible.

But as it is already possible with sendgameservercommand, there is no real need to find this. Just tunnel your b3 into a cvar instead of the rcon say thingy and sendgameservercommand from there.

IzNoGoD
15th January 2015, 22:14
Ok, the functions have some (initially) unintended side-effects:

The "name" cvar can be read as an input after which the script can decide to 1) rename the player to that name 2) not rename the player at all 3) rename the player to a different name.
Basically these new functions allow for a "rename" event.

https://github.com/kungfooman/libcod/commit/e74966cad0633f5ec158c26a6a913e11caa7bf30

voron00
16th January 2015, 04:20
Hmm it seems now on SD names are changing instantly, without having to wait till the next round, even without calling these functions, but i like it, actually.

IzNoGoD
16th January 2015, 07:49
playerinit()
{
self.izno = []; //initialize player-info array
self checkinitialname(); //fix initial "Unknown Soldier" names
self.izno["userinfoname"] = self get_userinfo("name"); //keep track of the name as per the user's userinfo
self thread monitor_namechange();
self allow_rename(false);
}

checkinitialname()
{
name = self get_userinfo("name");
self.izno["realname"] = name;
result = checkname(name); //checkname returns true if the name is not allowed or a duplicate
if(result)
{
//as i disallow duplicate names, this while() loop tries to find an open JumpersHeavenFan#NUMBER for the player to use
while(result)
{
self.izno["realname"] = "JumpersHeavenFan#" + randomintrange(100, 1000);
result = self checkname(self.izno["realname"]);
}
//now, to keep the userinfo the same to not excessively trigger a clientuserinfochanged event, store the user's name before renaming him
userinfoname = self get_userinfo("name");

//this will allow clientuserinfochanged to rename the player
self allow_rename(true);

//change the name in the userinfo, then call the clientuserinfochanged function to do the actual renaming
self set_userinfo("name", self.izno["realname"]);
self clientuserinfochanged();

//disallow any future user-started rename events
self allow_rename(false);

//restore the original userinfo
self set_userinfo("name", userinfoname);
}

}

monitor_namechange()
{
self endon("disconnect");
while(true)
{
name = self get_userinfo("name");

//if the name in userinfo is different than the one stored serverside, the user changed his /name
if(name != self.izno["userinfoname"])
{
//namechange event
//check if user is allowed to change his name and what his real name should be
self.izno["realname"] = self namechanged(name);

//next few lines to the same as the renaming in checkinitialname()
self set_userinfo("name", self.izno["realname"]);
self allow_rename(true);
self clientuserinfochanged();
self allow_rename(false);
self.izno["userinfoname"] = name;
self set_userinfo("name", name);
}
wait 0.05;
}
}

namechanged(newname)
{
in_use = checkname(newname); //if the name is currently in use, try keeping your old name. If that's in use too, generate a new random name
if(in_use)
{
old_in_use = checkname(self.izno["realname"]);
self iprintlnbold("No duplicate names allowed");
if(!old_in_use)
return self.izno["realname"]; //user renaming DENIED
else
{
//find a new name for this user as his old name is in use
while(old_in_use)
{
newname = "JumpersHeavenFan#" + randomintrange(100, 1000);
result = self checkname(newname);
}
return newname;
}
}
else
return newname; //renaming allowed
}

checkname(name)
{
name = tolower(stripcolors(name)); //strip colors, remove capitals
switch(name)
{
//you can add more names to this banned-names list.
case "Unknown Soldier":
case "UnnamedPlayer":
return true;
}
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] == self)
continue;
if(tolower(stripcolors(players[i].name)) == name) //another player has the same name after stripping colors
return true;
}
return false;
}

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;
}
}
}
return string;
}

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

This code will work very well with the change. It prevents duplicate names and the rename notification of players when they rename to a duplicate name and back to their old name.
Can easily be extended to prevent players from renaming on an individual basis.

voron00
16th January 2015, 10:40
playerinit()
{
self.izno = []; //initialize player-info array
self checkinitialname(); //fix initial "Unknown Soldier" names
self.izno["userinfoname"] = self get_userinfo("name"); //keep track of the name as per the user's userinfo
self thread monitor_namechange();
self allow_rename(false);
}

checkinitialname()
{
name = self get_userinfo("name");
self.izno["realname"] = name;
result = checkname(name); //checkname returns true if the name is not allowed or a duplicate
if(result)
{
//as i disallow duplicate names, this while() loop tries to find an open JumpersHeavenFan#NUMBER for the player to use
while(result)
{
self.izno["realname"] = "JumpersHeavenFan#" + randomintrange(100, 1000);
result = self checkname(self.izno["realname"]);
}
//now, to keep the userinfo the same to not excessively trigger a clientuserinfochanged event, store the user's name before renaming him
userinfoname = self get_userinfo("name");

//this will allow clientuserinfochanged to rename the player
self allow_rename(true);

//change the name in the userinfo, then call the clientuserinfochanged function to do the actual renaming
self set_userinfo("name", self.izno["realname"]);
self clientuserinfochanged();

//disallow any future user-started rename events
self allow_rename(false);

//restore the original userinfo
self set_userinfo("name", userinfoname);
}

}

monitor_namechange()
{
self endon("disconnect");
while(true)
{
name = self get_userinfo("name");

//if the name in userinfo is different than the one stored serverside, the user changed his /name
if(name != self.izno["userinfoname"])
{
//namechange event
//check if user is allowed to change his name and what his real name should be
self.izno["realname"] = self namechanged(name);

//next few lines to the same as the renaming in checkinitialname()
self set_userinfo("name", self.izno["realname"]);
self allow_rename(true);
self clientuserinfochanged();
self allow_rename(false);
self.izno["userinfoname"] = name;
self set_userinfo("name", name);
}
wait 0.05;
}
}

namechanged(newname)
{
in_use = checkname(newname); //if the name is currently in use, try keeping your old name. If that's in use too, generate a new random name
if(in_use)
{
old_in_use = checkname(self.izno["realname"]);
self iprintlnbold("No duplicate names allowed
if(!old_in_use)
return self.izno["realname"]; //user renaming DENIED
else
{
//find a new name for this user as his old name is in use
while(old_in_use)
{
newname = "JumpersHeavenFan#" + randomintrange(100, 1000);
result = self checkname(newname);
}
return newname;
}
}
else
return newname; //renaming allowed
}

checkname(name)
{
name = tolower(stripcolors(name)); //strip colors, remove capitals
switch(name)
{
//you can add more names to this banned-names list.
case "Unknown Soldier":
case "UnnamedPlayer":
return true;
}
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] == self)
continue;
if(tolower(stripcolors(players[i].name)) == name) //another player has the same name after stripping colors
return true;
}
return false;
}

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;
}
}
}
return string;
}

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

This code will work very well with the change. It prevents duplicate names and the rename notification of players when they rename to a duplicate name and back to their old name.
Can easily be extended to prevent players from renaming on an individual basis.

Thanks for this, but there is an error in self iprintlnbold("No duplicate names allowed, also i tried this code but im getting segmentation fault on server after 30 seconds, wtf.

Ni3ls
16th January 2015, 10:41
Hmm it seems now on SD names are changing instantly, without having to wait till the next round, even without calling these functions, but i like it, actually.
That was already possible

setClientNameMode("auto_change");

Mitch
22nd January 2015, 18:11
Thanks for this, but there is an error in self iprintlnbold("No duplicate names allowed, also i tried this code but im getting segmentation fault on server after 30 seconds, wtf.

The segmentation fault is probably due to the address in gsc_player_clientuserinfochanged. (which is for 1.3 or 1.2)
I fixed the iprintlnbold error in the original code post. Now it is easier to look at the code and see what it does.

Edit: i modified the rename script to an event function like player messages. (CodeCallback_PlayerCommand)

https://github.com/M-itch/libcod/commit/801bb20a5ffe77ad5c53446236d949182ab52720 (+ 2 commits to fix some issues)



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

if(oldname != newname)
{
self iprintlnbold("Your old name is " + oldname);
self iprintlnbold("Your new name is " + newname);
}

self clientuserinfochanged();
}


I updated my libcod builds for cod2 1.0 - 1.3 on http://znation.nl/libcod/ubuntu/.

IzNoGoD
29th January 2015, 14:54
Removed a few functions from libcod (cant push to git due to no git account).

Now you can catch the clientuserinfochanged() as per mitch his update, then maybe renamebot(newname) the player, then issue a clientuserinfoupdate() command.
Works quite nicely