//===== Custom Script ===========================================================
//= Cash Codes
//===== By: =====================================================================
//= Arzzzae
//===== Current Version: ========================================================
//= 1.0 Arzzzae
//= 2.2 by Innos for www.ex-ro.de
//===== Compatible With: ========================================================
//= Latest svn
//===== Description: ============================================================
//= Adds 2 custom atcommands. @claim for players to claim
//= Cash code created by Game Masters. @code is for
//= administrators to add/view/delete Cash codes.
//===== Additional Comments: ====================================================
//= 1.0 - Initial Release.
//= 2.0 - SQL Table with char_id and close Code
//= 2.1 - Code can be used only from one Player or every Account once time. 
//= 2.2 - Code can store Text from 4-10 length.
//===============================================================================

//@loadnpc npc/thor/claim_codes.txt
//@unloadnpc promoclaim

-	script	promoclaim	-1,{

OnInit:

/*
CREATE TABLE claim_codes (
  code varchar(12) NOT NULL DEFAULT '',
  nameid int(11) UNSIGNED NOT NULL DEFAULT 0,
  item_name varchar(45) NOT NULL DEFAULT '',
  amount smallint(6) 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
AVG_ROW_LENGTH = 78
CHARACTER SET latin1
COLLATE latin1_swedish_ci;
*/

	bindatcmd "claim",strnpcinfo(3)+"::OnClaim";
	end;

OnClaim:
	mes "^FF0000 Enter your 12 Digit Claim Code:";
	input .@pc$;
	close2;

		if ( query_sql("SELECT nameid,amount FROM claim_codes WHERE `code`='"+md5(.@pc$)+"'", .@item,.@amt) == 0) {
			query_logsql "INSERT INTO claimlog (`claim_date`,`account_id`,`char_id`,`char_name`,`claim_code`) VALUES ('"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"',"+getcharid(CHAR_ID_ACCOUNT)+","+getcharid(CHAR_ID_CHAR)+",'"+escape_sql(strcharinfo(PC_NAME))+"','"+escape_sql(.@pc$)+"')";
			dispbottom "Invalid Cash code. This transaction has been logged.";
			end;
			}				
		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.";
			end;
			}
		else {
			getitem .@item,.@amt;
			if (.@item == 7557) {
				getitem(Emperium,1);
			}
			/*
			if (.@amt == 10) {
				getitem 7179,2;		// 2000 Rok
				logmes "BONUS: 2 CASH VOUCHER";
			}
			if (.@amt == 17) {
				getitem 7179,4;		// 4000 Rok
				logmes "BONUS: 4 CASH VOUCHER";
			}

			if (.@amt == 26) {
				getitem 7179,6;		// 6000 Rok
				logmes "BONUS: 6 CASH VOUCHER";

			}
			if (.@amt == 34) {
				getitem 7179,8;		// 8000 Rok
				logmes "BONUS: 8 CASH VOUCHER";
				
			}
			if (.@amt == 43) {
				getitem 7179,10;	// 10000 Rok
				logmes "BONUS: 10 CASH VOUCHER";
				
			}
			if (.@amt == 95) {
				getitem 7179,30;	// 30000 Rok
				logmes "BONUS: 30 CASH VOUCHER";	
				
			}
			*/
		
			dispbottom "Cash code successfuly redeemed.";
			logmes "CLAIM: "+.@pc$+" redeemed "+.@item+" "+.@amt;
			query_sql("UPDATE claim_codes SET `redeem_time`=NOW(),`account_id`='"+getcharid(3)+"',`char_id`='"+getcharid(0)+"',`claimed`= 1 WHERE `code` = '"+md5(.@pc$)+"'" );
			end;
		}
				
}