-	script	referral	-1,{
OnInit:
	bindatcmd("referral","referral::OnCheck",0,99);
	set .rlvl,75;
	end;

OnCheck:
	mes "Hello ^28bf00"+strcharinfo(0)+"^000000, here is your referral report";
	mes "- - - - - -";
	mes "- ^FF00001,000 Credits^000000 per valid referral";
	mes "- Max of ^FF000010^000000 accounts to claim";
	mes "- Account should reach ^FF0000Base Level 75^000000 on any of his character.";
	mes "^28bf00Status:^000000";
	mes "- - - - - -";
	query_sql("SELECT account_id,redeemed,level FROM referral WHERE `referral_id` = "+getcharid(CHAR_ID_ACCOUNT)+" ORDER BY `redeemed` DESC, `level` DESC LIMIT 10", .@referral_id,.@redeemed,.@level);
	.@size = getarraysize( .@referral_id );
	if( !.@size ){
		mes "You have not referred anyone yet.  Please visit ^FF0000http://www.classicro.net/?module=referral^000000 to start inviting friends.";
		close;
	} else {
		set .@player_menu$,"";
		for( .@i = 0; .@i < .@size; .@i++ ){
			.@player_menu$ = .@player_menu$ + ( .@i+1 )+". " + .@referral_id[.@i]+" Lvl [ "+.@level[.@i]+" ] - "+(.@redeemed[.@i] ? "^FF0000Claimed^000000" : "^28bf00Unclaimed^000000")+":";
		}
		set .@Select,select( .@player_menu$ ) - 1;
		set .@referral_id,.@referral_id[.@Select];
		set .@redeemed,.@redeemed[.@Select];
		set .@level,.@level[.@Select];
		if (.@redeemed) {
			mes "This referral has been claimed already";
			 close;
		}
		if (.@level < .rlvl ) {
			mes "Referred account must reach level "+.rlvl+" on any of his character";
			close;
		}
		chanmsg("#system",""+strcharinfo(0)+" has claimed 1,000 Credit Points from referral system. You can also earn as much as 10,000 Credit by inviting your friend. Visit http://classicro.net/?module=referral for more info.");
		query_sql "UPDATE referral SET `redeemed` = 1, credits_before="+callfunc("F_CheckCredit")+", credits_after="+(callfunc("F_CheckCredit")+1000)+", redemption_date ='"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"' WHERE `account_id` = "+.@referral_id;
		callfunc("F_UpdateCredit",+1000);
		close;
	}
	end;

OnPCLoginEvent:

	query_sql("SELECT `referral` FROM login WHERE `account_id` = "+getcharid(3),.@isref);
	if (!.@isref) end;

	.@bool = 0;
	query_sql("SELECT `account_id`,`level`,`redeemed` FROM referral WHERE `account_id` = "+getcharid(CHAR_ID_ACCOUNT), .@bool,.@level,.@redeemed);
	if( .@bool ){
		if(.@level < BaseLevel && !.@redeemed ) {
			query_sql "UPDATE referral SET `level` = "+BaseLevel+" WHERE `account_id` = "+getcharid(CHAR_ID_ACCOUNT);
		}
	} else {
		query_sql "INSERT INTO referral (account_id,referral_id,redeemed,redemption_date,credits_before,credits_after,level) VALUES ("+getcharid(CHAR_ID_ACCOUNT)+","+.@isref+",0,NULL,0,0,"+BaseLevel+")";
	}
	end;
}	 