Creating an Admin Script/Example1

From SA-MP Wiki

Jump to: navigation, search

Script

#include <a_samp>
#include <dini>
 
#if !defined floatstr
  native Float:floatstr(const string[]);
#endif
 
#define SLOTS 8
new player_level[SLOTS] = {-1, ...}; // Saves the player's level - the default
// one is -1 for not connected, 0 for not logged in and others for logged in
 
new player_names[SLOTS][MAX_PLAYER_NAME]; // Saves the player's names
#define USERFILE "users.txt"
 
#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
 
public OnPlayerCommandText(playerid, cmdtext[]) {
  dcmd(login,5,cmdtext);
  // dcmd(command-name (without /),   length of the command,   cmdtext);
  return 0; // This shows SERVER: Unknown Command
}
 
 
dcmd_login(playerid, params[]) {
  if(player_level[playerid] != 0) { // User is logged in
    SendClientMessage(playerid, 0xFFFFFFFF, "You are already logged in.");
	return 1; // We dont need to execute the rest of the function, do we?
  }
  else if(strlen(params) == 0) { // There is no password specified, only /login
    SendClientMessage(playerid, 0xFFFFFFFF, "Please use /login [password]");
    return 1;
  }
  else if(!dini_Isset(USERFILE,player_names[playerid])) { // not registered
    SendClientMessage(playerid, 0xFFFFFFFF, "Your nick is not registered.");
    SendClientMessage(playerid, 0xFFFFFFFF, "Use /register [password] first.");
    return 1;
  }
  else if(adler32_hash(params) != dini_Int(USERFILE,player_names[playerid])) {
	// There's a password hash generated and compared to the one from the config file
    SendClientMessage(playerid, 0xFFFFFFFF, "Password mismatch.");
    return 1;
  }
  player_level[playerid] = 1; // Logged in successfully
  SendClientMessage(playerid, 0xFFFFFFFF, "You are now logged in. Have a nice day.");
 
  return 1;
}
 
adler32_hash(buf[]) {
    new length=strlen(buf);
    new s1 = 1;
    new s2 = 0;
    new n;
    for (n=0; n<length; n++)
    {
       s1 = (s1 + buf[n]) % 65521;
       s2 = (s2 + s1)     % 65521;
    }
    return (s2 << 16) + s1;
}
 
 
public OnPlayerConnect(playerid) {
  if(player_level[playerid] == -1) {
    player_level[playerid] = 0;
    GetPlayerName(playerid,player_names[playerid],MAX_PLAYER_NAME); // Getting the playername and saving it
  }
  return 1;
}
public OnPlayerDisconnect(playerid) {
  player_level[playerid] = -1;
  return 1;
}

Example files

This script should now work, what happened is that something got changed and *** was in place of what should have been there and made everything go wrong.

Personal tools
Navigation
Toolbox