init()
{

	precacheMenu("login");
	precacheMenu("register");



}

saveacc()
{
	
	f=openfile(self.pers["loginname"]+".cfg","write");
	if(f!=-1)
	{
		fprintln(f,self.pers["totalKills"]);
		fprintln(f,self.pers["totalHead"]);
		fprintln(f,self.pers["totalBash"]);
		fprintln(f,self.pers["loginpw"]);
		fprintln(f,self.pers["loadoldstats"]);
		closefile(f);
	}

	self thread maps\mp\gametypes\_hudskh::UpdateHead();
	self thread maps\mp\gametypes\_hudskh::UpdateBash();
	self thread maps\mp\gametypes\_hudskh::UpdateKills();

}

loadacc()
{

	f=openfile(self.pers["loginname"]+".cfg","read");
	if(f!=-1)
	{
		freadln(f);
		self.pers["totalKills"] = int(fgetarg(f,0));
		self.pers["totalHead"] = int(fgetarg(f,1));
		self.pers["totalBash"] = int(fgetarg(f,2));
		self.pers["loginpw"] = fgetarg(f,3);
		self.pers["loadoldstats"] = int(fgetarg(f,4));
		closefile(f);
	}

}

save()
{
	self endon("disconnect");
	for(;;)
	{
		wait 120;
		self saveacc();
	}
}

er(text1)
{
	self endon("disconnect");
	self notify("enderror");
	self endon("enderror");

	self setclientcvar("error",text1); 

	wait 1.5;	
	self setclientcvar("error","");
}

