/*Las Venturas Freeroam
 *Created By Assasin
 *All Rights Reserved(2006)
 *
*/

#if defined _dutils_included
  #endinput
#endif

new logged[MAX_PLAYERS];
new bank[MAX_PLAYERS];
#define FILE_SETTINGS "settings.ini"
#define FILE_TOTALSTAT "totalstat.ini"

public StatGivePlayerMoney(playerid, amount)
{
	new tmp[256];
	if (IsPlayerConnected(playerid)) {
	    tmp = dini_Get(FILE_SETTINGS, "maxmoney");
		if (GetPlayerMoney(playerid)+amount <= strval(tmp)) {
		    GivePlayerMoney(playerid, amount);
		    if (!dini_Exists(FILE_TOTALSTAT)) dini_Create(FILE_TOTALSTAT);
			if (amount > 0) {
                tmp = dini_Get(FILE_TOTALSTAT, "moneyreceived");
				dini_IntSet(FILE_TOTALSTAT, "moneyreceived", strval(tmp)+amount);
			}
			else {
			    tmp = dini_Get(FILE_TOTALSTAT, "moneylost");
				dini_IntSet(FILE_TOTALSTAT, "moneylost", strval(tmp)-amount);
			}
		}
		else {
		    SendClientMessage(playerid, COLOR_RED, "* WARNING! Your money at maximum and you cannot receive more money!");
		    SendClientMessage(playerid, COLOR_RED, "* Store money in bank or send them to be able to get more.");
		    return 0;
		}
	}
	return 1;
}


