//@unloadnpc promoclaim
//@loadnpc npc/valhalla/claim.txt

-	script	promoclaim	-1,{
end;
OnInit:
	query_sql ("CREATE TABLE IF NOT EXISTS `claim_codes` ("+
	  "`code` varchar(12) NOT NULL DEFAULT '',"+
	  "`nameid` int(11) UNSIGNED NOT NULL DEFAULT 0,"+
 	  "`item_name` varchar(45) NOT NULL DEFAULT '',"+
	  "`amount` int(11) UNSIGNED NOT NULL DEFAULT 0,"+
	  "`time_created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',"+
	  "`redeem_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',"+
	  "`account_id` int(11) UNSIGNED NOT NULL DEFAULT 0,"+
	  "`char_id` int(11) UNSIGNED NOT NULL DEFAULT 0,"+
	  "PRIMARY KEY (code)"+
	  ") ENGINE = MYISAM;"
	);	
	bindatcmd "claim",strnpcinfo(3)+"::OnClaim";
	end;

OnClaim:
	mes "^FF0000 Enter your a Claim Code:";
	input .@pc$;
	if ( query_sql("SELECT nameid,amount FROM claim_codes WHERE `code`='"+md5(.@pc$)+"'", .@item,.@amt) == 0) {
		dispbottom "Invalid cash code. This transaction has been logged.";
		close;
	} else if (query_sql("SELECT code,claimed FROM claim_codes WHERE `code`='"+md5(.@pc$)+"' AND `claimed`>'0'",.@dummy,.@dummy)) {
		dispbottom "This cash code has already been claimed.";
		close;
	} else {
		query_sql("UPDATE claim_codes SET `redeem_time`=NOW(),`account_id`='"+getcharid(3)+"',`char_id`='"+getcharid(0)+"',`claimed`= 1 WHERE `code` = '"+md5(.@pc$)+"'" );
		if (.@item == 7179) {
			.@cv = .@amt/100;
			.@cr = .@amt - (.@cv * 100);
			dispbottom "Donation Credits: "+F_InsertComma(.@amt);
			dispbottom "You will receive: ";
			dispbottom "   "+.@cv+" Cash Voucher";
			dispbottom "   "+.@cr+" Cash Points";
			#CASHPOINTS += .@cr;
			getitem 7179,.@cv;
			callfunc("F_CashLog",#CASHPOINTS,.@cr,"Redeemed Donation");
			close;
		} else if (.@item == 7711){ // Event Ticket
			#CASHPOINTS += 500;	
			callfunc("F_CashLog",#CASHPOINTS,500,"Tag & Share Event");	
			getitem 30023,24;
			chanmsg("#system","Congratulation! Player [ "+strcharinfo(PC_NAME)+" ] has claimed 500x Cash Points and 24x Playtime TOken from Facebook Tag & Share Event.");
		} else if (.@item == 6243){
			getitem 30011,350;
			getitem 30012,350;
		} else if (.@item == 12622){
			getitembound .@item,.@amt,2;
		} else {
			getitem .@item,.@amt;
			logmes "CLAIM: "+.@pc$+" redeemed "+.@item+" "+.@amt;
		}
		close;
	}
	close;
}

