Código original adicionado
This commit is contained in:
parent
ab148fb510
commit
a6c372c9a9
10 changed files with 1317 additions and 0 deletions
125
util.js
Normal file
125
util.js
Normal file
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
Força: um (livre) clone do jogo Forca
|
||||
Copyright (C) 2020 luca0N!
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Entre em contato comigo por e-mail via <luca0n@luca0n.com>.
|
||||
*/
|
||||
|
||||
class Util {
|
||||
/**
|
||||
* Alerta todos os clientes de uma sala sobre um evento.
|
||||
* @param string O ID do evento.
|
||||
* @param string salaNome A sala desejada.
|
||||
* @param object extra Os dados extra do evento.
|
||||
* @since 12 de outubro de 2020, 13:32 (UTC -03:00).
|
||||
*/
|
||||
static alertarClientesEvento(evento, salaNome, extra){
|
||||
// Pesquisar pela sala desejada.
|
||||
//var sala = Util.pesquisarSala(salaNome);
|
||||
// Cancelar alerta caso a sala não seja existente.
|
||||
if (!(sala instanceof Object))
|
||||
throw new Error('Sala deve ser um objeto');
|
||||
|
||||
for (let x = 0; x < sala.clientes.length; x++){
|
||||
let mensagem = '<' + extra.apelido + '> ' + extra.mensagem;
|
||||
sala.clientes[x].soquete.send(JSON.stringify(new Resposta("EVENTO", "SERVIDOR", evento.substring(7), extra)));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Pesquisa por uma sala com o nome desejado e retorna um objeto Sala correspondente.
|
||||
* @param string sala O nome da sala.
|
||||
* @returns Sala O objeto da sala.
|
||||
* @since 12 de outubro de 2020, 19:49 (UTC -03:00).
|
||||
*/
|
||||
static pesquisarSala(sala){
|
||||
for (let x = 0; x < salas.length; x++)
|
||||
if (salas[x].sala === sala)
|
||||
return salas[x];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna o objeto de um cliente baseado em seu apelido e sala.
|
||||
* @param Object sala O objeto da sala.
|
||||
* @param String apelido O apelido dado pelo jogador.
|
||||
* @since 23 de dezembro de 2020.
|
||||
*/
|
||||
static receberClientePorApelido(sala, apelido){
|
||||
for (let x = 0; x < sala.clientes.length; x++)
|
||||
if (sala.clientes[x].apelido === apelido)
|
||||
return sala.clientes[x];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filtra a mensagem recebida.
|
||||
* @param String mensagem A mensagem para filtrar.
|
||||
* @param Array filtro A lista de palavras filtradas, codificadas em Base64.
|
||||
* @since 25 de dezembro de 2020.
|
||||
*/
|
||||
static filtrarMensagem(mensagem, filtro){
|
||||
for (let x = 0; x < filtro.length; x++){
|
||||
let filtragem;
|
||||
let dec = Buffer.from(filtro[x], 'base64').toString(); // Termo Base64 decodificado
|
||||
let regex = Util.compilarRegex(dec);
|
||||
mensagem = mensagem.replace(regex, "[CENSURADO]")
|
||||
}
|
||||
return mensagem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforma o padrão providenciado em um RegEx avançado.
|
||||
* @param String regex O RegEx básico.
|
||||
* @param String flags As opções para serem passadas para o objeto RegExp. Este parâmetro não é obrigatório, e, quando não é especificado, o padrão é "gi".
|
||||
* @returns RegExp Um novo objeto RegExp criado baseado no padrão dado.
|
||||
* @since 28 de dezembro de 2020.
|
||||
*/
|
||||
static compilarRegex(regex, flags){
|
||||
if (flags === undefined) // caso nenhuma opção tenha sido dada
|
||||
flags = "gi";
|
||||
|
||||
let subst = { // possíveis substituições.
|
||||
"a": "(a|â|ã|á|à|ä|4|@)",
|
||||
"i": "(i|î|ĩ|í|ì|ï|1|!|l)",
|
||||
"e": "(e|ê|ẽ|é|è|ë|3)",
|
||||
"s": "(s|ŝ|ś|5)",
|
||||
"b": "(b|6|8)",
|
||||
"t": "(t|ẗ|7)",
|
||||
"o": "(o|ô|õ|ó|ò|ö|0)",
|
||||
"u": "(u|û|ũ|ú|ù|ü|v)",
|
||||
"c": "(c|ĉ|ć|k)"
|
||||
}
|
||||
|
||||
let letras = [];
|
||||
let detec = "(\\s|\\.|\\*)*"; // detector de separação de termos
|
||||
for (let x = 0; x < regex.length; x++){
|
||||
letras.push(regex[x]);
|
||||
if (x != regex.length - 1)
|
||||
letras.push(detec);
|
||||
}
|
||||
|
||||
for (let x = 0; x < letras.length; x++)
|
||||
if (letras[x][0] !== '(' && subst[letras[x]] !== undefined)
|
||||
letras[x] = subst[letras[x]];
|
||||
|
||||
let cr = "";
|
||||
for (let x = 0; x < letras.length; x ++)
|
||||
cr += letras[x];
|
||||
|
||||
return new RegExp(cr, flags);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Util;
|
||||
|
Reference in a new issue