Raised This Month: $ Target: $400
 0% 

Reading from a file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Paladinrocker
Junior Member
Join Date: Jan 2007
Old 02-04-2007 , 17:49   Reading from a file
Reply With Quote #1

Hello, first off, I want to introduce myself from AMX, I'm Paladinrocker and I did AMX coding awhile back and made myself a few plugins. I'm returning now in AMXX, and I'm making some plugins for my clan's server in Zombie Panic, but unfortuantely I've forggoten a lot, and I've also searched the forums and didn't find anything, so here's my question. I basically want to look through a file full of steamids, and see if the person connecting matches any of the steamids in the list. And if at all possible, could you tell me how to write a steam id into the file as well, but in a skipped pattern line like:

steamid1
steamid2
steamid3

Thanks a lot.
Paladinrocker is offline
Drak
Veteran Member
Join Date: Jul 2005
Old 02-04-2007 , 18:10   Re: Reading from a file
Reply With Quote #2

I never used the new reading/writing system thingy, so I have no idea if this works.
Also, there's a tut here:
http://forums.alliedmods.net/showthread.php?t=46218
Edit: Wow, apparently what your asking for is already done in that thread.
Code:
#include <amxmodx> #include <amxmisc> new steamfile[256] public plugin_init() {     register_plugin("Read SteamID","1.0","")         //Set the configs dir     get_configsdir(steamfile,255)     format(steamfile,255,"%s/steamids.ini",steamfile)         // If the file doesn't exist, make one.     if(!file_exists(steamfile))     {         write_file(steamfile,";SteamID File");          }           } public client_putinserver(id) {      if(valid_steamid(id)) {         //If valid ID     } } public valid_steamid(id) {     new valid = 0     new file = fopen(steamfile,"r")     if(file) {         new read[130],steamid[33],authid[33]         while(fgets(file,read,129)) {             parse(read,steamid,32)             get_user_authid(id,authid,32)             if(equal(authid,steamid)) {                 valid = 1             }             else {                 valid = 0             }         }     }     return valid }

Last edited by Drak; 02-04-2007 at 18:13.
Drak is offline
Send a message via MSN to Drak
Paladinrocker
Junior Member
Join Date: Jan 2007
Old 02-04-2007 , 19:02   Re: Reading from a file
Reply With Quote #3

Thanks a lot, I'm looking into it.
Paladinrocker is offline
Paladinrocker
Junior Member
Join Date: Jan 2007
Old 02-04-2007 , 19:38   Re: Reading from a file
Reply With Quote #4

Ok I did it, and basically I just had it display a red message if the two steam ids were equal, and green if not, but they ended up all being red. Here's the code:
Code:
public special_steamid(id)
{
    new steamFile = fopen("specialsteamids.txt", "r");
    
    
    if(steamFile)
    {
        new data[128], authId[32], steamId[32];
        while(fgets(steamFile, data, 127))
        {
            parse(data, steamId, 31);
            
            get_user_authid(id, authId, 31);
            if(equal(authId, steamId))
            {
                fclose(steamFile);
                return 1;
            }
        }
    }
    fclose(steamFile);
    return 0;
}
EDIT: Oops, sorry for double-posting.
Paladinrocker is offline
Drak
Veteran Member
Join Date: Jul 2005
Old 02-04-2007 , 20:25   Re: Reading from a file
Reply With Quote #5

Quote:
Originally Posted by Paladinrocker View Post
Ok I did it, and basically I just had it display a red message if the two steam ids were equal, and green if not, but they ended up all being red. Here's the code:
Code:
public special_steamid(id)
{
    new steamFile = fopen("specialsteamids.txt", "r");
    
    
    if(steamFile)
    {
        new data[128], authId[32], steamId[32];
        while(fgets(steamFile, data, 127))
        {
            parse(data, steamId, 31);
            
            get_user_authid(id, authId, 31);
            if(equal(authId, steamId))
            {
                fclose(steamFile);
                return 1;
            }
        }
    }
    fclose(steamFile);
    return 0;
}
EDIT: Oops, sorry for double-posting.
I don't get it, all that code is doing, checking the SteamID. If correct, close the file.
Drak is offline
Send a message via MSN to Drak
Paladinrocker
Junior Member
Join Date: Jan 2007
Old 02-04-2007 , 20:40   Re: Reading from a file
Reply With Quote #6

