| 
		
			| get_part_name, chathooks and strings [message #366913] | Sun, 11 January 2009 07:25  |  
			| 
				
				|  |  reborn Messages: 3231
 Registered: September 2004
 Location: uk - london
 
	Karma: 0
 | General (3 Stars) |  |  |  
	| I wanted to make it eaiser to get the GameObject * of a player by typing his name out. Then I found it was slightly annoying when players had stupid names like "11||1!\!!|\1\!|!|!\11\11|||", so I experimented with "Get_Part_Name". However, the function never seems to return a gameobject no matter how unique I start typing there name... 
 Here is a little chat hook I made to test the function out:
 
 
 
 class testChatCommand : public ChatCommandClass {
	void Triggered(int ID,const TokenClass &Text,int ChatType) {
				GameObject *obj = Get_GameObj(ID);
					if (!Text[1].empty()) {
std::string player = Text[1].c_str();
GameObject *pobj = Get_GameObj_By_Player_Name(player.c_str());
if(!pobj){
Console_Input(StrFormat("ppage %d You typed %s, there is no exact match to this name.",ID,player.c_str()).c_str());
}
else{
Console_Input(StrFormat("ppage %d You typed %s, i've found this player and I confirm his name is %s.",ID,player.c_str(),Get_Player_Name(pobj)).c_str());
}
GameObject *pobj2;
pobj2 = Get_Part_Name(player.c_str());
if(!pobj2){
Console_Input(StrFormat("ppage %d You typed %s, the name is not unique enough to find.",ID,player.c_str()).c_str());
}
else{
Console_Input(StrFormat("ppage %d You typed %s, you probably mean %s.",ID,player.c_str(),Get_Player_Name(pobj2)).c_str());
}
					}
	}
	};
ChatCommandRegistrant<testChatCommand> testChatCommandReg("!find",CHATTYPE_ALL,1,GAMEMODE_AOW);
 I used this chathook in a server with twenty players, I typed out a few players names that was long, but just missed off the last letter. I was always paged that the name was not unique enough.
 
 Is there something wrong with the way I am using the function Get_Part_Name, or is the function itself not working how it should?
 
 Get_Part_Name function is here if you cab't be bothered to look it up
  
 
 
GameObject *Get_Part_Name(const char *name1)
{
	GenericSLNode *x = BaseGameObjList->HeadNode;
	int count = 0;
	GameObject *current = 0;
	while (x)
	{
		GameObject *o = As_SoldierGameObj((GameObject *)x->NodeData);
		if (o)
		{
			const char *name = Get_Player_Name(o);
			if (!stristr(name,name1))
			{
				current = o;
				count++;
			}
			delete[] name;
		}
		x = x->NodeNext;
	}
	if ((count == 1) && (current) && (Commands->Get_ID(current)))
	{
		return current;
	}
	else
	{
		return 0;
	}
}
 
 
 |  
	|  |  | 
	|  | 
	|  | 
	|  | 
	|  | 
	|  | 
	|  | 
	|  | 
	| 
		
			| Re: get_part_name, chathooks and strings [message #367617 is a reply to message #366915] | Sat, 17 January 2009 04:01   |  
			| 
				
				|  |  reborn Messages: 3231
 Registered: September 2004
 Location: uk - london
 
	Karma: 0
 | General (3 Stars) |  |  |  
	| | cAmpa wrote on Sun, 11 January 2009 10:04 |  | In "GameObject *Get_Part_Name(const char *name1)" is a small bug,
 replace
 
 
 if (!stristr(name,name1))
 with
 
 
 
 | 
 
 
 This bug also exists in the "Get_Part_Names" function too. It needs to be changed to:
 
 
 
int Get_Part_Names(const char *name1)
{
	GenericSLNode *x = BaseGameObjList->HeadNode;
	int count = 0;
	while (x)
	{
		GameObject *o = As_SoldierGameObj((GameObject *)x->NodeData);
		if (o /*&& Commands->Is_A_Star(o)*/)
		{
			const char *name = Get_Player_Name(o);
			if (stristr(name,name1))
			{
				count++;
			}
			delete[] name;
		}
		x = x->NodeNext;
	}
	return count;
}
 Just an FYI if anyone ever wondered why it doesn't work how they might expect it to.
 
 
 
 |  
	|  |  | 
	|  | 
	| 
		
			| Re: get_part_name, chathooks and strings [message #367678 is a reply to message #366918] | Sat, 17 January 2009 20:37  |  
			| 
				
				
					| Genesis2001 Messages: 1397
 Registered: August 2006
 
	Karma: 0
 | General (1 Star) |  |  |  
	| | RoShamBo wrote on Sun, 11 January 2009 08:30 |  | 
 
int FindPlayer(const char *Part) //-2: not unique, -1: not found
{
	int Player = -1;
	for(GenericSLNode *x = PlayerList->HeadNode; x != 0; x = x->NodeNext)
	{
		cPlayer *p = (cPlayer *)x->NodeData;
		if(p && p->IsActive)
		{
			const char *pName = WideCharToChar(p->PlayerName);
			if(stricmp(pName, Part) == 0)
			{
				delete []pName;
				Player = p->PlayerId;
				break;
			}
			if(stristr(pName, Part))
			{
				if(Player >= 0)
				{
					delete []pName;
					return -2;
				}
				else
				{
					Player = p->PlayerId;
				}
			}
			delete []pName;
		}
	}
	return Player;	
}
 | 
 
 
 Nice
   |  
	|  |  |