Raised This Month: $51 Target: $400
 12% 

Map Tiers Length problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Elit59
Member
Join Date: Feb 2016
Old 05-05-2023 , 22:59   Map Tiers Length problem
Reply With Quote #1

Hello. I would like to add Length. but for some reason it's not good. What could be the problem?

Code:
#include <amxmodx>
#include <amxmisc>
#include <regex>

new const g_szPluginName[] = "Map Tiers";
new const g_szPluginVersion[] = "v1.0.0";
new const g_szPluginAuthor[] = "Cram";

new const g_szINIFileName[] = "maptiers.ini";

new g_szCurrentMap[32];
new g_szTier[16];
new g_szLength[16];

public plugin_init()
{   
    register_plugin(g_szPluginName, g_szPluginVersion, g_szPluginAuthor);
    
    register_clcmd("say /maptier", "cmdShowCurrentMapTier");
    register_clcmd("say /tier", "cmdShowCurrentMapTier");
    
    get_mapname(g_szCurrentMap, charsmax(g_szCurrentMap));

    getCurrentMapTier();
}

public cmdShowCurrentMapTier(id)
{   
    client_print(id, print_chat, "[KZ TIERS] Current: %s, Tier: %s, Length: %s", g_szCurrentMap, g_szTier, g_szLength);
    return PLUGIN_CONTINUE;
}

stock getCurrentMapTier()
{
    new szINIFilePath[256];
    get_configsdir(szINIFilePath, charsmax(szINIFilePath));
    format(szINIFilePath, charsmax(szINIFilePath),"%s/%s", szINIFilePath, g_szINIFileName);
    
    // Make sure each line in the INI is written properly "<anything><whitespace><anything>"
    // Otherwise, the line will be ignored and will be logged
    new const szPattern[] = "(.+)(\w)(.+)$";

    new pFile = fopen(szINIFilePath, "r");
    new szFileLine[64];
    new iFaultyLines = 0;
    
    copy(g_szTier, charsmax(g_szTier), "Not Specified");
    copy(g_szLength, charsmax(g_szLength), "Not Specified");
    
    while(fgets(pFile, szFileLine, charsmax(szFileLine)))
    {   
        if(regex_match(szFileLine, szPattern))
        {   
            new szLeft[32], szRight[16];
            split(szFileLine, szLeft, charsmax(szLeft), szRight, charsmax(szRight), " ");
            
            if(equal(szLeft, g_szCurrentMap))
            {   
                copy(g_szTier, charsmax(g_szTier), szRight);
                copy(g_szLength, charsmax(g_szLength), g_szLength);
                break;
            }
        }
        else
            iFaultyLines++;
    }
    fclose(pFile);
    
    if(iFaultyLines > 0)
    {   
        // Log all of the imporperly formatted lines in your INI file to fix rather than cause an error
        new szLog[256];
        format(szLog, charsmax(szLog), "[MAP TIERS] In: %s, number of improperly formatted lines: (%d)", g_szINIFileName, iFaultyLines);
        
        server_print(szLog);
        log_amx(szLog);
    }
}
Code:
maptiers.ini
hb_dropzone Easy Short
Attached Thumbnails
Click image for larger version

Name:	20230418101822_1.jpg
Views:	47
Size:	73.8 KB
ID:	200515  
Elit59 is offline
WATCH_D0GS UNITED
Senior Member
Join Date: Jan 2023
Old 05-06-2023 , 00:09   Re: Map Tiers Length problem
Reply With Quote #2

Are you trying to return text (string) or numbers about the Lenght?

In case of numbers, replace %s by %d

We could not load your plugin so we cannot check it.
__________________
💻Know Our New Blog👄
🔗tube2downs.blogspot.com
WATCH_D0GS UNITED is offline
Elit59
Member
Join Date: Feb 2016
Old 05-06-2023 , 00:56   Re: Map Tiers Length problem
Reply With Quote #3

Quote:
Originally Posted by WATCH_D0GS UNITED View Post
Are you trying to return text (string) or numbers about the Lenght?

In case of numbers, replace %s by %d

We could not load your plugin so we cannot check it.
sorry, I sent the wrong code. the text is not good for some reason. picture attached

Code:
#include <amxmodx>
#include <amxmisc>
#include <regex>

new const g_szPluginName[] = "Map Tiers";
new const g_szPluginVersion[] = "v1.0.0";
new const g_szPluginAuthor[] = "Cram";

new const g_szINIFileName[] = "maptiers.ini";

new g_szCurrentMap[32];
new g_szTier[16];
new g_szLength[16];

