/*
#                 Cez-Account  v1.0
#        (c)  Copyright 2008 - 2009 Cezar Mihail Ignat
#
#    Requires:  DracoBlue's DJson  -  (c) Copyright 2008 DracoBlue
#                [url=http://cgarage.blogspot.com/]http://cgarage.blogspot.com/[/url]
#
*/
// Small modifications by Lavamike


#if defined _cezacc_included
  #endinput
#endif

#define _cezacc_included

#include <djson>

// DUDB Conversions
#define dUser(%1).( AccountGet(%1,
#define dUserINT(%1).( AccountGetInt(%1,
#define dUserSet(%1).( AccountSet(%1,
#define dUserSetINT(%1).( AccountSetInt(%1,
#define dUserSetFLOAT(%1).( AccountSetFloat(%1,
#define dUserFLOAT(%1).( AccountGetFloat(%1,
#define dUserIsSet(%1).( AccountIsSet(%1,
#define udb_Exists(%1) AccountExists(%1)

// Whirlpool for SA:MP by Y-Less
// [url=http://forum.sa-mp.com/index.php?topic=89418.0]http://forum.sa-mp.com/index.php?topic=89418.0[/url]
native WP_Hash(buffer[], len, const str[]);

stock num_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;
}

stock AccountExists(nickname[]) {
  return djIsSet("accounts.json",nickname,false);
}


stock AccountRemove(nickname[]) {
  return djUnset("accounts.json",nickname);
}

stock AccountSetInt(nickname[],key[],value) {
  if(!AccountExists(nickname)) return false;
  new str[255];
  format(str, sizeof(str), "%s/%s", nickname, key); 
  return djSetInt("accounts.json", str, value);
}

stock AccountSetFloat(nickname[],key[],Float:value) {
  if(!AccountExists(nickname)) return false;
  new str[255];
  format(str, sizeof(str), "%s/%s", nickname, key); 
  return djSetFloat("accounts.json", str, value);
}

stock AccountSet(nickname[],key[],value[]) {
  if(!AccountExists(nickname)) return false;
  new str[255];
  format(str, sizeof(str), "%s/%s", nickname, key); 
  return djSet("accounts.json", str, value);
}

stock AccountGet(nickname[],key[]) {
  new str[255];
  format(str, sizeof(str), "%s/%s", nickname, key); 
  return dj("accounts.json", str);
}

stock Float:AccountGetFloat(nickname[],key[]) {
  new str[255];
  format(str, sizeof(str), "%s/%s", nickname, key); 
  return djFloat("accounts.json",str);
}

stock AccountGetInt(nickname[],key[]) {
  new str[255];
  format(str, sizeof(str), "%s/%s", nickname, key); 
  return djInt("accounts.json",str);
}

stock AccountCheckLogin(nickname[],pwd[]) {
  new str[255];
  format(str, sizeof(str), "%s/password", nickname); 
  if (djInt("accounts.json",str) == num_hash(pwd)) return true;
  return false;
}

stock AccountCreate(nickname[],pwd[]) {
  if (AccountExists(nickname)) return false;
  new str[31];
  format(str, sizeof(str), "%s/password", nickname);
  new buf[145];
  WP_Hash(buf, sizeof(buf), pwd);
  djSet("accounts.json",str,buf);
  printf("Account Created: %s - %s\n%s", nickname, pwd, buf);
  return true;
}

// I'm pretty sure I added this into the DUDB include a while ago.
// I just added and converted it here since I used it in my script :P
// It just checks if a key has something set to it or not.
stock AccountIsSet(nickname[],key[]) {
  new str[255];
  format(str, sizeof(str), "%s/%s", nickname, key);
  return djIsSet("accounts.json",str);
}