Phones that are not associated with the player data
#1

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.
Reply
#2

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
Reply
#3

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 ];
}
Reply
#4

Its the same, the seconds will use more memory than the first, but will be faster and cleaner
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)