I forgot another tidbit, this is the actual displaying part. It's when the player joins.

Code:
if(special_steamid(players[i]))
        {
            set_hudmessage(255, 0, 0, 0.0, 0.13, 0, 2.0, 12.0);
            show_hudmessage(players[i], "%s (%s) connected", playerName, authId);
        }
        else
        {
            set_hudmessage(0, 255, 0, 0.0, 0.13, 0, 2.0, 12.0);
            show_hudmessage(players[i], "%s (%s) connected", playerName, authId);
        }
Paladinrocker is offline
Drak
Veteran Member
Join Date: Jul 2005
Old 02-04-2007 , 21:27   Re: Reading from a file
Reply With Quote #7

Quote:
Originally Posted by Paladinrocker View Post
I forgot another tidbit, this is the actual displaying part. It's when the player joins.

Code:
if(special_steamid(players[i]))
        {
            set_hudmessage(255, 0, 0, 0.0, 0.13, 0, 2.0, 12.0);
            show_hudmessage(players[i], "%s (%s) connected", playerName, authId);
        }
        else
        {
            set_hudmessage(0, 255, 0, 0.0, 0.13, 0, 2.0, 12.0);
            show_hudmessage(players[i], "%s (%s) connected", playerName, authId);
        }
I'm not exactly sure, but on the other function (Special_Steamid). I tryed making it return 1 or 0 but always returned zero. So that's why i used the 'valid' integer to make it return 1 or 0.
Drak is offline
Send a message via MSN to Drak
facuq
Junior Member
Join Date: Feb 2007
Old 02-04-2007 , 21:33   Re: Reading from a file
Reply With Quote #8

¿shouldn't you read the file into an array or another data structure? opening, closing, and sweeping through a file everytime someone connects is not efficient, unless the file is cached in memory, but you really can't depend on the OS for that.
facuq is offline
Paladinrocker
Junior Member
Join Date: Jan 2007
Old 02-04-2007 , 21:42   Re: Reading from a file
Reply With Quote #9

By the way, where do I place that .txt file? Do I place it in the main mod directory, or do I place it elsewhere? I'm right in the middle of testing so I don't know if making an integer helped it.

EDIT: How would I do that facuq? Like I said, I'm still re-learning all this.
Paladinrocker is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 02-05-2007 , 05:17   Re: Reading from a file
Reply With Quote #10

Quote:
Originally Posted by Paladinrocker View Post
By the way, where do I place that .txt file? Do I place it in the main mod directory, or do I place it elsewhere? I'm right in the middle of testing so I don't know if making an integer helped it.
In amxx configsdir.
.ini, not .txt.
The plugin creates it for you.

Code:
#include <amxmodx> #include <amxmisc> new steamfile[128] public plugin_init() {     register_plugin("Read SteamID","1.0","")         get_configsdir(steamfile, 127)     new len = strlen(steamfile)         formatex(steamfile[len], 127-len, "/steamids.ini")         if ( ! file_exists(steamfile) ) {         write_file(steamfile,"");         } } public client_putinserver(id) {     if ( valid_steamid(id) )         set_hudmessage(255, 0, 0, 0.0, 0.13, 0, 2.0, 12.0);     else         set_hudmessage(0, 255, 0, 0.0, 0.13, 0, 2.0, 12.0);         new pName[32], pAuth[20]         get_user_authid(id, pAuth, 19)     get_user_name(id, pName, 31)         new players[32], pNum     get_players(players, pNum, "c")     for ( new i ; i < pNum ; i++ )         show_hudmessage(players[i], "%s (%s) connected", pName, pAuth); } public valid_steamid(id) {     new file = fopen(steamfile,"r")         if ( ! file )         return 0         new mAuth[20], pAuth[20]     get_user_authid(id, pAuth, 19) // We don't want to get this every loop.         if ( ! pAuth[0] )         return 0;         while ( ! feof(file) ) {                 fgets(file, mAuth, 19);         new len = strlen(pAuth);                 if ( equal(pAuth, mAuth, len) ) {             fclose(file)             return 1; // We don't want to keep reading if we found it.         }     }     fclose(file)     return 0; }
Code:
parse(read,steamid,32)
Aaah, my eyes are burning!

Last edited by [ --<-@ ] Black Rose; 02-05-2007 at 15:45.
[ --<-@ ] Black Rose is offline
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

Forum Jump


All times are GMT -4. The time now is 00:44.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode