PDA

View Full Version : |Source| Sistema de Notícias Sem o Uso de Thread



Romário
10-29-2023, 02:39 PM
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

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

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++;
}
}
}

embreve
10-31-2023, 09:00 PM
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

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

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.

Romário
10-31-2023, 11:38 PM
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.