Seeking if a phone number is unused
#1

Good morning, or afternoon / evening,

I'm a little bit stuck, not a big problem but I need to know the right solution to learn from. I explain:

I'm making the phone system for my roleplay. All piece of cake, but then buying the cellphone.. I want to write in a file which numbers are used. Simple to. But how to check if a certain number is in a file? For example:

I have pressed to buy a cellphone. In the script happens: 'new ph = random(10000);'. This would return a number from 0-10000. Lets say it has returned 7800.

Now, how to check if '7800' is not already bought? If it is, it has already been written in a file.

Now the main question: how to check if 7800 is already written in a file?



Hope you have understood it.
Regards, Jochem
Reply
#2

Theres only a 1 in a 10000 chance it would be the same so I wouldn't check it.
Reply
#3

Okey, you have to create a file first of all, lets call it Numbers.txt. (You have to create it at your scriptfiles folder.
Now, let's start with the code:

At TOP:
pawn Код:
#define MAX_PHONE_NUMBERS 10000
new PhoneNumbers[MAX_PHONE_NUMBERS];
At your command:
pawn Код:
if(PhoneNumbers[ph]==1) ph = GetFreeNumber();
if(ph != -1)
{
    new File:Code, string[20]; //Here we will write it into the file:
    Code = fopen("Numbers.txt",io_append);
    format(string,sizeof(string),"%d\r\n",ph);
    fwrite(Code,string);
    fclose(Code);
    //Your phone buy code...
}
else return SendClientMessage(playerid, COLOR_RED, "ERROR: No free phone number anymore!");
At OnGameModeInit
pawn Код:
new string[20], File:Code;
if((Code = fopen("Numbers.txt",io_read)))
{
    while(fread(Code,string))
    {
        PhoneNumbers[strval(string)] = 1;
    }
}
At bottom:
pawn Код:
stock GetFreeNumber()
{
    for(new i=0; i<MAX_PHONE_NUMBERS; i++) if(PhoneNumbers[i] != 1) return i;
    return -1;
}
I've just typed this, so it might have bugs, which I don't hope, but who knows. ^^
If there are any bugs/errors, and you don't know how to go on, feel free to ask.

Jeffry
Reply
#4

Quote:
Originally Posted by Retardedwolf
Посмотреть сообщение
Theres only a 1 in a 10000 chance it would be the same so I wouldn't check it.
I need to be 100% ensured. If there are 1000 numbers bought there will be a really big chance (1 / 10).
Reply
#5

Modified so it fits, it crashes with the code on OnGameModeInit:

pawn Код:
new File:UsedNumbers;
    if(UsedNumbers == fopen("Numbers.txt",io_read))
    {
        while(fread(UsedNumbers,string))
        {
            PhoneNumbers[strval(string)] = 1;
        }  
    }
The server just closes itself after loading the (0) filterscripts. Nothing in server_log.
Reply
#6

Have you created the file? If not, the server will crash.
Reply
#7

Yea, I have.
Reply
#8

I've tested this one:
pawn Код:
new File:UsedNumbers, stringy[20];
    if((UsedNumbers = fopen("Numbers.txt",io_read)))
    {
        while(fread(UsedNumbers,stringy))
        {
            PhoneNumbers[strval(stringy)] = 1;
        }
    }
    fclose(UsedNumbers);
Worked for me.

I've written into the Numbers.txt:
Код:
12
54
55
Just to test it.

Try it out, it should work.
Reply
#9

Ok, I got another piece of code. I know I can use yours, if I can't fix this, I will.

pawn Код:
stock GetFreeNumber(playerid)
{
    new string[50],Playername[MAX_PLAYER_NAME];
    new ph = random(1000000);
    new File:NumFile = fopen("Stuff/Numbers.txt",io_read);
    fread(NumFile,string);
    {
        if(strval(string) == ph) return GetFreeNumber(playerid);   
    }
    fclose(NumFile);
    GetPlayerName(playerid,Playername,sizeof(Playername));
    format(file,sizeof(file),"Accounts/%s.ini",Playername);
    dini_IntSet(file,"PhoneNumber",ph);
    return ph;
}
Means server crash. Nothing gets writed. What's the problem? :S
Reply
#10

And you really could compile this code?

You forgot the loop.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)