All pastes #137523 Raw Edit

DUDB

public unlisted text v1 · immutable
#137523 ·published 2006-08-18 13:09 UTC
rendered paste body
#include <a_samp>
// for the samp stuff
#include <dutils>
// for the functions dini and dudb need
#include <dudb>
// for the userdatabase, get dudb version 1.2 for this.
#define dcmd(%1,%2,%3) if ((strcmp(%3, "/%1", true, %2+1) == 0)&&(((%3[%2+1]==0)&&(dcmd_%1(playerid,"")))||((%3[%2+1]==32)&&(dcmd_%1(playerid,%3[%2+2]))))) return 1
#define COLOR_SYSTEM 0x33AA33FF

new PLAYERLIST_authed[MAX_PLAYERS];

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

public OnPlayerCommandText(playerid,cmdtext[]) {
  dcmd(login,5,cmdtext); // because login has 5 characters
  dcmd(register,8,cmdtext); // because register has 8 characters
  return false;
}

public OnPlayerDisconnect(playerid) {
  if (PLAYERLIST_authed[playerid]) {
     udb_setAccState(PlayerName(playerid),GetPlayerMoney(playerid));
  }
  return false;
}

public OnPlayerConnect(playerid) {
  PLAYERLIST_authed[playerid]=false;
  return false;
}

public PlayerName(playerid) {
  new name[MAX_PLAYER_NAME];
  GetPlayerName(playerid, name, MAX_PLAYER_NAME);
  return name;
}

 dcmd_register(playerid,params[]) {

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

    // 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,GetPlayerMoney(playerid),"")) return SystemMsg(playerid,"Account successfully created! Now login using /login password.");
    return true;

 }
 
 /*
 *  /login password
 *
 */
  dcmd_login(playerid,params[]) {

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

    // 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, register your nickname using /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

       // Since we are using the "accstate"-variable to save and
       // read the money of the player, we can update his money now.

       // Following thing is the same like the missing SetPlayerCommand
       GivePlayerMoney(playerid,udb_getAccState(PlayerName(playerid))-GetPlayerMoney(playerid));

       PLAYERLIST_authed[playerid]=true;

       return SystemMsg(playerid,"Logged In!");
    } else {
       // Login was incorrect
       return SystemMsg(playerid,"Incorrect password, please try again");
    }
    return true;
 }