SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 20/09/2018, 08:00 AM   #4081
0xAAAAAA
Little Clucker
 
Join Date: Aug 2018
Posts: 5
Reputation: 0
Default Re: Little coding questions - For general minor queries 5

Hello. In a house system, when player stands near the entrance pickup of a house, and types /buyhouse, he gets a dialog, which he should either submit or cancel, if player submits it, then on response of this dialog there should be all the stuff with money and mysql queries. The problem is that when player types /buyhouse, server loops through all houses (which might be over 2000 on a server), to find at which house's entrance the player is at, then on respone of the dialog there is the same loop, to get id of the house again. So this is kind of ineficcient to do those 2 loops, a solution that comes to my mind is to store the id of the house in a special variable in player's enum when he types /buyhouse, then use the value in it in the ondialogresponse function, but is this a bad practice?
P.S. by id of house i mean the index of it in the array of houses, and i obviously need to change variables in it after it's purchased.
0xAAAAAA is offline   Reply With Quote
Old 20/09/2018, 08:50 AM   #4082
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,037
Reputation: 170
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by 0xAAAAAA View Post
Hello. In a house system, when player stands near the entrance pickup of a house, and types /buyhouse, he gets a dialog, which he should either submit or cancel, if player submits it, then on response of this dialog there should be all the stuff with money and mysql queries. The problem is that when player types /buyhouse, server loops through all houses (which might be over 2000 on a server), to find at which house's entrance the player is at, then on respone of the dialog there is the same loop, to get id of the house again. So this is kind of ineficcient to do those 2 loops, a solution that comes to my mind is to store the id of the house in a special variable in player's enum when he types /buyhouse, then use the value in it in the ondialogresponse function, but is this a bad practice?
P.S. by id of house i mean the index of it in the array of houses, and i obviously need to change variables in it after it's purchased.
You can avoid the 2 loops if you use streamer. In data manipulation, there is an item in enumerator called E_STREAMER_EXTRA_ID. When you create the pickup for the first time, set the array index to it. I'll give an example:
pawn Code:
// looping through all rows
// create a global variable and set value equal to the houses loaded.
// Let's call it "HousesLoaded". Say you have loaded 1500 houses so "HousesLoaded" is equal to 1500
House[i][Pickup] = CreateDynamicPickup(...);
Streamer_SetIntData(STREAMER_TYPE_PICKUP, House[i][Pickup], E_STREAMER_EXTRA_ID, i + 2000);
// native Streamer_SetIntData(type, id, data, value);
Streamer_SetIntData returns 0 if none is set, I think. This is one of the reason I did not use "i" as value but used "i + 2000" instead. You may use it in other systems so you need a way to recognize what type of pickup it is. You can use values to identify. In this case, 2000 + MAX_HOUSES, 4000 + MAX_SOMETHING_ELSE etc.

There is entrance pickup, when OnPlayerPickUpDynamicPickup is called you check for the type.
pawn Code:
// native Streamer_GetIntData(type, id, data);
new extra_id = Streamer_GetIntData(STREAMER_TYPE_PICKUP, House[i][Pickup], E_STREAMER_EXTRA_ID);

// now we want to check if subtracting 2000 from it will give us a range of 0-HousesLoaded.
if (0 <= (extra_id - 2000) <= HousesLoaded)
{
    // okay, the type of pickup is house. alternative way might be to check the pickup modelid.
    // now we'll store to another global variable the last house.
    Player[playerid][Last_House] = extra_id - 2000;

    // whenever you want to check now if the player is near a house, you can check if the player is in range with the last house
    // you know the house index and you know the position

    // show dialog to submit or cancel
}
Now on dialog response, you apply the same method. You know the last house the player pickup.
Calisthenics is offline   Reply With Quote
Old 20/09/2018, 09:28 AM   #4083
0xAAAAAA
Little Clucker
 
