//@unloadnpc Event Assistant
//@loadnpc npc/valhalla/gmreward.txt

-	script	Event Assistant	-1,{
	function func_sql_reward;
	function func_item_name;
	function func_list_char;
	function func_display_card;
	function func_list_reward;
	function func_view_reward;
	end;

	OnReward:
	.@gm_level = getgmlevel();
	.@not_gm = ( .@gm_level < .gm_level || !.gm_level || !.@gm_level );

	cutin "kafra_09",2;
	mes "^FF0000[ Event Assistant ]^000000";
	mes "Hi "+strcharinfo(0)+",";
	mes "You can check information of our ongoing events and claim rewards here.";
	mes "What do you like to check?";
	next;
	switch ( select(
		( .@not_gm )? "^FF0000Check Ongoing Event^000000":"",
		( .@not_gm )? "Check Pending Reward":"Check Pending Reward",
		( .@not_gm )? "Check Reward History":"Check Reward History",
		( .@not_gm )? "":"^FF0000[GM]^000000 Send Reward",
		( .@not_gm )? "":"^FF0000[GM]^000000 Delete Reward",
		( .@not_gm )? "":"^FF0000[GM]^000000 Check History"
		":Close"
	)) {
		case 1:
			mes "^FF0000[ Event Assistant ]^000000";
			//mes "Here is the ONGOING event";
			//mes "Event: ^FF0000ScreenShot Post^000000";
			//mes  "Reward: 1x "+Itemlink(20101);
			//mes "Just screenshot and POST it to our FORUM.";
			//mes "<URL>How To Join<INFO>https://community.crazyporing.com/index.php?/topic/13-event-post-your-screenshots/&tab=comments#comment-17</INFO></URL>";
			break;
		case 2:
			mes "^FF0000[ Event Assistant ]^000000";
			mes "I will show you a list of rewards that are still not yet claimed.";
			func_list_reward( "`claim_name` = '"+escape_sql( strcharinfo(0) )+"' AND `status` = 0" );
			break;
		case 3:
			mes "^FF0000[ Event Assistant ]^000000";
			mes "I will show you a list of rewards that are claimed previously.";
			func_list_reward( "`claim_name` = '"+escape_sql( strcharinfo(0) )+"' AND `status` = 1" );
			break;
		case 4:
			mes "^FF0000[ Event Assistant ]^000000";
			doevent "Event Assistant::OnSendReward";
			end;
		case 5:
			mes "^FF0000[ Event Assistant ]^000000";
			mes "I will show you a list of rewards that are sent out but not yet claimed.";
			func_list_reward( "`status` = 0" );
			break;
		case 6:
			mes "^FF0000[ Event Assistant ]^000000";
			mes "I will show you a list of recent rewards based on the status you selected.";
			.@i = select( "Pending Reward","Claimed Reward","Deleted Reward" ) - 1;
			func_list_reward( "`status` = "+.@i );
			break;
		default: 
			break;
	}
	close2;
	cutin "",255;
	end;

	OnInit:
		bindatcmd "reward","Event Assistant::OnReward",0,99;
		.gm_level = 1;
		end;
		
	OnSendReward:
		cutin "kafra_09",2;
		set .@Menu$,"";	
		set .@reward,query_sql(" SELECT `nameid`,`qty`, `refine`, `card1`, `card2`, `card3`, `card4` FROM item_reward",.@nameid, .@qty, .@refine, .@card1, .@card2, .@card3, .@card4);
		for( set .@i,0; .@i < .@reward; set .@i,.@i + 1 )
			set .@Menu$,.@Menu$ + "^FF0000[ "+( .@i+1 )+". ]^0000FF "+.@qty[.@i]+"x "+getitemname(.@nameid[.@i])+":";
			set .@Select,select( .@Menu$ ) - 1;
			set .@r_nameid,.@nameid[.@Select];
			set .@r_qty,.@qty[.@Select];
			set .@r_refine,.@refine[.@Select];
			set .@r_card1,.@card1[.@Select];
			set .@r_card2,.@card2[.@Select];
			set .@r_card3,.@card3[.@Select];
			set .@r_card4,.@card4[.@Select];	

		mes "^FF0000[ GM Reward - Checklist ]^000000";
		mes "Enter Player name:";
		mes "^777777------------------------------^000000";
		mes "^6495ED# ^000000"+func_item_name( .@r_nameid,.@r_qty,.@r_refine );
		.@item_type = getiteminfo( .@r_nameid,2 );
		if ( .@item_type == IT_ARMOR || .@item_type == IT_WEAPON )
				func_display_card( .@r_card1,.@r_card2,.@r_card3,.@r_card4);

			.@player_name$ = func_list_char();
			if ( .@player_name$ != "" ) {
				next;
				mes "^FF0000[ GM Reward - Confirmation ]^000000";
				mes "Player: ^FF0000"+.@player_name$+"^000000";
				mes "^777777------------------------------^000000";
				
					mes "^6495ED# ^000000"+func_item_name( .@r_nameid,.@r_qty,.@r_refine );
					.@item_type = getiteminfo( .@r_nameid,2 );
					if ( .@item_type == IT_ARMOR || .@item_type == IT_WEAPON )
						func_display_card( .@r_card1,.@r_card2,.@r_card3,.@r_card4);

				if ( select( "Confirm","Cancel" ) == 1 ) {
					next;
					mes "^FF0000[ GM Reward - Confirmation ]^000000";
					mes "Enter a Title:";
					do {
						.@length = input( .@note$,4,70 );
						.@check = ( compare( .@note$,":" ) || .@length );
						if ( .@check ) {
							mes "^777777Invalid title^000000";
						}
					}while ( .@check );
					
					.@gm_aid = getcharid(3);
					.@gm_name$ = strcharinfo(0);
					
					query_sql( "SELECT `account_id`,`char_id`,`online` FROM `char` WHERE `name` = '"+escape_sql( .@player_name$ )+"' LIMIT 1",.@player_aid,.@player_cid,.@online );
					

						.@formatted_name$ = func_item_name( .@r_nameid,.@r_qty,.@r_refine );
						if ( func_sql_reward( .@gm_aid,escape_sql( .@gm_name$ ),.@r_nameid,.@r_qty,.@r_refine,escape_sql( .@formatted_name$ ),.@r_card1,.@r_card2,.@r_card3,.@r_card4,.@player_aid,.@player_name$,.@note$ ) )
							dispbottom "[Reward Sent] "+.@player_name$+" - "+.@formatted_name$;
						else 
							dispbottom "[Reward Sent Failure] "+.@player_name$+" - "+.@formatted_name$;
			
					
					if ( .@player_aid > 0 && isloggedin( .@player_aid,.@player_cid ) ) {
						message .@player_name$,"You received "+.@formatted_name$+" reward(s) from "+.@gm_name$+". Go to Event Assistant NPC and claim your reward.";
						message .@gm_name$,"You sent "+.@formatted_name$+" as reward(s) to "+.@player_name$;
							}
					mes "Rewards sent.";
					rodex_sendmail(.@player_cid, "[ "+.@gm_name$+" ]", "[ Reward Notice ]", "You have a pending reward [ "+.@formatted_name$+" ] to be claim.  Type @reward to claim.",0);
				}
			}

		close2;
		cutin "",255;
		end;
		
	function	func_sql_reward	{

		if ( getargcount() >= 12 ) {
			.@gm_aid = getarg(0);
			.@gm_name$ = getarg(1);
			.@sold_nameid = getarg(2);
			.@sold_quantity = getarg(3);
			.@sold_refine = getarg(4);
			.@formatted_name$ = getarg(5);
			.@sold_card1 = getarg(6);
			.@sold_card2 = getarg(7);
			.@sold_card3 = getarg(8);
			.@sold_card4 = getarg(9);
			.@player_aid = getarg(10);
			.@player_name$ = getarg(11);
			.@note$ = getarg(12);
			
			query_sql( "INSERT INTO `gm_reward` ( `gm_aid`,`gm_name`,`nameid`,`amount`,`refine`,`format_name`,`card1`,`card2`,`card3`,`card4`,`reward_time`,`claim_aid`,`claim_name`,`note` ) VALUES ( "+.@gm_aid+",'"+escape_sql( .@gm_name$ )+"',"+.@sold_nameid+","+.@sold_quantity+","+.@sold_refine+",'"+escape_sql( .@formatted_name$ )+"',"+.@sold_card1+","+.@sold_card2+","+.@sold_card3+","+.@sold_card4+",NOW(),"+.@player_aid+",'"+escape_sql( .@player_name$  )+"','"+.@note$+"' )" );	
			return 1;
		}
		
		return 0;
	}

	function	func_item_name	{
		.@item_id = getarg(0);
		.@amount = getarg(1);
		.@refine = getarg(2);
		
		.@string$ = getitemname( .@item_id );
		.@type = getiteminfo( .@item_id,2 );
			
		if ( .@string$ == "null" )
			return "none";
			
		if ( .@refine ) 
			.@string$ = "+"+.@refine+" "+.@string$;
		if ( .@type == IT_WEAPON || .@type == IT_ARMOR )
			.@string$ = .@string$ + " ["+getitemslots( .@item_id )+"]";
		if ( .@amount > 1 )
			.@string$ = .@amount+" x "+.@string$;
			
		return .@string$;
	}
	
	function	func_list_char	{
		__OnInput:
			while( input( .@player_name$,3,24 ) );
			query_sql( "SELECT `name`,`class` FROM `char` WHERE `name` LIKE '%"+escape_sql( .@player_name$ )+"%' ORDER BY `name` LIMIT 50",.@temp_name$,.@class );
			.@temp_name_size = getarraysize( .@temp_name$ );
			
			if ( .@temp_name_size <= 0 ) {
				dispbottom "[ GM Reward - Checklist ] '"+.@player_name$+"' doesnt exist.";
				// next;
				if( select( "Search another Name","Cancel" ) == 1 ) goto __OnInput;
				return "";
			}
			else if ( .@temp_name_size > 1 ) {
				dispbottom "[ GM Reward - Checklist ] Pick a player.";
				for ( .@i = 0; .@i < .@temp_name_size; .@i++ )
					.@menu$ = .@menu$ + "^FF0000["+jobname( .@class[.@i] )+"] ^000000" + .@temp_name$[.@i] +":";
				.@i = select( .@menu$ ) - 1;
			}
		return .@temp_name$[.@i];
	}
	
	function	func_display_card	{
		for ( .@i = 0; .@i < 4; .@i++ ) {
			.@card_id = getarg( .@i );
			if ( .@card_id ) {
				.@card_found++;
				.@card[.@i] = .@card_id;
				setd( ".@card_"+.@card_id+"_count" ),getd( ".@card_"+.@card_id+"_count" ) + 1;
			}
		}
		if ( .@card_found ) {
			.@exist_card$ = "#";
			for ( .@i = 0; .@i < 4; .@i++ )
				if ( .@card[.@i] ) {
					if ( !compare( .@exist_card$,"#"+.@card[.@i]+"#" ) )
						mes "  ^6495ED>^000000 "+func_item_name( .@card[.@i],getd( ".@card_"+.@card[.@i]+"_count" ),0 );
					.@exist_card$ = .@exist_card$ + .@card[.@i] + "#";
				}
		}
		return;
	}

	function	func_list_reward	{
		.@condition$ = getarg( 0,"" );

		query_sql( "SELECT `id`,`nameid`,`amount`,`refine`,`status`,`note` FROM `gm_reward` WHERE "+.@condition$+" ORDER BY `id` DESC LIMIT 50",.@id,.@nameid,.@amount,.@refine,.@status,.@note$ );
		.@id_size = getarraysize( .@id );
		
		mes " ";
		if ( .@id_size <= 0 ) {
			mes "^FF0000No records are founds.^000000";
		}
		else {
			mes "Found "+.@id_size+" Reward(s).";
			while( .@id_size > 0 ) {
				
				.@menu$ = "";
				for ( .@i = 0; .@i < .@id_size; .@i++ ) {
					// .@menu$ = .@menu$ + func_item_name( .@nameid[.@i],.@amount[.@i],.@refine[.@i] );
					.@menu$ = .@menu$ + .@note$[.@i];
					.@menu$ = .@menu$ + ":";
				}
				next;
				.@reward = select( .@menu$ ) - 1;
			
				if ( func_view_reward( .@id[.@reward],.@status[.@reward] ) > 0 ) {
					deletearray .@id[.@reward],1;
					deletearray .@nameid[.@reward],1;
					deletearray .@amount[.@reward],1;
					deletearray .@amount[.@reward],1;
					deletearray .@status[.@reward],1;
					.@id_size--;
				}
			}
		}
		return;
	}
	
	function	func_view_reward	{
		.@id = getarg(0);
		.@status = getarg(1);
		
		if ( .@id <= 0 ) {
			mes "Invalid Reward - "+.@id;
			return 0;
		}
		else {
			query_sql( "SELECT `gm_name`,`nameid`,`amount`,`refine`,`reward_time`,`claim_name`,`claim_time`,`note`,`card1`,`card2`,`card3`,`card4` FROM `gm_reward` WHERE `id` = '"+.@id+"' LIMIT 1",.@gm_name$,.@nameid,.@amount,.@refine,.@reward_time$,.@claim_name$,.@claim_time$,.@note$,.@card1,.@card2,.@card3,.@card4 );
			
			.@item_name$ = func_item_name( .@nameid,.@amount,.@refine );
			mes "^0055FF[ Reward # "+.@id+" ]^000000";
			mes "GM : ^777777"+.@gm_name$+"^000000";
			mes "Player : ^777777"+.@claim_name$+"^000000";
			mes "Item: ^0055FF"+.@item_name$+"^000000";
			.@item_type = getiteminfo( .@nameid,2 );
			if ( .@item_type == IT_ARMOR || .@item_type == IT_WEAPON )
				func_display_card( .@card1,.@card2,.@card3,.@card4 );
				
			mes "Date: ^777777"+.@reward_time$+"^000000";
			mes "Claimed: "+( ( .@status )? "^777777"+.@claim_time$:"^FF0000not yet" )+"^000000";
			mes "GM Note : ";
			mes "^777777"+.@note$+"^000000";
			
			.@char_name$ = strcharinfo(0);
			.@is_gm = ( getgmlevel() >= .gm_level );
			
			switch( select( 
				( .@char_name$ == .@claim_name$ && !.@status && .@claim_time$ == "0000-00-00 00:00:00" ) ? "Claim Reward":"",
				( .@is_gm && .@status == 0 ) ? "^FF0000Delete Reward^000000":"",
				( .@is_gm && .@status == 2 ) ? "^FF0000Recover Reward^000000":"",
				"Back"
			)){
				default: return 0;
				case 1:
					if ( !checkweight( .@nameid,.@amount ) ) {
						message .@char_name$,"Overweight Detected!";
					}
					else {
						query_sql( "UPDATE `gm_reward` SET `status` = 1,`claim_time` = NOW() WHERE `id` = "+.@id+" LIMIT 1" );
						if( .@nameid == 5582 || .@nameid == 5581 ){
							getitem2 .@nameid,.@amount,1,.@refine,0,.@card1,.@card2,.@card3,.@card4;
							#CASHPOINTS += 500;
							dispbottom "Received 500 Cash Points";
							callfunc("F_CashLog",#CASHPOINTS,500,"PvP Match");
							message .@char_name$,"Claimed Reward # "+.@id+" - "+.@item_name$+" and 500 Cash Points";
						} else {
							getitem2 .@nameid,.@amount,1,.@refine,0,.@card1,.@card2,.@card3,.@card4;
							message .@char_name$,"Claimed Reward # "+.@id+" - "+.@item_name$;
						}
						break;
					}
				case 2:
					query_sql( "UPDATE `gm_reward` SET `status` = 2 WHERE `id` = "+.@id+" LIMIT 1" );
					message .@char_name$,"Deleted Reward # "+.@id+" - "+.@item_name$;
					break;
				case 3:
					query_sql( "UPDATE `gm_reward` SET `status` = 0 WHERE `id` = "+.@id+" LIMIT 1" );
					message .@char_name$,"Recovered Reward # "+.@id+" - "+.@item_name$;
					break;
			}
		}
		return 1;
	}
}







/*

CREATE TABLE IF NOT EXISTS `gm_reward` (
	`id` int(11) NOT NULL AUTO_INCREMENT,
	`gm_aid` int(11) NOT NULL default '0',
	`gm_name` varchar(30) NOT NULL default '',
	`nameid` int(11) NOT NULL default '0',
	`amount` int(11) unsigned NOT NULL default '0',
	`refine` tinyint(3) unsigned NOT NULL default '0',
	`card1` smallint(4) unsigned NOT NULL default '0',
	`card2` smallint(4) unsigned NOT NULL default '0',
	`card3` smallint(4) unsigned NOT NULL default '0',
	`card4` smallint(4) unsigned NOT NULL default '0',
	`format_name` varchar(30) NOT NULL default 'null',
	`reward_time` datetime NOT NULL default '0000-00-00 00:00:00',
	`status` tinyint(1) NOT NULL default '0',
	`claim_aid` int(11) NOT NULL default '0',
	`claim_name` varchar(30) NOT NULL DEFAULT '',
	`claim_time` datetime NOT NULL default '0000-00-00 00:00:00',
	`note` varchar(255) NOT NULL default 'none',
	PRIMARY KEY  (`id`)
) ENGINE=MyISAM;

CREATE TABLE IF NOT EXISTS `item_reward` (
  id int(11) NOT NULL AUTO_INCREMENT,
  nameid int(11) NOT NULL DEFAULT 0,
  qty smallint(6) DEFAULT 1,
  refine smallint(2) DEFAULT 0,
  card1 smallint(4) DEFAULT 0,
  card2 smallint(4) DEFAULT 0,
  card3 smallint(4) DEFAULT 0,
  card4 smallint(4) DEFAULT 0,
  PRIMARY KEY (id)
)
ENGINE = MyISAM;

*/

