Bom dia, senhores leitores do blog do Cronos, eu sou o novo colaborador, antigo [JPP]MrBean, e hoje, vou ensinar vocês a salvar as coisas...
-- Introdução --
Voces vao ver o que salvar com o que eu vou ensinar voces a fazerem, claro que voces podem se aprimorar mais, mais apenas vou ensinar o básico...
enum aAcc
{
aPassword[128],
aScore,
aCash,
Float:aX,
Float:aY,
Float:aZ,
Float:aA,
aLogged,
}
Como visto acima, nós usamos apenas uma variável em vez de colocar uma variável pra cada coisa ocupando mais memória...
new AccountInfo[MAX_PLAYERS][aAcc];
A variável acima, será o que vai reconhecer o PlayerID do jogador para salvar as informaçoes...
-- Criando, Carregando e Salvando contas --
- Registrando
forward RegisterPlayer(playerid, password[]);
public RegisterPlayer(playerid, password[])
{
if(IsPlayerConnected(playerid))
{
new name[MAX_PLAYER_NAME], str[128];
GetPlayerName(playerid, name, sizeof name);
format(str, sizeof str, "%s.ini", name);
new File:account = fopen(str, io_write);
if (account)
{
strmid(AccountInfo[playerid][aPassword], password, 0, strlen(password), 128);
AccountInfo[playerid][aCash] = GetPlayerMoney(playerid);
AccountInfo[playerid][aScore] = GetPlayerScore(playerid);
GetPlayerPos(playerid, AccountInfo[playerid][aX], AccountInfo[playerid][aY], AccountInfo[playerid][aZ]);
GetPlayerFacingAngle(playerid, AccountInfo[playerid][aA]);
new file[128];
{
format(file, sizeof file, "Password: %s\n", AccountInfo[playerid][aPassword]);
{ fwrite(account, file); }
format(file, sizeof file, "Grana: %d\n",AccountInfo[playerid][aCash]);
{ fwrite(account, file); }
format(file, sizeof file, "Score: %d\n", AccountInfo[playerid][aScore]);
{ fwrite(account, file); }
format(file, sizeof file, "X: %f\n",AccountInfo[playerid][aX]);
{ fwrite(account, file); }
format(file, sizeof file, "Y: %f\n",AccountInfo[playerid][aY]);
{ fwrite(account, file); }
format(file, sizeof file, "Z: %f\n",AccountInfo[playerid][aZ]);
{ fwrite(account, file); }
format(file, sizeof file, "Angulo: %f\n",AccountInfo[playerid][aA]);
{ fwrite(account, file); }
}
fclose(account);
SendClientMessage(playerid, 0x21DD00FF, "Voce foi registrado, agora logue-se.");
}
}
return 1;
}
- Agora que a conta foi criada vamos fazer o jogador se logar...
forward LoginPlayer(playerid, password[]);
public LoginPlayer(playerid, password[])
{
new name[MAX_PLAYER_NAME], str[128];
GetPlayerName(playerid, name, sizeof name);
format(str, sizeof str, "%s.ini", name);
new File:account = fopen(str, io_read);
if (account)
{
new pass[256];
new passres[128], value[128];
fread(account, pass, sizeof pass);
passres = GetFileString(pass);
if (!strcmp("Password", passres))
{
value = GetFileValue(pass);
strmid(AccountInfo[playerid][aPassword], value, 0, strlen(value)-1, 128);
}
if (!strcmp(AccountInfo[playerid][aPassword], password, true))
{
while (fread(account, pass, 256))
{
passres = GetFileString(pass);
if (strfind(passres, "Cash") != -1)
{
value = GetFileValue(pass);
AccountInfo[playerid][aCash] = strval(value);
}
if (strfind(passres, "Score") != -1)
{
value = GetFileValue(pass);
AccountInfo[playerid][aScore] = strval(value);
}
if (strfind(passres, "X") != -1)
{
value = GetFileValue(pass);
AccountInfo[playerid][aX] = floatstr(value);
}
if (strfind(passres, "Y") != -1)
{
value = GetFileValue(pass);
AccountInfo[playerid][aY] = floatstr(value);
}
if (strfind(passres, "Z") != -1)
{
value = GetFileValue(pass);
AccountInfo[playerid][aZ] = floatstr(value);
}
if (strfind(passres, "Angle") != -1)
{
value = GetFileValue(pass);
AccountInfo[playerid][aA] = floatstr(value);
}
}
fclose(account);
AccountInfo[playerid][aLogged] = 1;
}
else
{
SendClientMessage(playerid, 0xE60000FF, "Senha errada.");
fclose(account);
return 1;
}
GivePlayerMoney(playerid, AccountInfo[playerid][aCash]);
SetPlayerScore(playerid, AccountInfo[playerid][aScore]);
SetPlayerPos(playerid, AccountInfo[playerid][aX], AccountInfo[playerid][aY], AccountInfo[playerid][aZ]);
SetPlayerFacingAngle(playerid, AccountInfo[playerid][aA]);
SendClientMessage(playerid, 0x21DD00FF, "Logado com sucesso");
printf("%s logou", name);
}
return 1;
}
Agora vamos salvar....
forward SavePlayer(playerid);
public SavePlayer(playerid)
{
if(IsPlayerConnected(playerid))
{
if(AccountInfo[playerid][aLogged] == 1)
{
new name[MAX_PLAYER_NAME], str[128];
GetPlayerName(playerid, name, sizeof name);
format(str, sizeof str, "%s.ini", name);
new File:account = fopen(str, io_write);
if (account)
{
AccountInfo[playerid][aCash] = GetPlayerMoney(playerid);
AccountInfo[playerid][aScore] = GetPlayerScore(playerid);
GetPlayerPos(playerid, AccountInfo[playerid][aX], AccountInfo[playerid][aY], AccountInfo[playerid][aZ]);
GetPlayerFacingAngle(playerid, AccountInfo[playerid][aA]);
new file[128];
{
format(file, sizeof file, "Password: %s\n", AccountInfo[playerid][aPassword]);
{ fwrite(account, file); }
format(file, sizeof file, "Grana: %d\n",AccountInfo[playerid][aCash]);
{ fwrite(account, file); }
format(file, sizeof file, "Score: %d\n", AccountInfo[playerid][aScore]);
{ fwrite(account, file); }
format(file, sizeof file, "X: %f\n",AccountInfo[playerid][aX]);
{ fwrite(account, file); }
format(file, sizeof file, "Y: %f\n",AccountInfo[playerid][aY]);
{ fwrite(account, file); }
format(file, sizeof file, "Z: %f\n",AccountInfo[playerid][aZ]);
{ fwrite(account, file); }
format(file, sizeof file, "Angulo: %f\n",AccountInfo[playerid][aA]);
{ fwrite(account, file); }
}
fclose(account);
}
}
}
return 1;
}
-- AGORA FAZENDO O BONECO SALVAR MESMO --
public OnPlayerDisconnect(playerid, reason)
{
SavePlayer(playerid);
return 1;
}
Isso vai salvar quando ele sair =D
public OnPlayerConnect(playerid)
{
AccountInfo[playerid][aCash] = 0;
AccountInfo[playerid][aScore] = 0;
AccountInfo[playerid][aX] = 0;
AccountInfo[playerid][aY] = 0;
AccountInfo[playerid][aZ] = 0;
AccountInfo[playerid][aA] = 0;
strmid(AccountInfo[playerid][aPassword], "-", 0, 1, 1);
return 1;
}
Isso vai criar um pedaço da conta quando ele entrar =D
-- AGORA OS COMANDOS --
new cmd[128], tmp[128], idx;
cmd = strtok(cmdtext, idx);
if (!strcmp(cmd, "/logar", true))
{
new tmppass[128];
if(AccountInfo[playerid][aLogged] == 1) return SendClientMessage(playerid, 0xE60000FF, "Você já está logado.");
tmp = strtok(cmdtext, idx);
if(!strlen(tmp)) return SendClientMessage(playerid, 0xF97804FF, "USE: /logar [password]");
new plname[MAX_PLAYER_NAME];
GetPlayerName(playerid, plname, sizeof plname);
format(plname, sizeof plname, "%s.ini", plname);
if(!fexist(plname)) return SendClientMessage(playerid, 0xE60000FF, "Por favor registre-se primeiro");
strmid(tmppass, tmp, 0, strlen(cmdtext), 128);
Encrypt(tmppass);
LoginPlayer(playerid, tmppass);
return 1;
}
if (!strcmp(cmd, "/registrar", true))
{
if(AccountInfo[playerid][aLogged] == 1) return SendClientMessage(playerid, 0xE60000FF, "Você já está logado.");
new plname[MAX_PLAYER_NAME];
GetPlayerName(playerid, plname, sizeof plname);
format(plname, sizeof plname, "%s.ini", plname);
new File: hFile = fopen(plname, io_read);
if (hFile)
{
SendClientMessage(playerid, 0xE60000FF, "Alguem já está usando esse nick.");
fclose(hFile);
return 1;
}
new tmppass[128];
tmp = strtok(cmdtext, idx);
if(!strlen(tmp)) return SendClientMessage(playerid, 0xF97804FF, "USE: /registrar [password]");
strmid(tmppass, tmp, 0, strlen(cmdtext), 128);
Encrypt(tmppass);
RegisterPlayer(playerid, tmppass);
return 1;
}
-- E AGORA AS FUNÇOES DE PROTEÇAO DE SENHA --
stock Encrypt(string[])
{
for(new x=0; x < class="pawnkeyword">if(string[x] > (0xff))
{
string[x] -= 256;
}
}
}
stock strtok(const string[], &index,seperator=' ')
{
new length = strlen(string);
new offset = index;
new result[128];
while ((index < class="pawnkeyword">if ((index < class="pawnkeyword">return result;
}
Gente, de meu próximo tutorial, vou ensinar a usar dini, esse é so para voces verem mesmo que pode-se fazer sem usar outro include...