Join Date: Aug 2018
Posts: 5
Reputation: 0
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by Calisthenics View Post
You can avoid the 2 loops if you use streamer. In data manipulation, there is an item in enumerator called E_STREAMER_EXTRA_ID. When you create the pickup for the first time, set the array index to it. I'll give an example:
pawn Code:
// looping through all rows
// create a global variable and set value equal to the houses loaded.
// Let's call it "HousesLoaded". Say you have loaded 1500 houses so "HousesLoaded" is equal to 1500
House[i][Pickup] = CreateDynamicPickup(...);
Streamer_SetIntData(STREAMER_TYPE_PICKUP, House[i][Pickup], E_STREAMER_EXTRA_ID, i + 2000);
// native Streamer_SetIntData(type, id, data, value);
Streamer_SetIntData returns 0 if none is set, I think. This is one of the reason I did not use "i" as value but used "i + 2000" instead. You may use it in other systems so you need a way to recognize what type of pickup it is. You can use values to identify. In this case, 2000 + MAX_HOUSES, 4000 + MAX_SOMETHING_ELSE etc.

There is entrance pickup, when OnPlayerPickUpDynamicPickup is called you check for the type.
pawn Code:
// native Streamer_GetIntData(type, id, data);
new extra_id = Streamer_GetIntData(STREAMER_TYPE_PICKUP, House[i][Pickup], E_STREAMER_EXTRA_ID);

// now we want to check if subtracting 2000 from it will give us a range of 0-HousesLoaded.
if (0 <= (extra_id - 2000) <= HousesLoaded)
{
    // okay, the type of pickup is house. alternative way might be to check the pickup modelid.
    // now we'll store to another global variable the last house.
    Player[playerid][Last_House] = extra_id - 2000;

    // whenever you want to check now if the player is near a house, you can check if the player is in range with the last house
    // you know the house index and you know the position

    // show dialog to submit or cancel
}
Now on dialog response, you apply the same method. You know the last house the player pickup.


