[HowTo] Using dudb 2.4 to save money/stats/whatever (0.2#Ready)

Quote:
Originally Posted by Micko9
Quote:
Originally Posted by DracoBlue
DUDB was released to have an easy to handle library to save and load data about users.

Even the commands are really damned simple, but still some guys around here, which just copy and paste and can't get it to work then. If you can't add a own command, if you don't know how to call functions (I don't mean Copy n' Paste, I mean know what each line of your source does!) - then please stop reading, get somebody to pay and create the source for you. Its a pitty to see guys not beeing able to code anything and then complaining about not beeing able to cut and paste this. Sorry guys, but this makes somebody angry who thought that this library is really easy as hell.

Example implementation wich saves money (the result of the howto is this script)
o DNick : http://forum.sa-mp.com/index.php?topic=4799

How to add "Position Saving?" - Read the second how to.
How to add "Save kills/death?" - Read the third how to.
How to use "myfolder/name.sav folder structure?" - Read the third how to.

I won't stick at the slow command handling in all default gamemodes, I will use the fast command processor here. So if you are not able to convert your OnPlayerCommandText-Code to the fast command processor-structure, stop and just don't use saving data.

I suggest you read the following text ones to understand how it works. Don't forget to create a folder called 'scriptfiles' in your samp directory to enable file access for the server.

We will develop a simple filterscript with all these features, so you can use this stub-file.

pawn Код:
#include <a_samp>
// for the samp stuff
#include <dutils>
// for the functions dini and dudb need
#include <dudb>
// for the userdatabase, get dudb version 2.0 for this.
#define dcmd(%1,%2,%3) if ((strcmp((%3)[1], #%1, true, (%2)) == 0) && ((((%3)[(%2) + 1] == 0) && (dcmd_%1(playerid, "")))||(((%3)[(%2) + 1] == 32) && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
#define COLOR_SYSTEM 0xEFEFF7AA

stock SystemMsg(playerid,msg[]) {
 if ((IsPlayerConnected(playerid))&&(strlen(msg)>0)) {
   SendClientMessage(playerid,COLOR_SYSTEM,msg);
 }
 return 1;
}

public OnPlayerCommandText(playerid,cmdtext[]) {
 return false;
}

public OnPlayerConnect(playerid) {
 return false;
}

/*
 * This function will be useful when we need the playername
 * © by DracoBlue 2006
 */

stock PlayerName(playerid) {
 new name[255];
 GetPlayerName(playerid, name, 255);
 return name;
}
We will use SystemMsg for things the system sends. The dcmd-macro is what we use for the fast command processing later.

First of all we need a login/register method.

Nothing easier then this.

pawn Код:
/*
 * /register password
 *
 */

 dcmd_register(playerid,params[]) {

  // The command shouldn't work if we already are authed
  if (PLAYERLIST_authed[playerid]) return SystemMsg(playerid,"Already authed.");

  // The command shouldn't work if an account with this
  // nick already exists
  if (udb_Exists(PlayerName(playerid))) return SystemMsg(playerid,"Account already exists, please use '/login password'.");

  // Did he forgot the password?
  if (strlen(params)==0) return SystemMsg(playerid,"Correct usage: '/register password'");

  // We save the money to the accstate
  if (udb_Create(PlayerName(playerid),params)) return SystemMsg(playerid,"Account successfully created. Login with '/login password' now.");
  return true;

 }
Thats it? Yes thats it!

Want to see the /login?

pawn Код:
/*
 * /login password
 *
 */

 dcmd_login(playerid,params[]) {

  // The command shouldn't work if we already are authed
  if (PLAYERLIST_authed[playerid]) return SystemMsg(playerid,"Already authed.");

  // The command shouldn't work if an account with this
  // nick does not exists
  if (!udb_Exists(PlayerName(playerid))) return SystemMsg(playerid,"Account doesn't exist, please use '/register password'.");

  // Did he forgot the password?
  if (strlen(params)==0) return SystemMsg(playerid,"Correct usage: '/login password'");

  if (udb_CheckLogin(PlayerName(playerid),params)) {
   // Login was correct

   // Following thing is the same like the missing SetPlayerCommand
   GivePlayerMoney(playerid,dUserINT(PlayerName(playerid)).("money")-GetPlayerMoney(playerid));

   PLAYERLIST_authed[playerid]=true;

   return SystemMsg(playerid,"Successfully authed!");
  }
  // Login was incorrect
  return SystemMsg(playerid,"Login failed!");
 }
Of course we need to put a
pawn Код:
new PLAYERLIST_authed[MAX_PLAYERS];
at the beginning, because this will see if the player is authed or not.

We want to set this PLAYERLIST_authed for the player to false, if he enters the game.
pawn Код:
public OnPlayerConnect(playerid) {
 PLAYERLIST_authed[playerid]=false;
 return false;
}
Now we need to register the two commands and we are ready to use the filterscript!
pawn Код:
public OnPlayerCommandText(playerid,cmdtext[]) {
 dcmd(login,5,cmdtext); // because login has 5 characters
 dcmd(register,8,cmdtext); // because register has 8 characters
 return false;
}
One thing is missing, we need to save the money if the player leaves the server.
pawn Код:
public OnPlayerDisconnect(playerid) {
 if (PLAYERLIST_authed[playerid]) {
  // Was loggedin, so save the data!
  dUserSetINT(PlayerName(playerid)).("money",GetPlayerMoney(playerid));
 }
 PLAYERLIST_authed[playerid]=false;
 return false;
}
Thats it.


U FORGOT SOMETHING REALLY IMPORTNAT i read the dudb...you need dini

and much plp have errors becouse of that it uses alot of functions with dini ex : dini_Create in udb_Create n much so...
i have modified dudb and these are my errors ( i just changed the account file path ) :

Код:
error 027: invalid character constant
MYSERVER_DUDB(100) : error 027: invalid character constant
MYSERVER_DUDB(144) : error 027: invalid character constant
MYSERVER_DUDB(144) : error 027: invalid character constant
Reply

Show the modified file path.
Reply

how would i save ip's? i tryed but it just shows numbers like 284 like 1-3 theyre all random i put this to see if it would work, it guess its not
pawn Код:
dUserSetINT(PlayerName(playerid)).("ip",GetPlayerIp(playerid, pIP, sizeof(pIP)));
dosent work, im doing this so i can have auto login for matching i[p's to their userfiles, if this is possible please post a how to or some code on it, thanks
Reply

On your register command add:

Код:
new plrip[128];
GetPlayerIp(playerid,plrip,sizeof(plrip));
Код:
dUserSet(PlayerName(playerid)).("ipaddress",plrip);
Reply

Does someone know why 'я' appears on the first line of the file sometimes? If I have password_hash on the first line,
it becomes яpassword_hash and then I fail to login until I remove it from there.. k thnx!
Reply

for some reason i get a error on my auto-login function, error is
pawn Код:
C:\Users\Windows Vista\Desktop\sa-mpr7\gamemodes\Cranked.pwn(518) : error 006: must be assigned to an array
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase


1 Error.
that line is
pawn Код:
IP[playerid] = dUser(PlayerName(playerid)).("ip");
anyone know why?
Reply

Quote:
Originally Posted by [DJ
[SF]Зурэ Яииr ∞™ ]
for some reason i get a error on my auto-login function, error is
pawn Код:
C:\Users\Windows Vista\Desktop\sa-mpr7\gamemodes\Cranked.pwn(518) : error 006: must be assigned to an array
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase


1 Error.
that line is
pawn Код:
IP[playerid] = dUser(PlayerName(playerid)).("ip");
anyone know why?
You have to format the string, in order to make this work.
pawn Код:
format(IP[playerid],sizeof(IP[playerid]), "%s", dUser(PlayerName(playerid)).("ip"));
Have fun coding,
Draco
Reply

tyvm for replying to me, but there is another problem, its with my autologin it dosent seem to be working, even tho i have the IP saved in the file, it just isnt working tho, here is what i have
pawn Код:
format(IP,sizeof(IP), "%s", dUser(PlayerName(playerid)).("ip"));
  new pIp[16];
  GetPlayerIp(playerid, pIp, sizeof(pIp));
  if(strcmp(IP,pIp,true))
  {
  PLAYERLIST_authed[playerid]=true;
  SendClientMessage(playerid, COLOR_YELLOW, "You Have Been Automaticly Logged In");
  }
also i tried
pawn Код:
if(strcmp(IP,pIp,false))
it didnt help, the
pawn Код:
if(IP == pIp)
seemed to give me errors, what am i doing wrong here?
Reply

Quote:
Originally Posted by [DJ
[SF]Зурэ Яииr ∞™ ]
... also i tried
pawn Код:
if(strcmp(IP,pIp,false))
it didnt help, the
pawn Код:
if(IP == pIp)
seemed to give me errors, what am i doing wrong here?
Nothing big . I help you to get an understanding, how to compare strings. First of all, IP == pIp does not work, you need strcmp. But strcmp has a not so intuitive interface if you want to check for equality.

http://www.compuphase.com/pawn/String_Manipulation.pdf @page 7
Quote:

Compare two strings
strcmp(const string1[], const string2[], bool: ignorecase=false, length=cellmax)

string1 The first string in the comparison.
string2 The first string in the comparison.
ignorecase If logically “true”, case is ignored during the compar-
ison.
length The maximum number of characters to consider for
comparison.

Returns:
−1 if string1 comes before string2,
1 if string1 comes after string2, or
0 if the strings are equal (for the matched length).

So:
pawn Код:
if(strcmp(IP,pIp) === 0) {
 // yay! same!
}
should do the trick.

Have fun coding,
Draco
Reply

Hello, If i have co ordinates at the bottom of my dudb file, How can i pull them out of it and save them into a varible?

pawn Код:
password_hash=501089376
CurrentOwner=Vacant_House
HouseName=Null_Property
ForSale=1
HouseSize=Small
CurrentPrice=2000000
PropertyTaxesPerWeek=1250
TimesPurchased=0
CarSaving=Yes
1248.7869,4797.4976,7896.4784
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)