14.06.2012, 00:36
(
Последний раз редактировалось ipsBruno; 14.06.2012 в 03:43.
)
Arquivos Temporбrios
ATUALIZADO - ARMAZENA TEMPO MESMO AO FECHAR SERVIDOR
pawn Код:
#include <a_samp>
// Redefina aqui em caso de for necessбrio
#define fp%0(%1) forward %0(%1); public %0(%1)
#if !defined MAX_FILE_NAME
#define MAX_FILE_NAME 32
#endif
#if !defined MAX_TEMP_FILES
#define MAX_TEMP_FILES 100
#endif
// Declarar variбveis novas
enum bsInfo {
szFilename[MAX_FILE_NAME],
fileTime
}
new
bstempFile[MAX_TEMP_FILES][bsInfo],
bsTotalfiles = -1;
// Ao iniciar script, chamar a pъblica para checar os arquivos
// Carregar o arquivo nas arrays
public OnFilterScriptInit() {
if(fexist("filesrename.bs")) {
new
File:file = fopen("filesrename.bs", io_read),
File:time = fopen("filestimers.bs", io_read),
stringRename[MAX_TEMP_FILES * MAX_FILE_NAME char],
stringTimers[MAX_TEMP_FILES * 13 char];
fread(file, stringRename, (MAX_TEMP_FILES * MAX_FILE_NAME) >> 2, true); // >> 2 para adaptar a char
fread(time, stringTimers, (MAX_TEMP_FILES * 13) >> 2, true); // 13 pois pawn sу aceita nъmeros com +- 13 algarismos
fclose(file);
fclose(time);
bsTotalfiles++;
// Carregar nomes arquivos
for(new x, j = -1; stringRename{x}; x++) {
if(stringRename{x} == '>') {
j = -1;
bsTotalfiles++;
}
else {
bstempFile[bsTotalfiles][szFilename][++j] = stringRename{x};
}
}
bsTotalfiles = 0;
// Carregar tempos
for(new x, j = -1, stringTemp[13]; stringTimers{x}; x++) {
if(stringTimers{x} == '|') {
j = -1;
bsTotalfiles++;
bstempFile[bsTotalfiles][fileTime] = strval(stringTemp);
stringTemp[0] = EOS;
}
else {
stringTemp[++j] = stringTimers{x};
}
}
fremove("filesrename.bs");
fremove("filestimers.bs");
printf("Arquivos temporбrios carregados: %d", bsTotalfiles);
}
return SetTimer("checarArquivos", 998, true); // 998 ms. Para demorar em torno de 1 segundo (2 ms execuзгo callback)
}
// Exportar array para arquivo
// Assim pode carregar os dados posteriormente
public OnFilterScriptExit() {
new
File:file = fopen("filesrename.bs", io_write),
File:time = fopen("filestimers.bs", io_write);
for(new i; i <= bsTotalfiles; i++) {
if(gettime() <= bstempFile[i][fileTime]) {
if(fexist(bstempFile[i][szFilename])) {
if(file) {
fwrite(file, bstempFile[i][szFilename]);
fwrite(time, intToStr(bstempFile[i][fileTime]));
fwrite(file, ">");
fwrite(time, "|");
}
}
}
}
if(file) {
fclose(file);
fclose(time);
}
return true;
}
// Checar se o tempo do arquivo jб passou do invбlido
// Aqui й deletado o arquivo apуs x tempo
fp checarArquivos() {
for(new i; i <= bsTotalfiles; i++) {
if(gettime() >= bstempFile[i][fileTime]) {
if(fexist(bstempFile[i][szFilename])) {
fremove(bstempFile[i][szFilename]);
CallRemoteFunction("OnTemporaryDeleteFile", "s", bstempFile[i][szFilename]);
}
}
}
return true;
}
// Funзгo para criar o arquivoTemporario
// Bastando especificar os segundos e o arquivo
fp arquivoTemporario(arquivo[], segundos) {
// caso arquivo jб existir editar tempo atual
if(fexist(arquivo)) {
alterarTempo(arquivo, segundos);
}
static
File:file;
if((file = fopen(arquivo, io_write))) {
strcat(bstempFile[++bsTotalfiles][szFilename], arquivo, MAX_FILE_NAME);
return fclose(file), bstempFile[bsTotalfiles][fileTime] = (gettime() + segundos);
}
return false;
}
// Funзгo para alterar o tempo de deletaзгo do arquivo.
// Partindo o tempo atual
fp alterarTempo(arquivo[], segundos) {
new cmd = prev_strcmp(arquivo);
for(new i; i <= bsTotalfiles; i++) {
if(cmd == prev_strcmp(bstempFile[i][szFilename])) {
if(!strcmp(arquivo, bstempFile[i][szFilename], true)) {
return bstempFile[i][fileTime] = gettime() + segundos;
}
}
}
return false;
}
// Ajudar na velocidade de comparaзгo de strings
// Sistema simples de conta caracteres, compara dados nъmeros й mais veloz
fp prev_strcmp(string[]) {
new
j;
for(new i; string[i]; i++) {
j += string[i];
}
return j;
}
// Transformar valor em string
// Sem declarar strings chatas
intToStr(valor) {
new szString[13];
format(szString, 13, "%i", valor);
return szString;
}
Simples uso
pawn Код:
arquivoTemporario("bruno.txt", 10);
Modo de uso.
1 Primeiramente salve o cуdigo ali encima como filterscript e carregue-o no server.cfg ATENCAO. O cуdigo acima deve ser salvo como um filterscript e carregado.
2 Coloque isto no seu game mode . No GAME MODE o cуdigo abaixo
pawn Код:
#define alterarTempo(%0,%1) CallRemoteFunction("alterarTempo", "si", %0,%1)
#define arquivoTemporario(%0,%1) CallRemoteFunction("arquivoTemporario", "si", %0,%1)
forward OnDeleteTempFile(file[]);
![Tongue](images/smilies/razz.gif)
Exemplo - Funзхes para VIP
Umas funзхes vips:
pawn Код:
stock darVip(playerid, dias) {
static
namePlayer[MAX_PLAYER_NAME + 25]
;
GetPlayerName(playerid, namePlayer, MAX_PLAYER_NAME);
format(namePlayer, MAX_PLAYER_NAME + 25], "/pastaVips/%s.ini", namePlayer);
arquivoTemporario(namePlayer, dias * 60 * 60 * 24);
// x * 60 = 1 minuto
// x * 60 * 60 = 1 hora
// x * 60 * 60 * 24 = 1 dia
}
stock remVip(playerid) {
static
namePlayer[MAX_PLAYER_NAME + 25]
;
GetPlayerName(playerid, namePlayer, MAX_PLAYER_NAME);
format(namePlayer, MAX_PLAYER_NAME + 25], "/pastaVips/%s.ini", namePlayer);
alterarTempo(namePlayer, 0);
// 0 = deleta arquivo na hora
}
stock chkVip(playerid) {
static
namePlayer[MAX_PLAYER_NAME + 25]
;
GetPlayerName(playerid, namePlayer, MAX_PLAYER_NAME);
format(namePlayer, MAX_PLAYER_NAME + 25], "/pastaVips/%s.ini", namePlayer);
return fexist(namePlayer);
}
OnDeleteTempFile serб chamada com nome do arquivo cada vez que o arquivo й deletado apуs determinado tempo
Nгo ocorre lag no servidor. Jб coloquei um sistema de comparaзгo de strings similar ao do Bini e DOF2 usando dados nъmericos para diminuir caso tiver latкncia. Mesmo nгo tendo usando "string compare" direto
![Tongue](images/smilies/razz.gif)
* Nenhum erro encontrado *
Abraзos!