I actually used a similar system before (i was checking if the pickup id was between the first loaded pickup of house #1 and the last loaded pickup of the last loaded house #n, and then i did something like pickupid modulus 2, since i have 2 pickups at each house, to determine whether the player is at the entrance or at the exit of the house), but i was told by someone on the forum that pickups might not always be consistent with their ids, for example when a house is purchased it must change its pickup icon (so i delete the old one and create new), and players might be purchasing houses at the same time, so houses might mess up their pickup ids. Is this still a thing with the streamer method? Can i really trust the pickup ids? And thank you for answer!

Edit: As i understood now, Streamer_SetIntData can change ids of dynamic pickups?
0xAAAAAA is offline   Reply With Quote
Old 20/09/2018, 09:51 AM   #4084
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,037
Reputation: 170
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by 0xAAAAAA View Post
I actually used a similar system before (i was checking if the pickup id was between the first loaded pickup of house #1 and the last loaded pickup of the last loaded house #n, and then i did something like pickupid modulus 2, since i have 2 pickups at each house, to determine whether the player is at the entrance or at the exit of the house), but i was told by someone on the forum that pickups might not always be consistent with their ids, for example when a house is purchased it must change its pickup icon (so i delete the old one and create new), and players might be purchasing houses at the same time, so houses might mess up their pickup ids. Is this still a thing with the streamer method? Can i really trust the pickup ids? And thank you for answer!
On second thought, it might not be consistent if a new house is created during run time.

I also mentioned an alternative way, you can combine pickup modelid + extra id. You may keep the exterior pickup only and change to a checkpoint inside interiors to avoid complication. This way, you can easily retrieve the house index using the combination of the two.

As for the re-creation of the pickup when it is sold/bought, use the data manipulation and just alter the pickup modelid. Do not destroy and create again.
pawn Code:
Streamer_SetIntData(STREAMER_TYPE_PICKUP, House[house_index_here][Pickup], E_STREAMER_MODEL_ID, new_pickup_modelid_here);
Calisthenics is offline   Reply With Quote
Old 24/10/2018, 10:45 AM   #4085
Ermanhaut
Gangsta
 
Ermanhaut's Avatar
 
Join Date: Apr 2016
Location: Brazil
Posts: 657
Reputation: 47
Default Re: Little coding questions - For general minor queries 5

I'm trying to use
pawn Code:
enum (--) { // or enum (-= 1)
  VAR_ONE = -1,
  VAR_TWO,
  VAR_THREE
};

But i'm getting a syntax error:
Code:
error 001: expected token: ")", but found "--"
Am i doing it wrong or this is impossible?
__________________
try, try and try again
Ermanhaut is offline   Reply With Quote
Old 24/10/2018, 12:33 PM   #4086
Ermanhaut
Gangsta
 
Ermanhaut's Avatar
 
Join Date: Apr 2016
Location: Brazil
Posts: 657
Reputation: 47
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by ****** View Post
I donít know why that doesnít work. I canít think of a good reason to not allow it. However, for now this does work:

Code:
enum (+= -1)
{
    A, B, C
}
Now it works!

I don't know either.
Maybe it's a bug of the pawn-lang community compiler.
Ermanhaut is offline   Reply With Quote
Old 09/12/2018, 01:32 PM   #4087
Logic_
Banned
 
Join Date: Jun 2015
Posts: 1,513
Reputation: 305
Default Re: Little coding questions - For general minor queries 5

Not sure if this is the right place or not, but what is the best method to convert time stamps to human-readable date. Are there any alternatives to TimeStamp2Date include?
Logic_ is offline   Reply With Quote
Old 30/12/2018, 04:49 PM   #4088
Gforcez
Gangsta
 
Gforcez's Avatar
 
Join Date: Jul 2010
Location: The Netherlands.
Posts: 761
Reputation: 435
Default Re: Little coding questions - For general minor queries 5

I've been out of the loop for a while. What's currently the best way to hash a password? I always used Whirlpool on my older gamemodes, but I'm not sure if that's still the best choice.
__________________

Gforcez is offline   Reply With Quote
Old 30/12/2018, 05:03 PM   #4089
Jeroen52
Gangsta
 
Jeroen52's Avatar
 
Join Date: Oct 2008
Location: The Netherlands
Posts: 882
Reputation: 74
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by Gforcez View Post
I've been out of the loop for a while. What's currently the best way to hash a password? I always used Whirlpool on my older gamemodes, but I'm not sure if that's still the best choice.
The best way to hash a password is to use a strong hashing algorithm like Whirlpool and use it the following method:
Hash=(Password+Salt)

Although it is just easier to use the bcrypt plugin as it also does hashing and iterations for you.
__________________
Jeroen52 is offline   Reply With Quote
Old 30/12/2018, 05:49 PM   #4090
Gforcez
Gangsta
 
Gforcez's Avatar
 
Join Date: Jul 2010
Location: The Netherlands.
Posts: 761
Reputation: 435
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by Jeroen52 View Post
The best way to hash a password is to use a strong hashing algorithm like Whirlpool and use it the following method:
Hash=(Password+Salt)

Although it is just easier to use the bcrypt plugin as it also does hashing and iterations for you.
I'm working with hashing, I'm just not sure if Whirlpool is still decent or if there's some new algorithm that's more secure or better. I'll check out bcrypt thanks!
__________________

Gforcez is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make a soldier aotumaticly be general when the general dies ? AlbanianGuy Scripting Help 35 13/05/2012 03:35 PM
Minor problem Desert Help Archive 4 17/03/2010 06:24 PM
Counting with mysql queries actiwe Help Archive 2 26/02/2010 02:36 PM
SQL queries using GROUP BY Dabombber Help Archive 1 05/10/2009 07:47 PM


All times are GMT. The time now is 12:24 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.