AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Need Opinion (Optimizing / File or Array) (https://forums.alliedmods.net/showthread.php?t=84774)

Drak 01-31-2009 18:14

Need Opinion (Optimizing / File or Array)
 
In a file, I have the line:
Code:

hitman *p STEAM_ID_SOMETHING a b c d
This restricts the model "hitman" to "steam_id_something" and access flags "a b c d". I have this function to check it.
Code:
bool:CheckAccess(id,const Model[]) {     if(!is_user_connected(id))         return false         new pFile = fopen(g_ConfigDir,"r"),bool:FileFound = false,bool:HasAccess = false     if(!pFile)         return false         static Data[128],fModel[36],Access[36]     while(!feof(pFile))     {         fgets(pFile,Data,127);         strbreak(Data,fModel,35,Access,35);                 if(containi(Model,fModel) == -1)             continue                 if(containi(Access,"*P") != -1)             strbreak(Access,Data,1,Access,35);                 FileFound = true                 if(!Access[0])             break                 strbreak(Access,Access,35,Data,127);         if(containi(Access,"STEAM_") != -1)         {             new AuthID[36]             get_user_authid(id,AuthID,35);                         if(equali(Access,Data))             {                 HasAccess = true                 break             }         }         if(Data[0])         {             new Len = strlen(Data) / 2,iAccess             server_print("LEN: %d",Len);             for(new Count;Count < Len;Count++)             {                 strbreak(Data,Access,35,Data,127);                 iAccess = GetStringAccess(Access);                 if(iAccess & GetUserAccess(id))                 {                     HasAccess = true                     break                 }             }             if(HasAccess)                 break         }     }     fclose(pFile);         if(FileFound)         return HasAccess         return true }

But I'm super annoyed when I think I'm doing something wrong. The above function works. But I think it can be better, is there anything / other alternatives i can do, to accomplish this?

Also, since i'm using a file. Should I use ether a vault system, or dynamic arrays?

stupok 01-31-2009 20:47

Re: Need Opinion (Optimizing / File or Array)
 
I think this should do it:

(I haven't tested and didn't check for typos.)

You should write the file like this if you want to use my function:
Code:

hitman *p STEAM_ID_SOMETHING abcd
Code:
public CheckAccess(id, const model) {     if(!is_user_connected) return 0         new fh = fopen(FILE_NAME, "rt")         if(!fh) return 0         static szBuffer[256], szModel[32], szSomething[8], szAuth[32], szFlags[32]         static szPlayerAuth[32]     get_user_authid(id, szPlayerAuth, charsmax(szPlayerAuth))         while(!feof(fh))     {         fgets(fh, szBuffer, charsmax(szBuffer))         parse(szBuffer, szModel, charsmax(szModel), szSomething, charsmax(szSomething), szAuth, charsmax(szAuth), szFlags, charsmax(szFlags))                 if(equali(model, szModel) && (equali(szAuth, szPlayerAuth) || get_user_flags(id) & read_flags(szFlags)))         {             fclose(fh)             return 1         }     }         fclose(fh)     return 0 }

Drak 01-31-2009 22:37

Re: Need Opinion (Optimizing / File or Array)
 
Well one thing is. I'm not using AMXX flags. It's a custom access thing. So read_flags could not be used here.

BOYSplayCS 01-31-2009 23:16

Re: Need Opinion (Optimizing / File or Array)
 
To answer your last question Drak, use a vault system.

danielkza 01-31-2009 23:57

Re: Need Opinion (Optimizing / File or Array)
 
Quote:

Originally Posted by Drak (Post 753550)
Well one thing is. I'm not using AMXX flags. It's a custom access thing. So read_flags could not be used here.

read_flags doesn't care about AMXX, it does what i says, converts the string into a bitflag. You can check it with your own values, use your own flags as you wish.

And I think the best way would be to read the file into a cellarray on mapload/command, and check it when needed.


All times are GMT -4. The time now is 01:43.

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