Jump to content
  • 0

Random Code Generator


Onairda

Question


  • Group:  Members
  • Topic Count:  71
  • Topics Per Day:  0.03
  • Content Count:  167
  • Reputation:   8
  • Joined:  12/30/16
  • Last Seen:  

Hello Everyone,

I was hoping if you could help me, to have the below script work. What I want is create a random generator code "Transaction Code" temporarily, which means only transacting players can see the generated Transaction Code and even 100 players using the script it wont give the same Transaction Code.

prontera,155,155,3	script	TESTSTE	92,{

	setarray .coupon_code$[0],"A","B","C","D","E","F","G","H","I","J","K","L",
							  "M","N","O","P","Q","R","S","T","U","V","W","X",
							  "Y","Z","0","1","2","3","4","5","6","7","8","9";
	
	set .@new_transaction$, "";
	set .@code_exists$, 0;

	mes "test";
	while (@code_exists == 0) {
		for (set @i, 0; @i < 6; set @i, @i+1) {
			set .@random_char$, rand(0, (getarraysize(.coupon_code$) - 1));
			set .@new_transaction$, .@new_transaction$ + .coupon_code$[.@random_char$];
		}

		query_sql("SELECT COUNT(*) FROM transaction_tbl WHERE transaction_code='" + .@new_transaction$ + "'", .@code_count);
		if (.@code_count == 0) {
			set .@code_exists$, 1;
			query_sql("INSERT INTO transaction_tbl (transaction_code) VALUES ('" + .@new_transaction$ + "')");
			mes "Transaction Code: " + .@new_transaction$ + " inserted into the database.";
		} else {
			set .@new_transaction$, "";
		}
	}

	set .@new_transaction$, "";

close;

}

 

Thank you

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

  • Group:  Members
  • Topic Count:  3
  • Topics Per Day:  0.01
  • Content Count:  39
  • Reputation:   29
  • Joined:  06/08/23
  • Last Seen:  

untested, but you can try this.

prontera,155,155,3	script	TESTSTE	92,{
	if (.user)
	{
		mes rid2name(.user) +" is currently per-using my service, please try again later.";
		close;
	}
	.user = getcharid(3);
	addtimer 10, strnpcinfo(0) +"::OnReleaseUser";
	mes "Generating new code for you..";
	freeloop(true);
	do
	{
		.@new_coupon$ = "";
		copyarray .@coupon_code$[0], .coupon_code$[0], .coupon_code_size;
		.@coupons = query_sql("SELECT `transaction_code` FROM `transaction_tbl` WHERE 1", .@coupons$);
		for (.@i = 0; .@i < .code_length;.@i++)
		{
			.@s = getarraysize(.@coupon_code$);
			.@r = rand(.@s);
			.@new_coupon$ += .@coupon_code$[.@r];
			deletearray .@coupon_code$[.@r], 1;
		}
		if (.@coupons < 1)
			break;
		if (inarray(.@coupons$, .@new_coupon$) == -1)
			break;
	} while (1);
	freeloop(false);
	clear;
	query_sql("INSERT INTO `transaction_tbl` (transaction_code) VALUES ('" +  escape_sql(.@new_coupon$) + "')");
	mes "New Coupon Code: "+ .@new_coupon$;
	close;

OnPCLogoutEvent:
	if (.user != getcharid(3))
		end;
OnReleaseUser:
	.user = 0;
	end;

OnInit:
	.code_length = 6;

	setarray .coupon_code$[0],
		"A","B","C","D","E","F","G","H","I","J","K","L",
		"M","N","O","P","Q","R","S","T","U","V","W","X",
		"Y","Z","0","1","2","3","4","5","6","7","8","9";

	.coupon_code_size = getarraysize(.coupon_code$);
	end;
}

 

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  4
  • Topics Per Day:  0.01
  • Content Count:  14
  • Reputation:   0
  • Joined:  12/02/22
  • Last Seen:  

On 8/27/2023 at 6:19 PM, Harvin said:

untested, but you can try this.

prontera,155,155,3	script	TESTSTE	92,{
	if (.user)
	{
		mes rid2name(.user) +" is currently per-using my service, please try again later.";
		close;
	}
	.user = getcharid(3);
	addtimer 10, strnpcinfo(0) +"::OnReleaseUser";
	mes "Generating new code for you..";
	freeloop(true);
	do
	{
		.@new_coupon$ = "";
		copyarray .@coupon_code$[0], .coupon_code$[0], .coupon_code_size;
		.@coupons = query_sql("SELECT `transaction_code` FROM `transaction_tbl` WHERE 1", .@coupons$);
		for (.@i = 0; .@i < .code_length;.@i++)
		{
			.@s = getarraysize(.@coupon_code$);
			.@r = rand(.@s);
			.@new_coupon$ += .@coupon_code$[.@r];
			deletearray .@coupon_code$[.@r], 1;
		}
		if (.@coupons < 1)
			break;
		if (inarray(.@coupons$, .@new_coupon$) == -1)
			break;
	} while (1);
	freeloop(false);
	clear;
	query_sql("INSERT INTO `transaction_tbl` (transaction_code) VALUES ('" +  escape_sql(.@new_coupon$) + "')");
	mes "New Coupon Code: "+ .@new_coupon$;
	close;

OnPCLogoutEvent:
	if (.user != getcharid(3))
		end;
OnReleaseUser:
	.user = 0;
	end;

OnInit:
	.code_length = 6;

	setarray .coupon_code$[0],
		"A","B","C","D","E","F","G","H","I","J","K","L",
		"M","N","O","P","Q","R","S","T","U","V","W","X",
		"Y","Z","0","1","2","3","4","5","6","7","8","9";

	.coupon_code_size = getarraysize(.coupon_code$);
	end;
}

 

Can i have sa sql file with this?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...