Phone system help!
#1

case 0:
{
new rand = random(100000),string[64];
if(PlayerInfo[playerid][pCellphone] == 1) return SendClientMessage(playerid, COLOR_GREY, "You already have a phone");
PlayerInfo[playerid][pNumber] = rand;
PlayerInfo[playerid][pCellphone] += 1;
format(string,sizeof(string), "Your new phone number is now %d",PlayerInfo[playerid][pNumber]);
SendClientMessage(playerid, COLOR_WHITE, string);
UpdateVariable(playerid, pNumberx);
UpdateVariable(playerid, pCellphonex);
}
So this is what happens when a player buys a phone form the shop,now,it works perfectly but i do have some things that i want to add and i have no ideea how.
First: I want the phone numbers to be generated from 10,000 to 100,000 because this,new rand = random(100000),this generates then numbers from 0 to 100,000 or from 1 i don't know for sure.


And second,if a number is already asociated to a player,like if a player buys a phone and he gets a random phone number
i want that number to not be given to another player ever again.
I do hope you can understand what i want to do and i hope you can help me.
Thanks in advance
Reply
#2

Quote:
Originally Posted by bosmania
Посмотреть сообщение
case 0:
{
new rand = random(100000),string[64];
if(PlayerInfo[playerid][pCellphone] == 1) return SendClientMessage(playerid, COLOR_GREY, "You already have a phone");
PlayerInfo[playerid][pNumber] = rand;
PlayerInfo[playerid][pCellphone] += 1;
format(string,sizeof(string), "Your new phone number is now %d",PlayerInfo[playerid][pNumber]);
SendClientMessage(playerid, COLOR_WHITE, string);
UpdateVariable(playerid, pNumberx);
UpdateVariable(playerid, pCellphonex);
}
So this is what happens when a player buys a phone form the shop,now,it works perfectly but i do have some things that i want to add and i have no ideea how.
First: I want the phone numbers to be generated from 10,000 to 100,000 because this,new rand = random(100000),this generates then numbers from 0 to 100,000 or from 1 i don't know for sure.


And second,if a number is already asociated to a player,like if a player buys a phone and he gets a random phone number
i want that number to not be given to another player ever again.
I do hope you can understand what i want to do and i hope you can help me.
Thanks in advance
10000 + random(90000)
Reply
#3

If you want a minimum value, then you have to start at that value and generate only how much is missing from your max.
In your code it would be:
Код:
new rand = 10_000 + random(90_000);
I would suggest saving these values somewhere so that you could edit them in an easier manner.
E.g.
Код:
#define MAX_PHONE_NUMBER 100_000 //along with your other defines/globals/whatever
#define MIN_PHONE_NUMBER 10_000
new rand = MIN_PHONE_NUMBER + random(MAX_PHONE_NUMBER - MIN_PHONE_NUMBER);
That way if you want to change the phone number limit to something else, all you need to do is change the defines.

If you want to create unique phone number, you will have to use some place to store the current numbers, like a MySQL database. Store the phone numbers along with user data and after generating a new number, query to see if it already exists. I think you could do it directly in the database and then just fetch the number but I haven't had to do it and don't want mess up with it.
Reply
#4

Damn i thought it was harder,thanks,and how about the second thing i want to add?
Reply
#5

I know i have to store somewhere every number that is generated,and then when a new player comes to buy a phone i have to check if the number that it's about to be randomly given to him already exists,but i dont know how,im using a mysql data base.
Reply
#6

You would need to query the list of phone numbers in your database. Something like this:

"SELECT COUNT(*) FROM `accounts` WHERE `PhoneNumber` = '%i'"
Format this string and insert your newly generated phonenumber into the %i placeholder with format();
Then query the database, get and save the result of this and see what you received. If 0 - you are OK, if more - you need to generate the phone again because it already exists.

Replace `Accounts` with the table name where you keep your phone numbers, and `PhoneNumber` with how you call your phone number column in you database.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)