Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

UGrade Manager

Lib

Experienced Elementalist
Joined
Oct 22, 2013
Messages
241
Reaction score
80
Dear ragezone,

This code manage, flashing usergrade color's and normal usergrade color's. The colors are readed from a xml file called 'UserGrade.xml'.


CPP File
Code:
#pragma once


#include "stdafx.h"
#include <queue>


enum ColorMode
{
    COLOR_NORMAL,
    COLOR_FLASH,
};


struct UserGrade
{
    ColorMode mode;
    int timeDelta;
    std::queue<unsigned long> colors;


    DWORD    time;
    int    count = 0;
};


class ZUserGradeManager : public map <int, UserGrade>
{
public:
    ZUserGradeManager::ZUserGradeManager() 
    {
    }


    ZUserGradeManager::~ZUserGradeManager()
    {
        clear();
    }


    static ZUserGradeManager* ZUserGradeManager::GetInstance()
    {
        static ZUserGradeManager getme;
        return &getme;
    }


    unsigned long ZUserGradeManager::GetColor(int c)
    {
        auto it = find(c);


        if (it == end())
            return 0xFFFFFFFF;
        else
        {
            DWORD time = timeGetTime();
            if (it->second.mode == COLOR_FLASH)
            {
                if (time - it->second.time > it->second.timeDelta)
                {
                    for (auto it = begin(); it != end(); it++)
                    {
                        unsigned long swapColor = it->second.colors.front();
                        it->second.colors.pop();
                        it->second.colors.push(swapColor);
                    }
                    it->second.time = time;
                }
            }
            return it->second.colors.front();
        }
    }


    bool ZUserGradeManager::ReadXml(MZFileSystem* pFileSystem, const char* szFileName)
    {
        MXmlDocument    xmlIniData;
        xmlIniData.Create();


        char *buffer;
        MZFile mzf;


        if (pFileSystem)
        {
            if (!mzf.Open(szFileName, pFileSystem))
            {
                if (!mzf.Open(szFileName))
                {
                    xmlIniData.Destroy();
                    return false;
                }
            }
        }
        else
        {
            if (!mzf.Open(szFileName))
            {
                xmlIniData.Destroy();
                return false;
            }
        }


        buffer = new char[mzf.GetLength() + 1];
        buffer[mzf.GetLength()] = 0;
        memset(buffer, 0, mzf.GetLength() + 1);


        mzf.Read(buffer, mzf.GetLength());


        if (!xmlIniData.LoadFromMemory(buffer))
        {
            xmlIniData.Destroy();
            return false;
        }


        delete[] buffer;
        mzf.Close();


        MXmlElement rootElement, chrElement, attrElement;
        char szTagName[256];


        rootElement = xmlIniData.GetDocumentElement();
        int iCount = rootElement.GetChildNodeCount();


        for (int i = 0; i < iCount; i++)
        {
            chrElement = rootElement.GetChildNode(i);
            chrElement.GetTagName(szTagName);
            if (szTagName[0] == '#') continue;


            if (!stricmp(szTagName, "USERGRADE"))
            {
                if (!ParseGrade(chrElement))
                    return false;
            }
        }


        xmlIniData.Destroy();
        return true;
    }


private:
    bool ZUserGradeManager::ParseGrade(MXmlElement& element)
    {
        int id = 0;
        int num = 0;
        char szAttrValue[256];
        char szAttrName[64];
        UserGrade pUserGrade;


        for (int i = 0; i < (int)element.GetAttributeCount(); i++)
        {
            memset(szAttrValue, 0, 256);
            memset(szAttrName, 0, 64);


            element.GetAttribute(i, szAttrName, szAttrValue);d
            if (!stricmp(szAttrName, "id"))
                id = atoi(szAttrValue);
            else if (!stricmp(szAttrName, "flash"))
                pUserGrade.mode = (ColorMode)atoi(szAttrValue);
            else if (sscanf(szAttrName, "color%i", &num))
                pUserGrade.colors.push(strtoul(szAttrValue, NULL, 0));
            else if (!stricmp(szAttrName, "timedelta"))
                pUserGrade.timeDelta = atoi(szAttrValue);
        }
        pUserGrade.time = timeGetTime();


        insert(std::pair<int, UserGrade>(id, pUserGrade));


        return true;
    }
};
inline ZUserGradeManager* ZGetUserGradeMgr() { return ZUserGradeManager::GetInstance(); }


XML Design.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <USERGRADE id="255" flash="1" color0="0xFF2700FF" color1="0xFFFFFFFF" color2="0xFFFF80F6" timedelta="500"/>
    <USERGRADE id="254" flash="0" color0="0xFF2700FF"/>
    <USERGRADE id="0" flash="0" color0="0xFFFFFFFF"/>
</xml>

Usage:
id = UGradeID
color<num> = 0xFF<hex color> *
timedelta = time in milliseconds to flash to next color

*
You can add unlimited flashing colors just count color0 - color1 - color2 etc

NOTE: This source isn't correct, see grandao's his post. No support will be given.

 
Last edited:
Junior Spellweaver
Joined
Feb 4, 2008
Messages
122
Reaction score
148
Some tips

Use xml structure (numbering each color is error prone)
Code:
<USERGRADE id="255" flash="1" timedelta="500">
    <COLOR>0xFF2700FF</COLOR>
    <COLOR>0xFFFFFFFF</COLOR>
    <COLOR>0xFFFF80F6</COLOR>
</USERGRADE>


I don't think std::queue is necessary here (besides being fancy)
Code:
struct UserGrade
{
    ColorMode mode;
    int timeDelta;
    [B]std::vector<unsigned long> colors;[/B]
    // notice that assign value in struct definition requires c++11
    // otherwise you should use contructor:
    // UserGrade(): currentColor(0), count(0) {}
    [B]int currentColor = 0;[/B]

    DWORD    time;
    int    count = 0;
};

    unsigned long ZUserGradeManager::GetColor(int c)
    {
        auto it = find(c);


        if (it == end())
            return 0xFFFFFFFF;
        else
        {
            DWORD time = timeGetTime();
            if (it->second.mode == COLOR_FLASH)
            {
                if (time - it->second.time > it->second.timeDelta)
                {
                    it->second.time = time;
                    return it->second.colors[ ++it->second.currentColor % it->second.colors.size() ];
                }
            }
        }
    }

Besides these little details keep with your releases
 
I'm retired, I'm already
Banned
Joined
Oct 3, 2011
Messages
832
Reaction score
155
I still do not understand how it works, if it shows an image may what to understand.
 
Newbie Spellweaver
Joined
Jul 24, 2011
Messages
35
Reaction score
1
Lib can you explain in deep and post a screenshot? And you have been posting your best works till now. Thanks buddy. ^_^
 
Newbie Spellweaver
Joined
Apr 29, 2015
Messages
75
Reaction score
6
help please ?

ZUserGradeManager.cpp(23): error C2864: 'count' : only const static integral data members can be initialized inside a class or struct
 
Back
Top