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

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.
Reply
#2

everytime i do this i get this error and i dont no how to fix it....

pawn Код:
C:\Program Files\Rockstar Games\GTA San Andreas\SA-MP\gamemodes\Basic_DM.pwn(969) : warning 203: symbol is never used: "fcopy"
C:\Program Files\Rockstar Games\GTA San Andreas\SA-MP\gamemodes\Basic_DM.pwn(969) : warning 203: symbol is never used: "frename"
C:\Program Files\Rockstar Games\GTA San Andreas\SA-MP\gamemodes\Basic_DM.pwn(969) : warning 203: symbol is never used: "strlower"
C:\Program Files\Rockstar Games\GTA San Andreas\SA-MP\gamemodes\Basic_DM.pwn(969) : warning 203: symbol is never used: "strupper"
but there is no line 969....
Reply
#3

update dutils please
Reply
#4

o kl. gonna fix it thanx
Reply
#5

everytime i try to compile it, windows jus says that it has encountered a problem and needs to close...i wud post my script but it is big. can u help plz. thanx
Reply
#6

its a filterscript, why is the code big then?
Reply
#7

Draco tnx a lot, i m no making RPG/Dm server, so i searched for this, so thanks again
Reply
#8

This is an example how to add a setPostion/getPosition to the dudb!

We want to save/read 3 floats extra.

Change this in your [b]public
Out dated, is now much easier
Look in first post.
Reply
#9

TNX FOR HELP
Reply
#10

up.... hey no one uses DUDB? please I need a little advice
Reply
#11

I knew you gonna put this as a quote but even Draco made some mistakes in his examples like floatstr() and strfloat()...
Quote:

I mean know what each line of your source does!

- i know what each line of my source code does but i never worked with DUDB before... so you could spend a few minutes reading my post and sharing your knowledge... but if you dont want - then sorry.

One more question - what is the reason of 'Scripting' section if no one can help me with 'Scripting'? I dont know where else can I ask for help... I got an idea - delete this forum and leave only 'Script Showroom' section, is this gonna be ok? No more n00bs asking for help, no need to say 'SEARCH' and stuff like 'If you dont know nothing - then please stop reading, go away, delete PAWNO and throw your PC out of the window...'
Reply
#12

Quote:
Originally Posted by axelpx
No more n00bs asking for help, no need to say 'SEARCH' and stuff like 'If you dont know nothing - then please stop reading, go away, delete PAWNO and throw your PC out of the window...'
roflmfao hahahaha
Reply
#13

Quote:
Originally Posted by axelpx
I knew you gonna put this as a quote but even Draco made some mistakes in his examples like floatstr() and strfloat()...
No the a_samp.inc in official release is broken.
Reply
#14

First I'd like to say thanks for posting this... its a big help and its an easy system to use and to learn from.
Second... I've been tweaking around with the get / set pos and it occured to me the one error in the filterscript you helped us make was that if you login to a made account, then exit the server, and come back in.. you keep your money without having to login again.... in other words if i wanted somones money all i'd have to do is wait for them to log in once, then when they logout, come on their name and get their doe.

I've spent a bit tweaking it and messing around with it but I cannot figure out why in the world its doing this... I've sat here and broke down every line of code in the DUDB.inc and the filterscript, and perhaps I missed something but guidence would be appretiated.. not asking for a code laid out and given to me, because I do enjoy working on stuff like this, but I would like a tip or something along the lines of that.

I was wondering if it was still reading the person as being authed when they connect... but then on the other hand it has auth false on connect?

Reguards,
Rob S.
Reply
#15

*edited*

NVM, got it working... thanks for the help bro... totally didn't even think about the gamemode
Reply
#16

Another question while im here... I set up the setPosition and getPosition that draco stated in one of his replies... and I got the calling and everything done... just one weird thing. Now whenever i try to register or something it just says "unknown command" and does nothing.

When I removed the calling in the filter and then went back to the plain DUDB.inc without the get/setposition modifications it worked fine...

Confuzled, me and my friend have been working on a gamemode for abit and just recently found this easier way to database info, and since then this problem has been a tough one, i'm decent at C++ but this has its differences...

Thanks in advance,
Rob S.
Reply
#17

I add modify good for LVDM.
He check when user is registered and when is not login in 60s, kick him.

code:
Code:
//authorize timeout
#define AUTH_TIMEOUT 60000
#define NOTIFY_TIME 10000
#define NOTIFY_TIMEOUT 15000
//player timer ids
new PLAYERLIST_timer[MAX_PLAYERS];
create user timer when is player registered
Code:
public OnPlayerConnect(playerid) {
 	PLAYERLIST_authed[playerid]=false;
  //have account?
	if(udb_Exists(PlayerName(playerid))){
	  new str[50];
		SystemMsg(playerid,"This user have account pleas register to 60s or you be kicked!(/login pass)");
		format(str,sizeof(str),"TIPL_%i",playerid);
		PLAYERLIST_timer[playerid] = SetTimer(str,AUTH_TIMEOUT,0);
		format(str,sizeof(str),"TNOT_%i",playerid);
		PLAYERLIST_timer[playerid] = SetTimer(str,NOTIFY_TIME,0);
	}
	else{
	  SystemMsg(playerid,"You can register /register pass");
	}
	return true;
}
user timer function:
Code:
//optimize this SetTimerEx (from tse) not work!
public TIPL_0(){TimerIsPlayerLoged(1);}
.....
.....
public TIPL_99(){TimerIsPlayerLoged(99);}


public TimerIsPlayerLoged(playerid){
	new str[256];
	if(!IsPlayerConnected(playerid))
	  return;
	if(PLAYERLIST_authed[playerid]){
	  SystemMsg(COLOR_SYSTEM,"Byl jsi uspesne autorizovan!");
	}
	else{
		format(str,sizeof(str),"User %s has not be autorized, kicked!",PlayerName(playerid));
		SendClientMessageToAll(COLOR_SYSTEM,str);
		Kick(playerid);
	}
}
Notify text timer function:
Code:
//optimize this SetTimerEx (from tse) not work!
public TNOT_0(){TimerNotify(0);}
....
....
public TNOT_99(){TimerNotify(99);}

public TimerNotify(playerid){
	if(!IsPlayerConnected(playerid))
	  return;
 	SystemMsg(playerid,"This user have account pleas register to 60s or you be kicked!(/login pass)");
	GameTextForPlayer(playerid,"~R~This user have account pls register to 60s or you be kicked!(/login pass)",NOTIFY_TIMEOUT,1);
}
Kill timer when player disconect:
Code:
public OnPlayerDisconnect(playerid) {
 ..
 KillTimer(PLAYERLIST_timer[playerid]);
...
}
This is all, not optimized code, thx for help with optimizing...

btw: is good add timer for save world(when server crasht), after 5 min interval save all connected user money.
Reply
#18

hi

i need the pwn file for this filterscript. i will translate it for my server in my language.

THX
Reply
#19

The pawn-code is there.
Reply
#20

how to fix it for saving score?
Reply


Forum Jump:


Users browsing this thread: 13 Guest(s)