OkMuOnline


NowaHosting


AlphaServers


Resultados 1 a 3 de 3
  1. #1
    Developer C++ Romário's Avatar

    Data de Ingresso
    Jul 2022
    Posts
    41
    Thanks Thanks Given 
    21
    Thanks Thanks Received 
    238
    Thanked in
    19 Posts
    Mentioned
    1 Post(s)
    País
    Brazil

    Sistema de Notícias Sem o Uso de Thread

    Bom, recentemente eu decidi abandonar completamente o uso de Threads em meus projetos de Mu Online, devido a isso, tive que atualizar muitos códigos. Então vou compartilhar um código simples para implementar notícias no servidor.

    News.h
    Código:
    struct NEWS{    
    public:
        NEWS();
        virtual ~NEWS();
        void Load();
        void Proc();
    private:
        int Switch;
        int Session;
        int Index;
        int Line;
        long Interval;    
        char Messages[100][3][220];
        unsigned long TimerProc;
    };
    extern NEWS Noticias;
    News.cpp
    Código:
    NEWS Noticias;
    
    NEWS::NEWS() : Index(0), Line(0), TimerProc(0)
    {
        
    }
    
    NEWS::~NEWS()
    {
        for (int i = 0; i < this->Session; i++)
        {
            delete[] this->Messages[i];
        }
        delete[] this->Messages;
    }
    
    void NEWS::Load()
    {
        this->Switch = GetPrivateProfileInt("Configurações", "Ativar", 0, "..\\Data\\News.txt");
        this->Session = GetPrivateProfileInt("Configurações", "Sessões", 0, "..\\Data\\News.txt");
        this->Interval = GetPrivateProfileInt("Configurações", "Intervalo", 30, "..\\Data\\News.txt") * 60;
    
    
        for (int i = 0; i < this->Session; i++)
        {
            char secao[220];
            StringCbPrintf(secao, MAX_BUFFER_STRING, _T("Notícia %02d"), i + 1);
    
    
            for (int j = 0; j < 3; j++)
            {           
                char chave[220];
                StringCbPrintf(chave, MAX_BUFFER_STRING, _T("Linha %02d"), j + 1);
                GetPrivateProfileString(secao, chave, "Notícia Teste", this->Messages[i][j], 220, "..\\Data\\News.txt");
            }
        }
    }
    
    void NEWS::Proc()
    {
        if (this->Switch > 0 && this->Session > 0)
        {
            if (this->TimerProc >= this->Interval)
            {           
                if (strlen(this->Messages[this->Index][this->Line]) > 0)
                {
                     ServerAnnounce(this->Messages[this->Index][this->Line]);
                }
                if (strlen(this->Messages[this->Index][this->Line + 1]) > 0)
                {
                     ServerAnnounce(this->Messages[this->Index][this->Line + 1]);
                }
                if (strlen(this->Messages[this->Index][this->Line + 2]) > 0)
                {
                     ServerAnnounce(this->Messages[this->Index][this->Line + 2]);
                }
                this->Index = (this->Index + 1) % this->Session;
                this->TimerProc = 0;
            }
            else
            {
                this->TimerProc++;
            }
        }
    }

  2. The Following 2 Users Say Thank You to Romário For This Useful Post:


  3. #2
    Membro embreve's Avatar

    Data de Ingresso
    Mar 2023
    Posts
    54
    Thanks Thanks Given 
    85
    Thanks Thanks Received 
    2
    Thanked in
    2 Posts
    Mentioned
    1 Post(s)
    País
    Brazil
    Citação Originally Posted by Romário Ver Post
    Bom, recentemente eu decidi abandonar completamente o uso de Threads em meus projetos de Mu Online, devido a isso, tive que atualizar muitos códigos. Então vou compartilhar um código simples para implementar notícias no servidor.

    News.h
    Código:
    struct NEWS{    
    public:
        NEWS();
        virtual ~NEWS();
        void Load();
        void Proc();
    private:
        int Switch;
        int Session;
        int Index;
        int Line;
        long Interval;    
        char Messages[100][3][220];
        unsigned long TimerProc;
    };
    extern NEWS Noticias;
    News.cpp
    Código:
    NEWS Noticias;
    
    NEWS::NEWS() : Index(0), Line(0), TimerProc(0)
    {
        
    }
    
    NEWS::~NEWS()
    {
        for (int i = 0; i < this->Session; i++)
        {
            delete[] this->Messages[i];
        }
        delete[] this->Messages;
    }
    
    void NEWS::Load()
    {
        this->Switch = GetPrivateProfileInt("Configurações", "Ativar", 0, "..\\Data\\News.txt");
        this->Session = GetPrivateProfileInt("Configurações", "Sessões", 0, "..\\Data\\News.txt");
        this->Interval = GetPrivateProfileInt("Configurações", "Intervalo", 30, "..\\Data\\News.txt") * 60;
    
    
        for (int i = 0; i < this->Session; i++)
        {
            char secao[220];
            StringCbPrintf(secao, MAX_BUFFER_STRING, _T("Notícia %02d"), i + 1);
    
    
            for (int j = 0; j < 3; j++)
            {           
                char chave[220];
                StringCbPrintf(chave, MAX_BUFFER_STRING, _T("Linha %02d"), j + 1);
                GetPrivateProfileString(secao, chave, "Notícia Teste", this->Messages[i][j], 220, "..\\Data\\News.txt");
            }
        }
    }
    
    void NEWS::Proc()
    {
        if (this->Switch > 0 && this->Session > 0)
        {
            if (this->TimerProc >= this->Interval)
            {           
                if (strlen(this->Messages[this->Index][this->Line]) > 0)
                {
                     ServerAnnounce(this->Messages[this->Index][this->Line]);
                }
                if (strlen(this->Messages[this->Index][this->Line + 1]) > 0)
                {
                     ServerAnnounce(this->Messages[this->Index][this->Line + 1]);
                }
                if (strlen(this->Messages[this->Index][this->Line + 2]) > 0)
                {
                     ServerAnnounce(this->Messages[this->Index][this->Line + 2]);
                }
                this->Index = (this->Index + 1) % this->Session;
                this->TimerProc = 0;
            }
            else
            {
                this->TimerProc++;
            }
        }
    }
    Nossa! Já abandonei o uso de Thread a bastente tempo em meus projetos.
    Interessante que o código acima é bem parecido com os sistemas de notícias de alguns projetos liberado na internet.
    Obrigado pela contribuição.

  4. #3
    Developer C++ Romário's Avatar

    Data de Ingresso
    Jul 2022
    Posts
    41
    Thanks Thanks Given 
    21
    Thanks Thanks Received 
    238
    Thanked in
    19 Posts
    Mentioned
    1 Post(s)
    País
    Brazil
    Citação Originally Posted by embreve Ver Post
    Nossa! Já abandonei o uso de Thread a bastente tempo em meus projetos.
    Interessante que o código acima é bem parecido com os sistemas de notícias de alguns projetos liberado na internet.
    Obrigado pela contribuição.
    Claro que há algumas coisas parecidas, afinal é a mesma finalidade, o que muda é a abordagem. Esse código está de acordo com todas as boas práticas de programação, diferente dos que existem na internet que ainda usam funções como sprint ou sprint_s.

Tags para este Tópico

Permissões de Postagem

  • Você não pode iniciar novos tópicos
  • You may not post Resposta(s)
  • Você não pode enviar anexos
  • Você não pode editar suas mensagens
  •