/*
	PostNuke Wasteland Joblibrary
	Author:	NeoPhoenix
	Date:	29.01.2012

	native job_OnPlayerChangeJob(playerid,newjob);
	native job_SavePlayerJob(playerid,jobid=-1);
	native job_OnPlayerJobInit(playerid,jobid=-1);
	native job_Cronjob();
*/

enum JOB_LIST (+=1)
{
	JOB_ARBEITSLOS = 0,
	JOB_JAEGER,
	JOB_MINENARBEITER,
	JOB_HOLZFAELLER,
	JOB_MECHANIKER,
	JOB_WAFFENSCHMIED,
}

enum job_info
{
	Jtime_left,
	Jtime_played,
	Jskill,
	bool:Jloaded,
}

new PlayerJob[MAX_PLAYERS][JOB_LIST][job_info],
	jobnames[JOB_LIST][] = {"Arbeitslos","Jäger","Minenarbeiter","Holzfäller","Mechaniker","Waffenschmied"};

stock job_OnPlayerChangeJob(playerid,JOB_LIST:newjob)
{
	if(newjob < JOB_LIST:0 || newjob > JOB_LIST) return 0;
	mysql_SetIntByInt("pnwl_user","job",int:newjob,"userid",PlayerInfo[playerid][PLAYER_ID]);
	job_SavePlayerJob(playerid,JOB_LIST:PlayerInfo[playerid][Job]);//Alten job speichern
	if(newjob == JOB_ARBEITSLOS) format(string,sizeof(string),"INFO: Du bist nun arbeitslos.");
	else format(string,sizeof(string),"INFO: Du hast einen neuen Beruf \"%s\".",jobnames[newjob]);
	SendClientMessage(playerid,GRUEN,string);
	PlayerInfo[playerid][Job] = int:newjob;
	job_OnPlayerJobInit(playerid,newjob);//Neuen job laden bzw anlegen
	return 1;
}

job_SavePlayerJob(playerid,JOB_LIST:jobid=JOB_LIST:-1)
{
	if(jobid < JOB_LIST:0)	jobid = JOB_LIST:PlayerInfo[playerid][Job];
	if(jobid == JOB_LIST:0)	return 0;
	format(query,sizeof(query),"SELECT * FROM pnwl_user_jobs WHERE userid='%d' AND jobid='%d'",PlayerInfo[playerid][PLAYER_ID],int:jobid);
	mysql_query(query);
	mysql_store_result();
	if(mysql_num_rows() >= 1)
	{//Existiert bereits
		mysql_free_result();
		format(query,sizeof(query),"UPDATE pnwl_user_jobs SET time_left='%d', time_played='%d', skill='%d' WHERE userid='%d' AND jobid='%d'",PlayerJob[playerid][jobid][Jtime_left],PlayerJob[playerid][jobid][Jtime_played],PlayerJob[playerid][jobid][Jskill],PlayerInfo[playerid][PLAYER_ID],int:jobid);
		mysql_query(query);
		mysql_free_result();
	}
	else
	{//Muss noch angelegt werden
		mysql_free_result();
		format(query,sizeof(query),"INSERT INTO pnwl_user_jobs (userid,jobid,time_left,time_played,skill) VALUES ('%d','%d','%d','%d','%d')",\
		PlayerInfo[playerid][PLAYER_ID],int:jobid,PlayerJob[playerid][jobid][Jtime_left],PlayerJob[playerid][jobid][Jtime_played],PlayerJob[playerid][jobid][Jskill]);
		mysql_query(query);
		mysql_free_result();
	}
	return 1;
}

job_OnPlayerJobInit(playerid,JOB_LIST:jobid=JOB_LIST:-1)
{
	if(jobid == JOB_LIST:-1)	jobid = JOB_LIST:PlayerInfo[playerid][Job];
	if(jobid <= JOB_ARBEITSLOS) return 0;//Arbeitslos oder falsche jobid
	if(PlayerJob[playerid][jobid][Jloaded]) return 3;//Wurde schon geladen
	format(query,sizeof(query),"SELECT * FROM pnwl_user_jobs WHERE userid='%d' AND jobid='%d'",PlayerInfo[playerid][PLAYER_ID],int:jobid);
	mysql_query(query);
	mysql_store_result();
	if(mysql_num_rows() >= 1)
	{
		mysql_retrieve_row();
		mysql_fetch_field_row(string,"time_left");		PlayerJob[playerid][jobid][Jtime_left] 		= strval(string);
		mysql_fetch_field_row(string,"time_played");	PlayerJob[playerid][jobid][Jtime_played] 	= strval(string);
		mysql_fetch_field_row(string,"skill");			PlayerJob[playerid][jobid][Jskill] 			= strval(string);		
		PlayerJob[playerid][jobid][Jloaded] = true;
		mysql_free_result();
		format(string,sizeof(string),"JOB: Du bist ein %s. Zeit gespielt: %d, Skill: %d",jobnames[jobid],PlayerJob[playerid][jobid][Jtime_played],PlayerJob[playerid][jobid][Jskill]);
		SendClientMessage(playerid,GRUEN,string);
		return 1;//Erfolgreich geladen
	}
	else
	{
		format(query,sizeof(query),"INSERT INTO pnwl_user_jobs (userid,jobid,time_left) VALUES ('%d','%d','%d')",PlayerInfo[playerid][PLAYER_ID],int:jobid,120);//120*5 Minuten = 600 Minuten = 10 Stunden
		mysql_query(query);
		PlayerJob[playerid][jobid][Jtime_left] 	= 120;//120*5 Minuten = 600 Minuten = 10 Stunden
		PlayerJob[playerid][jobid][Jloaded]		= true;
		mysql_free_result();
		return 2;//Muss noch angelegt werden
	}
}

job_Cronjob()
{//Wird alle 5 Minuten aufgerufen
	for(new i; i<=__SERVER[HIGHEST_PLAYER_ID]; i++)
	{
		new JOB_LIST:jobid = JOB_LIST:PlayerInfo[i][Job];
		if(jobid > JOB_ARBEITSLOS && jobid < JOB_LIST)
		{//Hat einen Job
			PlayerJob[i][jobid][Jtime_played] += 5;
			if(PlayerJob[i][jobid][Jtime_left] > 0)
			{
				if((PlayerJob[i][jobid][Jtime_left]-=5) <= 0)
				{
					PlayerJob[i][jobid][Jtime_left] = 0;
					format(string,sizeof(string),"INFO: Du hast 10 Spielstunden als %s gearbeitet und kannst nun den Job wechseln.",jobnames[JOB_LIST:PlayerInfo[i][Job]]);
					SendClientMessage(i,GRUEN,string);
				}
			}
		}
	}
	return 1;
}
	
	
	
	
	
	
	
	