SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 29/11/2019, 09:58 AM   #1
ImTobi
Big Clucker
 
ImTobi's Avatar
 
Join Date: Jun 2017
Posts: 144
Reputation: 3
Default 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
ImTobi is offline   Reply With Quote
Old 29/11/2019, 11:00 AM   #2
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,162
Reputation: 73
Default 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.
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Old 29/11/2019, 11:03 AM   #3
ImTobi
Big Clucker
 
ImTobi's Avatar
 
Join Date: Jun 2017
Posts: 144
Reputation: 3
Default Re: Fetch offline and online players

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.
ImTobi is offline   Reply With Quote
Old 03/12/2019, 12:27 PM   #4
ImTobi
Big Clucker
 
ImTobi's Avatar
 
Join Date: Jun 2017
Posts: 144
Reputation: 3
Default Re: Fetch offline and online players

*bump*
ImTobi is offline   Reply With Quote
Old 03/12/2019, 01:07 PM   #5
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,162
Reputation: 73
Default 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
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Old 03/12/2019, 01:11 PM   #6
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,634
Reputation: 1653
Default 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
}
__________________
Do not click on this URL -It is secret information
RIP Slayer 1981-2019 \m/
Discord: Kwarde#1991

Last edited by Kwarde; 25/12/2019 at 11:08 AM.
Kwarde is online now   Reply With Quote
Old 03/12/2019, 01:30 PM   #7
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 862
Reputation: 69
Default Re: Fetch offline and online players

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://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.
__________________

Join The Server (play.symonscnr.com) | Discord | Website/Forum
Choose up from 5 different skills, Con Artist, Drug Dealer, Firefighter, Hitman and Terrorist.
Play as a Civilian or a LEO, bomb buildings/vehicles, rob shops or bank and many many more. Join us!
Symon is offline   Reply With Quote
Old 03/12/2019, 02:08 PM   #8
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,229
Reputation: 205
Default 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.
Calisthenics is offline   Reply With Quote
Old 03/12/2019, 03:03 PM   #9
Joe Staff
High-roller
 
Join Date: Aug 2007
Posts: 2,593
Reputation: 379
Default Re: Fetch offline and online players

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)?
Joe Staff is offline   Reply With Quote
Old 03/12/2019, 04:35 PM   #10
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,634
Reputation: 1653
Default 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.
__________________
Do not click on this URL -It is secret information
RIP Slayer 1981-2019 \m/
Discord: Kwarde#1991
Kwarde is online now   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
[Tutorial] Fetch offline player's data (MySQL R41-4) StevenLV Tutorials 6 01/10/2019 05:42 PM
ID 8 is offline but is actually online. WhateverRP Scripting Help 0 11/02/2017 04:22 PM
[PHP] Help Online and Offline Tween73 Scripting Help 4 14/03/2016 02:13 PM
Offline and Online Members ShoortyFl Scripting Help 20 26/06/2015 05:51 PM
Looping trough online AND OFFLINE players Kyance Scripting Help 4 17/03/2014 01:48 PM


All times are GMT. The time now is 07:23 AM.


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