#include extreme\_ex_utils;

checkInit(checkrank)
{
	self endon("disconnect");

	self.ex_name = undefined;
	if(level.ex_ranksystem && checkrank) self.pers["preset_rank"] = 0;

	// check if guid number is authorised for extra privileges
	if(level.ex_security && !self checkGuid()) return;

	// check what clan they are in
	self checkClan();

	// if the rank system is enabled, check if they have a preset rank
	if(level.ex_ranksystem && checkrank) self.pers["preset_rank"] = self checkPresetRank();
}

checkGuid()
{
	self endon("disconnect");

	playerGuid = self getGuid();

	if(!playerGuid) return false;

	count = 0;
		
	for(;;)
	{
		guid = [[level.ex_drm]]("ex_guid_", 0, 0, 999999, "int");

		if(!guid) break;
			else if(guid == playerGuid) return true;
				else count ++;
	}

	return false;
}

checkClan()
{
	self endon("disconnect");

	clan_num = false;
	
	for(i = 1; i < 5; i++)
	{
		if(checkClanID(i))
		{
			clan_num = i;
			break;
		}
	}

	if(!clan_num) return;

	// Changed: self.ex_name now stores the clan tag (unmodified)
	self.ex_name = level.ex_cltag[clan_num];
	self.ex_clid = clan_num;

	clantag = [];
	clantag[1] = &"CUSTOM_CLAN1_TAG";
	clantag[2] = &"CUSTOM_CLAN2_TAG";
	clantag[3] = &"CUSTOM_CLAN3_TAG";
	clantag[4] = &"CUSTOM_CLAN4_TAG";
	self.pers["clantag"] = clantag[self.ex_clid];

	return;
}

checkClanID(check)
{
	// decolorize name and tag
	namestr = extreme\_ex_utils::monotone(self.name);
	tagstr = extreme\_ex_utils::monotone(level.ex_cltag[check]);

	if(namestr.size <= tagstr.size) return false;
	sizediff = namestr.size - tagstr.size;

	// check clan tag in front or at end of player's name
	cnfront = "";
	cnback = "";
	for(i = 0; i < tagstr.size; i++)
	{
		cnfront += namestr[i];
		cnback  += namestr[sizediff + i];
	}

	if(cnfront == tagstr || cnback == tagstr) return true;

	return false;
}

checkPresetRank()
{
	self endon("disconnect");

	count = 0;
	clan_check = "";

	if(isDefined(self.ex_name))
	{
		// convert the players clan name
		playerclan = convertMUJ(self.ex_name);

		for(;;)
		{
			// get the preset clan name
			clan_check = [[level.ex_drm]]("ex_psr_clan_" + count, "", "", "", "string");

			// check if there is a preset clan name, if not end here!
			if(clan_check == "") break;

			// convert clan name
			clan_check = convertMUJ(clan_check);

			// if the names match, break here and set rank
			if(clan_check == playerclan) break;
				else count ++;
		}
	}

	if(clan_check != "")
		return [[level.ex_drm]]("ex_psr_rank_" + count, 0, 0, 12, "int");

	// convert the players name
	playername = convertMUJ(self.name);

	count = 0;

	for(;;)
	{
		// get the preset player name
		name_check = [[level.ex_drm]]("ex_psr_name_" + count, "", "", "", "string");

		// check if there is a preset player name, if not end here!
		if(name_check == "") break;

		// convert name_check
		name_check = convertMUJ(name_check);

		// if the names match, break here and set rank
		if(name_check == playername) break;
		else count ++;
	}

	if(name_check == "") return 0;
	else return [[level.ex_drm]]("ex_psr_rank_" + count, 0, 0, 12, "int");
}

set_ignore_player()
{
	self endon("disconnect");

	self.pers["dontkick"] = false;

	count = 0;
	clan_check = "";

	if(isDefined(self.ex_name))
	{
		// convert the clan name
		playerclan = convertMUJ(self.ex_name);

		for(;;)
		{
			// get the preset clan name
			clan_check = [[level.ex_drm]]("ex_ddp_clan_" + count, "", "", "", "string");

			// check if there is a preset clan name, if not end here!
			if(clan_check == "") break;

			// convert clan name
			clan_check = convertMUJ(clan_check);

			// if the names match, break here and set kick status
			if(clan_check == playerclan) break;
				else count++;
		}
	}

	if(clan_check != "")
	{
		self.pers["dontkick"] = true;
		return;
	}

	// convert the players name
	playername = convertMUJ(self.name);

	count = 0;
		
	for(;;)
	{
		// get the preset player name
		name_check = [[level.ex_drm]]("ex_ddp_name_" + count, "", "", "", "string");

		// check if there is a preset player name, if not end here!
		if(name_check == "") break;

		// convert name_check
		name_check = convertMUJ(name_check);

		// if the names match, break here and set kick status
		if(name_check == playername) break;
			else count++;
	}

	if(name_check != "")
		self.pers["dontkick"] = true;
}
