Fetch offline and online players
#1

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

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

Quote:
Originally Posted by Mugala
View Post
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.
Reply
#4

*bump*
Reply
#5

you can't read all of the files in a folder, simply gonna need a plugin - https://sampforum.blast.hk/showthread.php?tid=92246
Reply
#6

How to get all files: You could use FileManager to fetch all dirs/files from a directory: https://sampforum.blast.hk/showthread.php?tid=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
}
Reply
#7

Quote:
Originally Posted by Kwarde
View Post
How to get all files: You could use FileManager to fetch all dirs/files from a directory: https://sampforum.blast.hk/showthread.php?tid=92246
Why your name isn't coloured in blue yet? You have damn a lot of patience.

@OP: Next time switch to MySQL.
Reply
#8

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

Quote:
Originally Posted by Kwarde
View Post
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)?
Reply
#10

@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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)