SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Scripting Help (https://forum.sa-mp.com/forumdisplay.php?f=12)
-   -   Fetch offline and online players (https://forum.sa-mp.com/showthread.php?t=670899)

ImTobi 29/11/2019 09:58 AM

Fetch offline and online players
 
Hello,
i want to make a /fmember list, so i can view every faction member, but i don't lnow ho wto fetch offline players that are in the faction.
i'm using dini2

Mugala 29/11/2019 11:00 AM

Re: Fetch offline and online players
 
For first, stop using dini2, better to move on improved database management systems, such as MySQL...
OT: You have to check all of the user files and get a current faction ID (if it's exists at all)
or, with a faster way (for processing), you can create a new file, where all of the users in the current faction will be stored.

ImTobi 29/11/2019 11:03 AM

Re: Fetch offline and online players
 
Quote:

Originally Posted by Mugala (Post 4123520)
For first, stop using dini2, better to move on improved database management systems, such as MySQL...
OT: You have to check all of the user files and get a current faction ID (if it's exists at all)
or, with a faster way (for processing), you can create a new file, where all of the users in the current faction will be stored.

i am using mysql in my other script, but for me it's easier to use dini, and dini 2 is way faster than dini, so it's ok. I will try that, and respond if i have problems.

I just don't know how to get all files.

ImTobi 03/12/2019 12:27 PM

Re: Fetch offline and online players
 
*bump*

Mugala 03/12/2019 01:07 PM

Re: Fetch offline and online players
 
you can't read all of the files in a folder, simply gonna need a plugin - https://forum.sa-mp.com/showthread.php?t=92246

Kwarde 03/12/2019 01:11 PM

Re: Fetch offline and online players
 
How to get all files: You could use FileManager to fetch all dirs/files from a directory: https://forum.sa-mp.com/showthread.php?t=92246

However I also recommend you to use MySQL (or SQLite). Looping through /reading (evenually writing) all files could create serious laggs (remember that SA-MP is single-threaded; If you would loop/read all files, nobody would be synced untill this action is finished! They can't chat or use commands etc...).

With MySQL you could do it like this. Le's assume we have three tables:
Code:

Table `fations`:
[ID] [Name]    [Owner]
1    MyFaction  Kwarde
1    MyFaction2 Kwarde

Table `faction_members`:
[ID] [Faction_ID] [Member]
1    1              1
2    1              5
3    1              3           
4    2              1

Table `users`:
[ID] [Name]
1    Kwarde
2    Merel
3    Petah
4    x_fjskd29
5    John_Doe

To get all user names from faction 1 and the name of faction ID 1 you could use:

Code:

SELECT a1.`Name` AS 'Faction_Name', a2.`Name` AS 'User_Name' FROM `factions` a1 JOIN `users` a2 WHERE a1.`ID` = 1 AND a2.`ID` IN (SELECT `Member` FROM `faction_members` WHERE `Faction_ID` = 1)
This query would return:
Code:

[Faction_Name] [User_Name]
MyFaction      Kwarde
MyFaction      Petah
MyFaction      John_Doe

Assuming you're using a threaded query you won't have laggs because the MySQL server will do the work (which is way faster than reading file-by-file). You should really consider using MySQL or SQLite for this.

I've nothing against using files but for functions like this you definitely don't want to use files.

To work with above query ingame (to fetch results):
Code:

//In the callback that's called after using _tquery()
new num_rows;
cache_get_row_count(num_rows);
if (!num_rows)
    return; //There are no results, end function
for (new i; i < num_rows; i++)
{
    new Faction_Name[30], User_Name[25];
    cache_get_value_name(i, "Faction_Name", Faction_Name);
    cache_get_value_name(i, "User_Name", User_Name);

    //You can now use variables 'Faction_Name' and 'User_Name' to show data to a player
}


Symon 03/12/2019 01:30 PM

Re: Fetch offline and online players
 
Quote:

Originally Posted by Kwarde (Post 4123987)
How to get all files: You could use FileManager to fetch all dirs/files from a directory: https://forum.sa-mp.com/showthread.php?t=92246

Why your name isn't coloured in blue yet? You have damn a lot of patience.

@OP: Next time switch to MySQL.

Calisthenics 03/12/2019 02:08 PM

Re: Fetch offline and online players
 
If you decide to use MySQL and use the table structures Kwarde suggested (which is the correct way to have members in a separate table), I would like to note two things.

1) `Owner` can be foreign key for target users(ID) instead of a VARCHAR column.
2) A simple JOIN is enough to get the members of a certain faction ID:
pawn Code:
SELECT Name AS User_Name
  FROM faction_members
  JOIN users u
    ON u.ID=`Member`
 WHERE faction_id=1;
Member is reserved keyword, this is why it is wrapped around in backticks.

Joe Staff 03/12/2019 03:03 PM

Re: Fetch offline and online players
 
Quote:

Originally Posted by Kwarde (Post 4123987)
With MySQL you could do it like this. Le's assume we have three tables:
Code:

Table `fations`:
[ID] [Name]    [Owner]
1    MyFaction  Kwarde
1    MyFaction2 Kwarde

Table `faction_members`:
[ID] [Faction_ID] [Member]
1    1              1
2    1              5
3    1              3           
4    2              1

Table `users`:
[ID] [Name]
1    Kwarde
2    Merel
3    Petah
4    x_fjskd29
5    John_Doe


If you don't mind me asking, why would he have 3 tables (factions, member/faction association, members) instead of just 2 tables (factions, members including faction Id)?

Kwarde 03/12/2019 04:35 PM

Re: Fetch offline and online players
 
@Calisthenics: Never been into foreign keys 'cuz when dropping all tables in a DB I'd have to uncheck 'check for foreign keys'. And I always forget that so I'd have to remove tables twice. (Lazyness level 1000...).
What's the benefit of using foreign?
@Joe staff: I don't mind you asking :P
Users table in this exampke would be the table containing the users on the server (so not persay directly connected to the faction system).
Then what remains is indeed the factions table and the table containing faction members.


All times are GMT. The time now is 02:42 PM.

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