SA-MP Forums Archive
Don't get the same number! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Don't get the same number! (/showthread.php?tid=263554)



Don't get the same number! - The Woody - 22.06.2011

Ive made a phone system, but you can have the same numbers and i don't want that, is there anyway i could make so people can't have the same number? i guess i could loop through all players numbers and if there is a match it won't work. Im new at that area so does anybody have any suggestions or something? i would appreciate any help!


Re: Don't get the same number! - Jeffry - 22.06.2011

Save all the used numbers in a file. Then, when someone buys a new number, loop through the file's context and use strcmp between the line of the file and the number (formatted to strings [wiki => strcmp helps]).
If the file is read to the end, and the strcmp was never same, then he/she may use the number, in case that there is a line same to the number, stop the loop and return that the number is already in usage.
Got that? ^^

Jeffry


Re: Don't get the same number! - The Woody - 22.06.2011

Quote:
Originally Posted by Jeffry
Посмотреть сообщение
Save all the used numbers in a file. Then, when someone buys a new number, loop through the file's context and use strcmp between the line of the file and the number (formatted to strings [wiki => strcmp helps]).
If the file is read to the end, and the strcmp was never same, then he/she may use the number, in case that there is a line same to the number, stop the loop and return that the number is already in usage.
Got that? ^^

Jeffry
I think i got it ^^. Then if they get a new number then delete it from the file again. Well im gonna try to make this thanks for your suggestion/help .


Re: Don't get the same number! - Jeffry - 22.06.2011

Quote:
Originally Posted by The Woody
Посмотреть сообщение
I think i got it ^^. Then if they get a new number then delete it from the file again. Well im gonna try to make this thanks for your suggestion/help .
No, if they get a new number, you have to add it to the file (Filename e.g.: UsedNumbers.txt). There are all the numbers inside which are in use.
If you have any questions feel free to ask.

Jeffry


Re: Don't get the same number! - The Woody - 22.06.2011

Quote:
Originally Posted by Jeffry
Посмотреть сообщение
No, if they get a new number, you have to add it to the file (Filename e.g.: UsedNumbers.txt). There are all the numbers inside which are in use.
If you have any questions feel free to ask.

Jeffry
Yes, but if i get number 4444 and i will have a new number i get number 5555 i have to delete that from the file so other people can get number 4444 since its not in use anymore .


Re: Don't get the same number! - Jeffry - 22.06.2011

Quote:
Originally Posted by The Woody
Посмотреть сообщение
Yes, but if i get number 4444 and i will have a new number i get number 5555 i have to delete that from the file so other people can get number 4444 since its not in use anymore .
Absolutely correct. I have misunderstood you then.
I ment, if someone creates a new number, you have to add it to the file. But for sure, if the number changes then you have to search=>delete the line and replace it with the new number.

Jeffry


Re: Don't get the same number! - Babul - 22.06.2011

i suggest you to waste some harddisc space for creating one file per number, and let the filesystem do the dirty work (sorting) for you:
Код:
new PhoneNumber[MAX_PLAYERS];//you already got an array for the phonenumbers. use yours indeed..
heres a sort of PhoneNumer saving snippet:
Код:
new filename[64];
new filecontent[64];
new PlayerName[MAX_PLAYER_NAME];
GetPlayerName(playerid,PlayerName,sizeof(PlayerName));
format(filename,sizeof(filename),"PhoneNumbers/%d",PhoneNumber[playerid]);
format(filecontent,sizeof(filecontent),"%s",PlayerName);
new File:PhoneFile=fopen(filename,io_readwrite);//open file "11275"..
fwrite(PhoneFile,filecontent);//write "Babul" into it,
fclose(PhoneFile);//and close the file. done.
a /call or /GetPlayerFromPhoneNumber <number> command is possible very easy this way...
if i would get the number 11275 as "Babul", then the file "scriptfiles/PhoneNumbers/11275" would contain simply "Babul".