public plugin_init() 
{    
    register_plugin(g_szPluginName, g_szPluginVersion, g_szPluginAuthor);
    
    register_clcmd("say /maptier", "cmdShowCurrentMapTier");
    register_clcmd("say /tier", "cmdShowCurrentMapTier");
    
    get_mapname(g_szCurrentMap, charsmax(g_szCurrentMap));

    getCurrentMapTier();

    register_dictionary("kz_tier.txt");
}

public cmdShowCurrentMapTier(id) 
{    
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "KZ_TIER_CHAT", g_szCurrentMap, g_szTier, g_szLength);
    return PLUGIN_CONTINUE;
}

stock getCurrentMapTier() 
{
    new szINIFilePath[256];
    get_configsdir(szINIFilePath, charsmax(szINIFilePath));
    format(szINIFilePath, charsmax(szINIFilePath),"%s/%s", szINIFilePath, g_szINIFileName);
    
    // Make sure each line in the INI is written properly "<anything><whitespace><anything>"
    // Otherwise, the line will be ignored and will be logged
    new const szPattern[] = "(.+)(\w)(.+)$";

    new pFile = fopen(szINIFilePath, "r");
    new szFileLine[64];
    new iFaultyLines = 0;

    format(g_szTier, charsmax(g_szTier), "%L", LANG_PLAYER, "KZ_TIER");
    format(g_szLength, charsmax(g_szLength), "%L", LANG_PLAYER, "KZ_LENGTH");
    
    while(fgets(pFile, szFileLine, charsmax(szFileLine))) 
    {    
        if(regex_match(szFileLine, szPattern)) 
        {    
            new szLeft[32], szRight[16];
            split(szFileLine, szLeft, charsmax(szLeft), szRight, charsmax(szRight), " ");
            
            if(equal(szLeft, g_szCurrentMap)) 
            {    
                copy(g_szTier, charsmax(g_szTier), szRight);
                copy(g_szLength, charsmax(g_szLength), g_szLength);
                break;
            }
        }
        else
            iFaultyLines++;
    }
    fclose(pFile);
    
    if(iFaultyLines > 0) 
    {    
        // Log all of the imporperly formatted lines in your INI file to fix rather than cause an error
        new szLog[256];
        format(szLog, charsmax(szLog), "[MAP TIERS] In: %s, number of improperly formatted lines: (%d)", g_szINIFileName, iFaultyLines);
        
        server_print(szLog);
        log_amx(szLog);
    }
}
Code:
[en]
KZ_TIER_CHAT = ^4[KZ TIERS] ^1Current: ^3%s^1, Tier: ^3%s^1, Length: ^3%s^1.
KZ_TIER = Not Specified
KZ_LENGTH = Not Specified
Code:
hb_dropzone Easy Short

https://forums.alliedmods.net/showth...highlight=tier
Attached Thumbnails
Click image for larger version

Name:	20230506064958_1.jpg
Views:	28
Size:	81.0 KB
ID:	200516  

Last edited by Elit59; 05-06-2023 at 00:58.
Elit59 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-06-2023 , 21:13   Re: Map Tiers Length problem
Reply With Quote #4

"\w" in Regex is not for white space, it is for word characters (equivalent to "[a-z_]"). I would recommend that your INI file be the more traditional key-value pairs using "=" (like you see in the lang files). Then, you can get rid of regex entirely and just split based on "=", then trim each side and validate that the right side is not an empty string before using it. This will also be much more efficient.

If you do use regex for some reason, you don't need any of the parentheses if you're not actually using submatches or regex patterns that actually require them (which you aren't using).
__________________
fysiks is online now
Elit59
Member
Join Date: Feb 2016
Old 05-07-2023 , 00:55   Re: Map Tiers Length problem
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
"\w" in Regex is not for white space, it is for word characters (equivalent to "[a-z_]"). I would recommend that your INI file be the more traditional key-value pairs using "=" (like you see in the lang files). Then, you can get rid of regex entirely and just split based on "=", then trim each side and validate that the right side is not an empty string before using it. This will also be much more efficient.

If you do use regex for some reason, you don't need any of the parentheses if you're not actually using submatches or regex patterns that actually require them (which you aren't using).
would you write an example?
Elit59 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-08-2023 , 23:11   Re: Map Tiers Length problem
Reply With Quote #6

The simplest way is to do the following:
  • Remove the regex check
  • Change the space character in the split function to an equal sign
  • Use trim() on both left and right parts before the map name check
__________________

Last edited by fysiks; 05-08-2023 at 23:12.
fysiks is online now
Reply



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 20:12.


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