Random Plates -
McGuiness - 11.05.2017
Hey all,
So i was trying to create a custom car ownership system and once I had finished it I added a random number plate generator, but with this I want it to search the database so it doesn't repeat the same plate over and over so that way the LEO's can find who the plate really belongs to, but when I try it, it just keeps repeating the stock over and over when I want it to return the plate that isn't taken, can someone please help me?
pawn Код:
stock GetRandomPlate()
{
new string[128];
format(string, sizeof(string), "%d%s%s%s%d%d%d", random(10), LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], random(10), random(10), random(10));
mysql_format(Mysql_users, GlobalQuery, sizeof(GlobalQuery), "SELECT * FROM `playervehicles` WHERE `Plate` = '%s'", string);
mysql_query(Mysql_users, GlobalQuery);
if(cache_get_row_count() == 1)
{
GetRandomPlate();
}
return string;
}
Re: Random Plates -
Jefff - 11.05.2017
You can hash player account id in MD5 (its unique id) then taking first 8 characters from that hash and you will be sure that will not happen again
First collision when MD5(82945) = "7b763dcb..." (same result as MD5(25302)) but i do not think you have so many vehicles
pawn Код:
stock GetRandomPlate(playerid)
{
new string[9]; // 8 + 1 extra for end of string
mysql_format(Mysql_users, GlobalQuery, sizeof(GlobalQuery), "SELECT UPPER(SUBSTRING(MD5('%d'),1,8));", PlayerAccountID[playerid]);
new Cache:result = mysql_query(Mysql_users, GlobalQuery);
cache_get_row(0, 0, string, Mysql_users, 9);
cache_delete(result,Mysql_users);
// INSERT OR UPDATE playervehicles Plate = string
return string;
}
Re: Random Plates -
Vince - 12.05.2017
^ Assuming that players can have exactly one car.
Why not just work with regular incrementing plates? The format you choose is, perhaps by sheer coincidence, almost the same as the Belgian format. E.g. 1-ABC-123 is simply followed by 1-ABC-124. Yes, this makes it possibly for others to determine the approximate time a plate was issued but this works the same in real life.
Re: Random Plates -
McGuiness - 12.05.2017
This isn't what I mean, I want the plates to be random (I didn't know it was the belgian format, I just made a random one from a U.S plate I saw). I want it so first it checks if the plate is in the database, if the plate is in the database I want it to generate a different plate, BUT if the plate is not in the database I want it to return the plate, kind of like a rank system where it returns the ranks name
Re: Random Plates -
McGuiness - 12.05.2017
I should've mentioned this but I'm trying ot create this:
pawn Код:
stock GenerateRegistration() //states the name of the stock
{
new registration = random(9999); //Generates a number out of 9999
foreach(Player, i) //Searches every user in the server checking if the vehicle plate is used.
{
if(PlayerInfo[i][vReg] == registration || registration < 1000) // Checks if the reg is in use or if its below 1000.
{
GenerateRegistration(); //Regenerates a reg if the criteria is not met
return 1;
}
}
return registration; //returns the reg if the criteria is met
}
But I want it to SEARCH the database and if the criteria isn't met, then it does it again until the criteria is met!
Re: Random Plates -
Tord - 13.05.2017
I suggest you to read this first:
https://sampwiki.blast.hk/wiki/MySQL#mysql_query
It's necessary in all queries used without threading, to clear the cache after being used. In the first code snippet you posted, I can't see any active "Cache". Example of assigning cache to a variable:
PHP код:
new Cache:varname
varname = mysql_query.....
//when done with the operations, do this:
if(cache_is_valid(varname)
{
cache_delete(varname);
}
. So first, get that sorted out - by deleting the cache when done.
In order to check the database - it seems your query is correct. Try with the cache stuff I mentioned and see if it solves your problem.
Secondly, if you want the reg to be above 1000 and below 10000 - do this:
PHP код:
new registration= random(8999) + 1000
This will always meet the criteria-
Re: Random Plates -
McGuiness - 13.05.2017
I can't get the layout right, I'm not sure how it's meant to be layed out and I've tried looking at some posts on the SAMP forums but I can't understand them that well as it takes me a bit to actually click in my head how the layout should work etc.
This is what I put:
pawn Код:
stock GetRandomPlate()
{
new string[128];
format(string, sizeof(string), "%d%s%s%s%d%d%d", random(10), LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], random(10), random(10), random(10));
mysql_format(Mysql_users, GlobalQuery, sizeof(GlobalQuery), "SELECT * FROM `playervehicles` WHERE `Plate` = '%s'", string);
new Cache:dbplate = mysql_query(Mysql_users, GlobalQuery);
if(cache_is_valid(dbplate))
{
cache_delete(dbplate);
GetRandomPlate();
}
return string;
}
Would that work or did I just get the layout of it all wrong? I'm still trying to learn how to use the R40 version as before it came out I was using an older version and it's trying to learn all the new features that replaced all the old ones.
Re: Random Plates -
Jefff - 13.05.2017
This should work
pawn Код:
stock GetRandomPlate(new_plate_string[], s_size = sizeof(new_plate_string))
{
format(new_plate_string, s_size, "%d%s%s%s%d%d%d", random(10), LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], random(10), random(10), random(10));
mysql_format(Mysql_users, GlobalQuery, sizeof(GlobalQuery), "SELECT * FROM `playervehicles` WHERE `Plate` = '%s' LIMIT 1;", string);
new Cache:dbplate = mysql_query(Mysql_users, GlobalQuery);
if(cache_is_valid(dbplate))
{
cache_delete(dbplate);
GetRandomPlate(new_plate_string, s_size);
}
}
but you need change usage to
pawn Код:
new string[8];
GetRandomPlate(string);
// new plate as string
Re: Random Plates -
McGuiness - 13.05.2017
Quote:
Originally Posted by Jefff
This should work
pawn Код:
stock GetRandomPlate(new_plate_string[], s_size = sizeof(new_plate_string)) { format(new_plate_string, s_size, "%d%s%s%s%d%d%d", random(10), LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], random(10), random(10), random(10)); mysql_format(Mysql_users, GlobalQuery, sizeof(GlobalQuery), "SELECT * FROM `playervehicles` WHERE `Plate` = '%s' LIMIT 1;", string); new Cache:dbplate = mysql_query(Mysql_users, GlobalQuery); if(cache_is_valid(dbplate)) { cache_delete(dbplate); GetRandomPlate(new_plate_string, s_size); } }
but you need change usage to
pawn Код:
new string[8]; GetRandomPlate(string); // new plate as string
|
Would it work like this if it was on a different part so say like:
new string[8];
format(CarInfo[vehid][Plate], CarInfo[vehid][Plate], "%s", GetRandomPlate(string));
Would that work or does it work differently?
Re: Random Plates -
Jefff - 13.05.2017
pawn Код:
new string[8];
GetRandomPlate(string);
format(CarInfo[vehid][Plate], sizeof_CarInfoPlateHere, "%s", string);
you need also add cache_get_row_count in that if
pawn Код:
if(cache_get_row_count())