AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Reading from a file (https://forums.alliedmods.net/showthread.php?t=50831)

Paladinrocker 02-04-2007 17:49

Reading from a file
 
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.

Drak 02-04-2007 18:10

Re: Reading from a file
 
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 }

Paladinrocker 02-04-2007 19:02

Re: Reading from a file
 
Thanks a lot, I'm looking into it.

Paladinrocker 02-04-2007 19:38

Re: Reading from a file
 
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.

Drak 02-04-2007 20:25

Re: Reading from a file
 
Quote:

Originally Posted by Paladinrocker (Post 435444)
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.

Paladinrocker 02-04-2007 20:40

Re: Reading from a file
 
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);
        }


Drak 02-04-2007 21:27

Re: Reading from a file
 
Quote:

Originally Posted by Paladinrocker (Post 435452)
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.

facuq 02-04-2007 21:33

Re: Reading from a file
 
¿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.

Paladinrocker 02-04-2007 21:42

Re: Reading from a file
 
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.

[ --<-@ ] Black Rose 02-05-2007 05:17

Re: Reading from a file
 
Quote:

Originally Posted by Paladinrocker (Post 435468)
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!


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

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