SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 24/04/2015, 12:48 PM   #1
dusk
High-roller
 
dusk's Avatar
 
Join Date: Jul 2008
Posts: 1,113
Reputation: 46
Default Phones that are not associated with the player data

Hello, I thought of a phone system: player can have as many phones as he wants. Player can put the phone in his house, garage or in any vehicle. The thing is, even if he's not carrying it, the phone stays active(kind of).

Table design(maybe it will help to understand what am I trying to achieve)
Code:
CREATE TABLE IF NOT EXISTS phones (
	number INT NOT NULL,
	online TINYINT NOT NULL DEFAULT '1',
	added_on DATETIME NOT NULL,
	location_type TINYINT NOT NULL,
	location_id INT NOT NULL,
	PRIMARY KEY(number),
	INDEX(location_type),
	INDEX(location_id)
) ENGINE=INNODB DEAFULT CHARSET=cp1257 COLLATE=cp1257_bin;

The question

How to keep in memory? One large array for ALL phone data? Or at loading time I should load them into separate arrays for people, garages, vehicles and houses?

The first approach seems more elegant as I will be able to maintain all their data in one place.

The second obviously will be faster, no need to loop through the large array to find that phone.
dusk is offline   Reply With Quote
Old 24/04/2015, 01:18 PM   #2
spell
Banned
 
Join Date: Mar 2012
Posts: 263
Reputation: 21
Default Re: Phones that are not associated with the player data

If you Know hoy many phones will have the server, use arrays. If not, I think you should use gvars (dynamic memory)

Fist, a enum for phone data

pawn Code:
// MySQL loading stuff
new datacount = sizeof phonedata;
new Data[sizeof phonedata +1], vname[18];
format(vname, 18, "phone%d", number);
format(Data, sizeof Data, " %d,%d,%d", phonedat1, pdata2, pdata3);
SetGVarString(vname, Data);
 

// and getgvarstring for retrieve phone data
spell is offline   Reply With Quote
Old 24/04/2015, 01:48 PM   #3
dusk
High-roller
 
dusk's Avatar
 
Join Date: Jul 2008
Posts: 1,113
Reputation: 46
Default Re: Phones that are not associated with the player data

Well that's not quite the question. I already decided that I will use arrays(well perhaps y_malloc...)

The question is whether to use one array or multiple:

pawn Code:
enum e_phone_data
{
   number,
   location,
   locationid,
};
new PhoneData[ some constant ][ e_phone_data ];

// When loading I load it all to the same array

// And then:
GetPlayerPhone(playerid, index)
{
   new count = 0;
   for(new i = 0; i < sizeof(PhoneData); i++)
      if(PhoneData[ i ][ number ] && PhoneData[ i ][ location ] == player && PhoneData[ i ][ locationid ] == GetPlayerSqlId(playerid))
           if(count++ == index)
                  return i;
   return -1;
}

OR

pawn Code:
new PlayerPhones[ MAX_PLAYERS ][ max phones per player ];
new GaragePhones[ MAX_GARAGES ][ max_phones per garage ];
new VehiclePhones[ MAX_VEHICLES ][ max phones per vehicle ];

// When loading storing the date to its array

// And then for using something as simple as:
GetPlayerPhone(playerid, index)
{
    return PlayerPhones[ playerid ][ index ];
}
dusk is offline   Reply With Quote
Old 24/04/2015, 02:03 PM   #4
spell
Banned
 
Join Date: Mar 2012
Posts: 263
Reputation: 21
Default Re: Phones that are not associated with the player data

Its the same, the seconds will use more memory than the first, but will be faster and cleaner
spell 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
China phones pantelimonfl Everything and Nothing 25 03/12/2013 01:17 PM
Textdraws , phones, computers. AdamCooper Scripting Help 2 07/07/2013 04:44 AM
Pay phones viddo Scripting Help 3 20/02/2012 05:25 PM
IP Phones Help. Mrkrabz Everything and Nothing 0 30/07/2010 02:12 PM
Help - how to get data on the player in PHP X12X Help Archive 1 02/07/2009 02:03 PM


All times are GMT. The time now is 05:27 PM.


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