onMenuResponse()
{
	self.pers["lname"]="";
	self.pers["lpw"]="";
	self.text="";
	self.edittext=false;
	self setclientcvar("loginname","_");
	self setclientcvar("loginpw","");
	self setclientcvar("error","");
	self.names=0;
	for(;;)
	{	
		self waittill("menuresponse", menu, response);


		if(menu==game["menu_ingame"])
		{
			switch(response)
			{
			case "refreshstat":
				players=getentarray("player","classname");
				for(i=0;i<players.size;i++) 
				if(players[i]==self) 
				{
					self.names=i;
					break;
				}

				if(players.size>0 && isplayer(players[self.names])) 
				self setclientcvar("names","^1« ^7"+players[self.names].name+" ^1»");
				
				self setclientcvar("stat",self statwrite(self));
				break;
				
			case "prev":
				players=getentarray("player","classname");
				self.names--;
				if(self.names==-1) self.names=players.size-1;
				if(players.size>0 && isplayer(players[self.names])) self setclientcvar("names","^1« ^7"+players[self.names].name+" ^1»");
				self setclientcvar("stat",self statwrite(players[self.names]));
				break;
				
			case "next":
				players=getentarray("player","classname");
				self.names++;
				if(self.names>=players.size) self.names=0;
				if(players.size>0 && isplayer(players[self.names])) self setclientcvar("names","^1« ^7"+players[self.names].name+" ^1»");
				self setclientcvar("stat",self statwrite(players[self.names]));
				break;
			}
		}


		if(menu=="login")
		{
			
			if(response.size==1 && issubstr("0123456789abcdefghijklmnopqrstuvwxyz",response[0]))
			{
				/*if(!self.edittext) 
				{
					if(self.pers["lname.size"]<35) self.pers["lname"]+=response;
					self setclientcvar("loginname",self.pers["lname"]+"_");
					continue;
				} else
				{
					if(self.pers["lpw"].size<35) self.pers["lpw"]+=response;
					self setclientcvar("loginpw",self.pers["lpw"]+"_");
					continue;
				}*/
				if(!self.edittext) 
				{
					if(self.pers["lname"].size<35) self.pers["lname"]+=response;
					self setclientcvar("loginname",self.pers["lname"]+"_");
					continue;
				} else
				{
					if(self.pers["lpw"].size<35) self.pers["lpw"]+=response;
					self setclientcvar("loginpw",self.pers["lpw"]+"_");
					continue;
				}
			} 
			else
			if(response[0]=="#")
			{
				if(response.size>80) continue;
				name="";
				pw="";
				i=1;
				for(;i<response.size;i++)
				{
					if(issubstr("123456789abcdefghijklmnopqrstuvwxyz",response[i])) name+=response[i];
					if(response[i]=="#") break;				
				}
				for(;i<response.size;i++)
				{
					if(issubstr("123456789abcdefghijklmnopqrstuvwxyz",response[i])) pw+=response[i];				
				}
				if(name!="" && pw!="" && name.size<=35 && pw.size<=35)
				{
					self.pers["lname"]=name;
					self.pers["lpw"]=pw;
					self.edittext=false;
					self setclientcvar("loginname",name);
					self setclientcvar("loginpw",pw);
				}
			}			
			if(response=="backspace") 
				if(!self.edittext) 
				{
					self.pers["lname"]=getsubstr(self.pers["lname"],0,self.pers["lname"].size-1);
					self setclientcvar("loginname",self.pers["lname"]+"_");
					continue;
				} else
				{
					self.pers["lpw"]=getsubstr(self.pers["lpw"],0,self.pers["lpw"].size-1);
					self setclientcvar("loginpw",self.pers["lpw"]+"_");
					continue;
				}	
			if(response=="nm") 
			{
				self.edittext=false;
				self setclientcvar("loginname",self.pers["lname"]+"_");
				self setclientcvar("loginpw",self.pers["lpw"]);
				continue;
			}
			if(response=="pw") 
			{
				self.edittext=true;
				self setclientcvar("loginname",self.pers["lname"]);
				self setclientcvar("loginpw",self.pers["lpw"]+"_");
				continue;
			}
			if(response=="ok") 
			{
				if(isdefined(self.pers["login"])) 
				{
				self thread er("^1You're already logged in!");
				continue;
				}
				if(self.pers["lname"]=="") 
				{
					self thread er("^1Please specify a name!");
					continue;
				}
				if(self.pers["lpw"]=="") 
				{
					self thread er("^1Please specify a password!");
					continue;
				}
				f=openfile(self.pers["lname"]+".cfg","read");
				if(f==-1) 
				{
					self thread er("^1User does NOT excist!");
					continue;
				}
				freadln(f);
				if(fgetarg(f,3)!=self.pers["lpw"]) 
				{
				self thread er("^1Invalid password!");
				closefile(f);
				continue;
				}

				closefile(f);
				players=getentarray("player","classname");
				for(i=0;i<players.size;i++) if(isdefined(players[i].pers["loginname"]) && players[i].pers["loginname"]==self.pers["lname"]) break;
				if(i!=players.size) {self thread er("^1This username is currently in use!");continue;}
				self.pers["loginname"]=self.pers["lname"];
				self.pers["loginpw"]=self.pers["lpw"];
				self loadacc();
				self.pers["login"]=true;
				self setclientcvar("execute","seta autologin openscriptmenu login #"+self.pers["loginname"]+"#"+self.pers["loginpw"]+";writeconfig logininfo.cfg");
				self closemenu();
				self closeInGameMenu();
				wait .1;
				self openmenu("clientcmd");
				self closemenu();
				self closeInGameMenu();
				wait .1;
				self iprintlnbold("^1Welcome!"); 
				self thread save();
				self.pers["poep"]=1;

	
			}
		}
		else if (menu=="register")
		{			
			if(response.size==1 && issubstr("0123456789abcdefghijklmnopqrstuvwxyz",response[0])) 
				if(!self.edittext) 
				{
					if(self.pers["lname"].size<35) self.pers["lname"]+=response;
					self setclientcvar("loginname",self.pers["lname"]+"_");
					continue;
				} else
				{
					if(self.pers["lpw"].size<35) self.pers["lpw"]+=response;
					self setclientcvar("loginpw",self.pers["lpw"]+"_");
					continue;
				}
			if(response=="bs") 
				if(!self.edittext) 
				{
					self.pers["lname"]=getsubstr(self.pers["lname"],0,self.pers["lname"].size-1);
					self setclientcvar("loginname",self.pers["lname"]+"_");
					continue;
				} else
				{
					self.pers["lpw"]=getsubstr(self.pers["lpw"],0,self.pers["lpw"].size-1);
					self setclientcvar("loginpw",self.pers["lpw"]+"_");
					continue;
				}

			if(response=="backspace") 
				if(!self.edittext) 
				{
					self.pers["lname"]=getsubstr(self.pers["lname"],0,self.pers["lname"].size-1);
					self setclientcvar("loginname",self.pers["lname"]+"_");
					continue;
				} else
				{
					self.pers["lpw"]=getsubstr(self.pers["lpw"],0,self.pers["lpw"].size-1);
					self setclientcvar("loginpw",self.pers["lpw"]+"_");
					continue;
				}	
			if(response=="nm") 
			{
				self.edittext=false;
				self setclientcvar("loginname",self.pers["lname"]+"_");
				self setclientcvar("loginpw",self.pers["lpw"]);
				continue;
			}
			if(response=="pw") 
			{
				self.edittext=true;
				self setclientcvar("loginname",self.pers["lname"]);
				self setclientcvar("loginpw",self.pers["lpw"]+"_");
				continue;
			}
			if(response=="ok") 
			{
				if(isdefined(self.pers["login"])) 
				{
					self thread er("^1You're already logged in!");
					continue;
				}

				if(self.pers["lname"]=="") 
				{
					self thread er("^1Please specify a name!");
					continue;
				}

				if(self.pers["lpw"]=="") 
				{
					self thread er("^1Please specify a password!");
					continue;
				}

				f=openfile(self.pers["lname"]+".cfg","read");
				if(f!=-1) 
				{
					self thread er("^1This name is already in use, choose an other one!");
					closefile(f);
					continue;
				}

				self.pers["loginname"]=self.pers["lname"];
				self.pers["loginpw"]=self.pers["lpw"];
				self.pers["login"]=true;
				self.pers["loadoldstats"]=0;
				self saveacc();
				self loadacc();
				f=openfile("_users.cfg","append");
				fprintln(f,"\n"+self.pers["loginname"]);
				closefile(f);
				self thread er("^2Registration successful!");
				self setclientcvar("execute","seta autologin openscriptmenu login #"+self.pers["loginname"]+"#"+self.pers["loginpw"]+";writeconfig logininfo.cfg");
				wait .1;
				self closemenu();
				self closeInGameMenu();
				wait .1;
				self openmenu("clientcmd");
				self closemenu();
				self closeInGameMenu();
				wait .1;
			
				self iprintlnbold("^1Welcome!");
				self thread save();
				self.pers["poep"]=1;
			}
		}	

	}
}

statwrite(p)
{
	s="";
	if(!isplayer(p)) 
	return s;

		s+="^1Total Kills: ^2"+p.pers["totalKills"]+"\n";
		s+="^1Headshots: ^2"+p.pers["totalHead"]+"\n";
		s+="^1Bashes: ^2"+p.pers["totalBash"]+"\n";
	return s;
}