if you want a /GetPhoneNumberFromPlayer <Name> command, its also a good idea to store the PhoneNumber in another file with the PlayerName as filename...


Re: Don't get the same number! - Jeffry - 22.06.2011

Yes, the way as Babul suggested is better and is faster to create and faster in reading/writing.
Better do it like this, then you can also save some extra things in the 'per-number-file', like Rest-Money to phone.

Jeffry


Re: Don't get the same number! - The Woody - 22.06.2011

Quote:
Originally Posted by Babul
Посмотреть сообщение
i suggest you to waste some harddisc space for creating one file per number, and let the filesystem do the dirty work (sorting) for you:
Код:
new PhoneNumber[MAX_PLAYERS];//you already got an array for the phonenumbers. use yours indeed..
heres a sort of PhoneNumer saving snippet:
Код:
your code..
a /call or /GetPlayerFromPhoneNumber <number> command is possible very easy this way...
if i would get the number 11275 as "Babul", then the file "scriptfiles/PhoneNumbers/11275" would contain simply "Babul".

if you want a /GetPhoneNumberFromPlayer <Name> command, its also a good idea to store the PhoneNumber in another file with the PlayerName as filename...
Oh ive made it like this:
pawn Код:
#define ALL_NUMBER_FILE             "AllNumbers.txt" // on top
 
new string[128], Number; // when i buy a phone
Player[playerid][PhoneN] = 940+random(699);
format( string, sizeof( string ), "You have purchased a cellphone. Your number is %d.", Player[playerid][PhoneN]);
SendClientMessage( playerid, WHITE, string);
Number = Player[playerid][PhoneN];
AddNumberToFile(ALL_NUMBER_FILE, Number);
 

stock AddNumberToFile(DFileName[], Number) // a stock.
{
    new File:NumFile, Line[128];

    format(Line, sizeof(Line), "%i\r\n", Number);
    NumFile = fopen(DFileName, io_append);
    fwrite(NumFile, Line);
    fclose(NumFile);
    return 1;
}
This only uses one txt file but maybe your way is better ill try playing around with it.


Re: Don't get the same number! - Jeffry - 22.06.2011

Ok, something here:

you better make it as Babul said.
I will make some parts for you:

where you buy the number:
pawn Код:
new string[128], Number;
NEWNUMBER:
Number = 940+random(699);
new numberstr[50];
format(numberstr, sizeof(numberstr), "/Numbers/%d.txt", Number);  //Create a folder called 'Numbers'.
if(fexist(numberstr)) //Checks if the number is already created
{
    goto NEWNUMBER;
    //Will get a new number, in case the current ones does already exist.
}
else
{
    //Creates the file for the corresponding number:
    new File:num;
    num=fopen(numberstr, io_readwrite);
    //Use 'fwrite' here to write something into the file.
    fclose(num);
    Player[playerid][PhoneN] = Number;
}
format( string, sizeof( string ), "You have purchased a cellphone. Your number is %d.", Player[playerid][PhoneN]);
SendClientMessage( playerid, WHITE, string);

!!: You have to create a folder in scriptfiles called: Numbers

If you have any more questions, feel free to ask.
Btw, you can also make it, that the player has to enter the number he/she wishes into a dialog and check then if the number exists. Would look like this:

pawn Код:
//DialogResponse...
new string[128], Number;
Number = strval(inputtext);
if(Number > 2000000000 || Number < 10000) return SendClientMessage(playerid, RED, "ERROR: Invalid number.");
new numberstr[50];
format(numberstr, sizeof(numberstr), "/Numbers/%d.txt", Number);  //Create a folder called 'Numbers'.
if(fexist(numberstr)) //Checks if the number is already created
{
    return SendClientMessage(playerid, RED, "ERROR: Number already in use, take another..");
}
else
{
... same as obove
You have to create the folder here too.

So, if you have any more questions, feel free to ask.

Greetings,
Jeffry