public OnPlayerCommandText(playerid, cmdtext[])
{
     new cmd[256];
     new tmp[256],tmp2[256];
     new playername[MAX_PLAYER_NAME];
     new idx;
     new moneys;
     new string[256];
     new giveplayer[MAX_PLAYER_NAME];
     new sendername[MAX_PLAYER_NAME];
     new giveplayerid,playermoney; 

if(strcmp(cmd, "/login", true) == 0) {
	    tmp = dini_Get(FILE_SETTINGS, "register");
		if (strval(tmp) == 0) {
		    SendClientMessage(playerid, COLOR_RED, "* Register are not allowed on this server!");
		    return 1;
		}

	    tmp = strtok(cmdtext, idx);

 		GetPlayerName(playerid, playername, sizeof(playername));
	    if(!strlen(tmp))
			SendClientMessage(playerid, COLOR_WHITE, "* USAGE: /login [password]");

		else {
			if (dini_Exists(udb_encode(playername))) {
		    	if (logged[playerid] == 1) {
		    	    format(string, sizeof(string), "--- %s already logged.", playername);
					printf(string);
					SendClientMessage(playerid, COLOR_YELLOW, "* You already logged.");
				}
				else {
					tmp2 = dini_Get(udb_encode(playername), "password");
			  		if (udb_hash(tmp) != strval(tmp2)) {
		  			    format(string, sizeof(string), "--- %s (id: %d) typed wrong password: %s.", playername, playerid, tmp);
						printf(string);
			    		SendClientMessage(playerid, COLOR_RED, "* Wrong password.");
					}
					else {
						logged[playerid] = 1;
						format(string, sizeof(string), "--- %s (id: %d) logged in. Password: %s.", playername, playerid, tmp);
						printf(string);
						SendClientMessage(playerid, COLOR_GREEN, "* You are logged in!");
					}
				}
			}
			else {
			    format(string, sizeof(string), "* %s no such account.", playername);
				SendClientMessage(playerid, COLOR_RED, string);
			}
		}

		return 1;
	}


if(strcmp(cmd, "/register", true) == 0) {
	    tmp = dini_Get(FILE_SETTINGS, "register");
		if (strval(tmp) == 0) {
		    SendClientMessage(playerid, COLOR_RED, "* Register are not allowed on this server!");
		    return 1;
		}
		tmp = dini_Get(FILE_SETTINGS, "maxusers");
		tmp2 = dini_Get(FILE_TOTALSTAT, "users");
		if (strval(tmp2) >= strval(tmp)) {
		    SendClientMessage(playerid, COLOR_RED, "* User limit reached! You cannot register.");
		    return 1;
		}

	    tmp = strtok(cmdtext, idx);

 		GetPlayerName(playerid, playername, sizeof(playername));

        if(20 < strlen(tmp) || strlen(tmp) < 5) {
			SendClientMessage(playerid, COLOR_YELLOW, "* Password length must be 5-20 symbols.");
			return 1;
		}

	    if(!strlen(tmp))
			SendClientMessage(playerid, COLOR_WHITE, "* USAGE: /register [password]");

		else {
			if (!dini_Exists(udb_encode(playername))) {
				dini_Create(udb_encode(playername));
				dini_IntSet(udb_encode(playername), "password", udb_hash(tmp));
				tmp2 = dini_Get(FILE_SETTINGS, "defaultprotection");
				dini_IntSet(udb_encode(playername), "protection", strval(tmp2));
				dini_Set(udb_encode(playername), "x", "x");
   				dini_Set(udb_encode(playername), "y", "y");
    			dini_Set(udb_encode(playername), "z", "z");
                if (!dini_Exists(FILE_TOTALSTAT)) dini_Create(FILE_TOTALSTAT);
                tmp2 = dini_Get(FILE_TOTALSTAT, "users");
				dini_IntSet(FILE_TOTALSTAT, "users", strval(tmp2)+1);
				format(string, sizeof(string), "--- %s (id: %d) created account. Password: %s.", playername, playerid, tmp);
				printf(string);
				format(string, sizeof(string), "* Account named %s created! You can login with password %s.", playername, tmp);
				SendClientMessage(playerid, COLOR_GREEN, string);
			}
			else {
				format(string, sizeof(string), "* %s already registered.", playername,tmp);
				SendClientMessage(playerid, COLOR_RED, string);
			}
		}

		return 1;
	}




if (strcmp(cmdtext, "/close", true) == 0 || strcmp(cmd, "/lock", true) == 0) {
		tmp = dini_Get(FILE_SETTINGS, "locking");
		if (strval(tmp) == 0) {
		    SendClientMessage(playerid, COLOR_RED, "* Locking are not allowed on this server!");
		    return 1;
		}

		if(IsPlayerInAnyVehicle(playerid)) {
			if(GetPlayerState(playerid) != PLAYER_STATE_DRIVER) {
				SendClientMessage(playerid, COLOR_GREY, "* Only the driver can lock the car!");
				return 1;
			}

			for(new i=0;i<MAX_PLAYERS;i++)
				if(i != playerid) SetVehicleParamsForPlayer(GetPlayerVehicleID(playerid), i, 0, 1);

			SendClientMessage(playerid, COLOR_GREY, "* Your car is now locked.");
		}
		else SendClientMessage(playerid, COLOR_GREY, "* You must be in a car to do it.");

		return 1;
	}


	if (strcmp(cmd, "/open", true) == 0 || strcmp(cmd, "/unlock", true) == 0) {
	    tmp = dini_Get(FILE_SETTINGS, "locking");
		if (strval(tmp) == 0) {
		    SendClientMessage(playerid, COLOR_RED, "* Locking are not allowed on this server!");
		    return 1;
		}

		if(IsPlayerInAnyVehicle(playerid)) {
			if(GetPlayerState(playerid) != PLAYER_STATE_DRIVER) {
				SendClientMessage(playerid, COLOR_GREY, "* Only the driver can open the car!");
				return 1;
			}

			for(new i=0;i<MAX_PLAYERS;i++)
				if(i != playerid) SetVehicleParamsForPlayer(GetPlayerVehicleID(playerid), i, 0, 0);

			SendClientMessage(playerid, COLOR_YELLOW, "Your car is now opened.");
		}
		else SendClientMessage(playerid, COLOR_RED, "You must be in a car to do it.");

		return 1;
	}



if(strcmp(cmd, "/bank", true) == 0) {
        tmp = dini_Get(FILE_SETTINGS, "bank");
		if (strval(tmp) == 0) {
		    SendClientMessage(playerid, COLOR_RED, "RpH Bank System: Bank are not allowed on this server!");
		    return 1;
		}

	    tmp = strtok(cmdtext, idx);
	    if(!strlen(tmp)) {
			SendClientMessage(playerid, COLOR_WHITE, "Bank System: /bank [amount]");
			return 1;
	    }
	    moneys = strval(tmp);

	    if(moneys < 1) {
			SendClientMessage(playerid, COLOR_YELLOW, "Bank System: Invalid Amount");
			return 1;
		}

		if(GetPlayerMoney(playerid) < moneys) {
			moneys = GetPlayerMoney(playerid);
		}

		StatGivePlayerMoney(playerid, 0-moneys);
		bank[playerid]+=moneys;
		GetPlayerName(playerid, giveplayer, MAX_PLAYER_NAME);
		format(string, sizeof(string), "Bank System: You have deposited $%d, your current balance is $%d.", moneys, bank[playerid]);
		SendClientMessage(playerid, COLOR_YELLOW, string);

		return 1;
	}
	if(strcmp(cmd, "/withdraw", true) == 0) {
	    tmp = dini_Get(FILE_SETTINGS, "bank");
		if (strval(tmp) == 0) {
		    SendClientMessage(playerid, COLOR_RED, "Bank System: Bank are not allowed on this server!");
		    return 1;
		}

	    tmp = strtok(cmdtext, idx);
	    if(!strlen(tmp)) {
			SendClientMessage(playerid, COLOR_WHITE, "Bank System: /withdraw [amount]");
			return 1;
	    }
	    moneys = strval(tmp);

	    if(moneys < 1) {
			SendClientMessage(playerid, COLOR_YELLOW, "Bank System: You Don't Have The Money Specified.");
			return 1;
		}

	    if(moneys > bank[playerid])
	        moneys = bank[playerid];

		StatGivePlayerMoney(playerid, moneys);
		bank[playerid] -= moneys;
		GetPlayerName(playerid, giveplayer, MAX_PLAYER_NAME);
		format(string, sizeof(string), "Bank System: You have withdrawn $%d, your current balance is $%d.", moneys, bank[playerid]);
		SendClientMessage(playerid, COLOR_YELLOW, string);

		return 1;
   	}

	if(strcmp(cmd, "/bal", true) == 0) {
	    tmp = dini_Get(FILE_SETTINGS, "bank");
		if (strval(tmp) == 0) {
		    SendClientMessage(playerid, COLOR_RED, "RpH Bank System: Bank are not allowed on this server!");
		    return 1;
		}

		format(string, sizeof(string), "Bank System: You have $%d in the bank.", bank[playerid]);
		SendClientMessage(playerid, COLOR_YELLOW, string);

		return 1;
	}


if(strcmp(cmd, "/send", true) == 0) {
	    tmp = dini_Get(FILE_SETTINGS, "send");
		if (strval(tmp) == 0) {
		    SendClientMessage(playerid, COLOR_RED, "* Send are not allowed on this server!");
		    return 1;
		}

		tmp = strtok(cmdtext, idx);

		GetPlayerName(playerid, sendername, sizeof(sendername));

  		if (dini_Exists(udb_encode(sendername)) && logged[playerid] == 0) {
  		    SendClientMessage(playerid, COLOR_YELLOW, "* You must be logged in to use this.");
			return 1;
		}

		if(!strlen(tmp)) {
			SendClientMessage(playerid, COLOR_WHITE, "* USAGE: /send [playerid] [amount]");
			return 1;
		}
		giveplayerid = strval(tmp);

		tmp = strtok(cmdtext, idx);
		if(!strlen(tmp)) {
			SendClientMessage(playerid, COLOR_WHITE, "* USAGE: /send [playerid] [amount]");
			return 1;
		}
 		moneys = strval(tmp);

 		GetPlayerName(giveplayerid, giveplayer, sizeof(giveplayer));

		if (IsPlayerConnected(giveplayerid)) {
			playermoney = GetPlayerMoney(playerid);
			if (moneys > 0 && playermoney >= moneys) {
				StatGivePlayerMoney(playerid, (0 - moneys));
				StatGivePlayerMoney(giveplayerid, moneys);
				format(string, sizeof(string), "--- %s (id: %d) sent $%d to %s (id: %d).", sendername,playerid,moneys,giveplayer,giveplayerid);
				printf(string);
				format(string, sizeof(string), "* You have sent %s (id: %d), $%d.", giveplayer,giveplayerid, moneys);
				SendClientMessage(playerid, COLOR_YELLOW, string);
				format(string, sizeof(string), "* You have recieved $%d from %s (id: %d).", moneys, sendername, playerid);
				SendClientMessage(giveplayerid, COLOR_YELLOW, string);
			}
			else {
				SendClientMessage(playerid, COLOR_YELLOW, "Invalid drug amount.");
			}
		}
		else {
				format(string, sizeof(string), "%d is not an active player.", giveplayerid);
				SendClientMessage(playerid, COLOR_YELLOW, string);
		}
		return 1;
	}




           if(strcmp(cmd, "/commands", true) == 0) {
             SendClientMessage(playerid, COLOR_GREEN, "Commands Listed Below");
             SendClientMessage(playerid, COLOR_GREEN, "/login /register");
             SendClientMessage(playerid, COLOR_GREEN, "/Bank /withdraw");
             SendClientMessage(playerid, COLOR_GREEN, "/Bal(balance) /Commands");
             SendClientMessage(playerid, COLOR_GREEN, "/send(givecash)");
             return 1;
             }

        return 0;
      }
