Bank and cash system

Started by Retard, Feb 03, 2018, 07:53 AM

Previous topic - Next topic

Retard

Everything is explained in the code.Copy paste it correct and it will work + it is tested
function onScriptLoad( )
{
// Create/load the database
  database <- ConnectSQL( "vcmp.db" );

// Create the table
QuerySQL( database, "CREATE TABLE IF NOT EXISTS money ( nick UNIQUE COLLATE NOCASE, cash INT, bank INT)" );

// Create an array to store the money information for each online player.
MoneySlot <- array( GetMaxPlayers(), null );

print( "=== Loaded SQLite Money Example ===" );
}


// Create a base class
class money {

constructor(player, icash, ibank) {

nick  = player;
cash = icash;
bank     = ibank;
}

function load(player)
{
// Create a class instance and store the object in our array.
MoneySlot[ player.ID ] = money( player, 0, 0 );

// Check for previously saved data
if ( money.find_plr(player) )
{
local query = ::QuerySQL( database, "SELECT cash, bank FROM money WHERE nick='" + player + "'" );

      if ( ::GetSQLColumnData( query, 0 ) != null )
      {         
          // Fetch the asked values from the columns of the returned row into the array index
          MoneySlot[ player.ID ].cash = ::GetSQLColumnData( query, 0 );
          MoneySlot[ player.ID ].bank = ::GetSQLColumnData( query,1 );
}

// Ask for the next row to finalize the query result
      ::GetSQLNextRow( query );
}

// sets the player's cash in-game
player.Cash = MoneySlot[ player.ID ].cash;
}

function save(player)
{
// Grab our player data from the array
local
    plr = player.Name,
        cash = MoneySlot[ player.ID ].cash,
        bank = MoneySlot[ player.ID ].bank;

  // If player had any data in the database, update the old entry
if ( money.find_plr(player) )
{
          // Format the query string using 'format'
local query = format( "UPDATE money SET cash=%i, bank=%i WHERE nick='%s'", cash, bank, plr );

          // Then execute the query
          ::QuerySQL( database, query );
      }
     
      // Else let's just make a new entry
      else
      {
          local query = format( "INSERT INTO money (nick, cash, bank) VALUES ('%s', %i, %i)", plr, cash, bank );
          ::QuerySQL( database, query );
      }
     
      // Reset the array slot
      MoneySlot[ player.ID ] = null;
}

function SetDisplay(player)
{
// Sets the players in-game money display
player.Cash = MoneySlot[ player.ID ].cash;
}

function formatInteger(number) // by Boylett
{
    local string = "";
    local newnum = number % 1000;
    while(newnum != number)
    {
      string = format(",%03d", (newnum < 0 ? -newnum : newnum)) + string;
      number /= 1000;
      newnum = number % 1000;
    }
    string = newnum.tostring() + string;
    return "$" + string;
}

function find_plr(player)
{
local query = ::QuerySQL( database, "SELECT nick FROM money WHERE nick='" + player + "'" );
return ::GetSQLColumnData( query, 0 ) != null ? true : false;
}

nick  = null;
cash = null;
bank     = null;
}

// Derived class declaration ( A derived class inherits all members and properties of it's base )
class bank extends money {

function deposit(player, amount)
{
// Amend the 'bank' property of our class instance.
MoneySlot[ player.ID ].bank += amount;

// Subtract amount from the 'cash' value.
MoneySlot[ player.ID ].cash -= amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function withdraw(player, amount)
{
// Amend the bank property value
MoneySlot[ player.ID ].bank -= amount;

// Add amount to the 'cash' value.
MoneySlot[ player.ID ].cash += amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function balance(player)
{
return MoneySlot[ player.ID ].bank;
}
}

class cash extends money {

function inc(player, amount)
{
// Amend the cash property of our class instance.
MoneySlot[ player.ID ].cash += amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function dec(player, amount)
{
// Amend the cash propery of our class instance.
MoneySlot[ player.ID ].cash -= amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function get(player)
{
return MoneySlot[ player.ID ].cash;
}
}

/*
     ------------
       EVENTS
     ------------
*/

function onPlayerChat( player, text )
{
local backupText = text;
local firstChar = backupText.slice( 0, 1 );
if (  firstChar == "!" )
{
// The following function makes any ! commands go to the OnPlayerCommand handler
local splittext = text.slice( 1 );
local params = split( splittext, " " );
if ( params.len() == 1 ) onPlayerCommand( player, params[ 0 ], "" );
else
{
splittext = splittext.slice( splittext.find( " " ) + 1 );
onPlayerCommand( player, params[ 0 ], splittext );
}
}
}

function onPlayerJoin( player )
{
    money.load( player ); // add to login function
}

function onPlayerSpawn( player )
{
    money.load( player ); // IsLoggedIn check needed
}

function onPlayerPart( player, reason )
{
    money.save( player ); // add to logout function
}
 
function onPlayerCommand( player, command, text )
{
// Identify the player
local
            plr = text != "" ? IsNum( text ) ? FindPlayer( text.tointeger() ) : FindPlayer( text ) : player,
    arg = split( text, " " );

if ( command == "money" )
{
if ( !plr ) MessagePlayer( "Player " + text + " is not online.", player );
else
{
local cash  =  cash.get( plr );
local bank  =  bank.balance( plr );
MessagePlayer( plr.Name + "'s money:" + " Cash: " + money.formatInteger(cash) + ", Bank: " + money.formatInteger(bank), player );
}
}
else if ( command == "deposit" )
{
// var holding parameter 0 as an integer (chat-box holds it as a string)
local iarg = arg.len() > 0 ? IsNum( arg[ 0 ] ) ? arg[ 0 ].tointeger() : arg[ 0 ] : null;

if ( arg.len() == 0 ) MessagePlayer( "no params", player );
else if ( !IsNum( arg[ 0 ] ) )  MessagePlayer( "Invalid Amount!", player );
else if ( iarg > cash.get( player ) ) MessagePlayer( "You cannot deposit more than you have!", player );
else
{
bank.deposit( player, iarg );
MessagePlayer( " You Deposited " + money.formatInteger(iarg), player );
}
}
else if ( command == "withdraw" )
{
// var holding parameter 0 as an integer (chat-box holds it as a string)
local iarg = arg.len() > 0 ? IsNum( arg[ 0 ] ) ? arg[ 0 ].tointeger() : arg[ 0 ] : null;

if ( arg.len() == 0 ) MessagePlayer( "no params", player );
else if ( !IsNum( arg[ 0 ] ) )  MessagePlayer( "Invalid Amount!", player );
else if ( iarg > bank.balance( player ) ) MessagePlayer( "You cannot withdraw more than you have!", player );
else
{
bank.withdraw( player, iarg );
MessagePlayer( " You withdrew " + money.formatInteger(iarg), player );
}
}
else if ( command == "payday" )
{
cash.inc( player, 500000 ); // looking forward to driving that ferrari!!
MessagePlayer( " Pay day arrived, check your cash with !money", player );
}
}

Mohamed Boubekri

Quote from: Ali Ahmed on Feb 03, 2018, 07:53 AMEverything is explained in the code.Copy paste it correct and it will work + it is tested
function onScriptLoad( )
{
// Create/load the database
  database <- ConnectSQL( "vcmp.db" );

// Create the table
QuerySQL( database, "CREATE TABLE IF NOT EXISTS money ( nick UNIQUE COLLATE NOCASE, cash INT, bank INT)" );

// Create an array to store the money information for each online player.
MoneySlot <- array( GetMaxPlayers(), null );

print( "=== Loaded SQLite Money Example ===" );
}


// Create a base class
class money {

constructor(player, icash, ibank) {

nick  = player;
cash = icash;
bank     = ibank;
}

function load(player)
{
// Create a class instance and store the object in our array.
MoneySlot[ player.ID ] = money( player, 0, 0 );

// Check for previously saved data
if ( money.find_plr(player) )
{
local query = ::QuerySQL( database, "SELECT cash, bank FROM money WHERE nick='" + player + "'" );

      if ( ::GetSQLColumnData( query, 0 ) != null )
      {         
          // Fetch the asked values from the columns of the returned row into the array index
          MoneySlot[ player.ID ].cash = ::GetSQLColumnData( query, 0 );
          MoneySlot[ player.ID ].bank = ::GetSQLColumnData( query,1 );
}

// Ask for the next row to finalize the query result
      ::GetSQLNextRow( query );
}

// sets the player's cash in-game
player.Cash = MoneySlot[ player.ID ].cash;
}

function save(player)
{
// Grab our player data from the array
local
    plr = player.Name,
        cash = MoneySlot[ player.ID ].cash,
        bank = MoneySlot[ player.ID ].bank;

  // If player had any data in the database, update the old entry
if ( money.find_plr(player) )
{
          // Format the query string using 'format'
local query = format( "UPDATE money SET cash=%i, bank=%i WHERE nick='%s'", cash, bank, plr );

          // Then execute the query
          ::QuerySQL( database, query );
      }
     
      // Else let's just make a new entry
      else
      {
          local query = format( "INSERT INTO money (nick, cash, bank) VALUES ('%s', %i, %i)", plr, cash, bank );
          ::QuerySQL( database, query );
      }
     
      // Reset the array slot
      MoneySlot[ player.ID ] = null;
}

function SetDisplay(player)
{
// Sets the players in-game money display
player.Cash = MoneySlot[ player.ID ].cash;
}

function formatInteger(number) // by Boylett
{
    local string = "";
    local newnum = number % 1000;
    while(newnum != number)
    {
      string = format(",%03d", (newnum < 0 ? -newnum : newnum)) + string;
      number /= 1000;
      newnum = number % 1000;
    }
    string = newnum.tostring() + string;
    return "$" + string;
}

function find_plr(player)
{
local query = ::QuerySQL( database, "SELECT nick FROM money WHERE nick='" + player + "'" );
return ::GetSQLColumnData( query, 0 ) != null ? true : false;
}

nick  = null;
cash = null;
bank     = null;
}

// Derived class declaration ( A derived class inherits all members and properties of it's base )
class bank extends money {

function deposit(player, amount)
{
// Amend the 'bank' property of our class instance.
MoneySlot[ player.ID ].bank += amount;

// Subtract amount from the 'cash' value.
MoneySlot[ player.ID ].cash -= amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function withdraw(player, amount)
{
// Amend the bank property value
MoneySlot[ player.ID ].bank -= amount;

// Add amount to the 'cash' value.
MoneySlot[ player.ID ].cash += amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function balance(player)
{
return MoneySlot[ player.ID ].bank;
}
}

class cash extends money {

function inc(player, amount)
{
// Amend the cash property of our class instance.
MoneySlot[ player.ID ].cash += amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function dec(player, amount)
{
// Amend the cash propery of our class instance.
MoneySlot[ player.ID ].cash -= amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function get(player)
{
return MoneySlot[ player.ID ].cash;
}
}

/*
     ------------
       EVENTS
     ------------
*/

function onPlayerChat( player, text )
{
local backupText = text;
local firstChar = backupText.slice( 0, 1 );
if (  firstChar == "!" )
{
// The following function makes any ! commands go to the OnPlayerCommand handler
local splittext = text.slice( 1 );
local params = split( splittext, " " );
if ( params.len() == 1 ) onPlayerCommand( player, params[ 0 ], "" );
else
{
splittext = splittext.slice( splittext.find( " " ) + 1 );
onPlayerCommand( player, params[ 0 ], splittext );
}
}
}

function onPlayerJoin( player )
{
    money.load( player ); // add to login function
}

function onPlayerSpawn( player )
{
    money.load( player ); // IsLoggedIn check needed
}

function onPlayerPart( player, reason )
{
    money.save( player ); // add to logout function
}
 
function onPlayerCommand( player, command, text )
{
// Identify the player
local
            plr = text != "" ? IsNum( text ) ? FindPlayer( text.tointeger() ) : FindPlayer( text ) : player,
    arg = split( text, " " );

if ( command == "money" )
{
if ( !plr ) MessagePlayer( "Player " + text + " is not online.", player );
else
{
local cash  =  cash.get( plr );
local bank  =  bank.balance( plr );
MessagePlayer( plr.Name + "'s money:" + " Cash: " + money.formatInteger(cash) + ", Bank: " + money.formatInteger(bank), player );
}
}
else if ( command == "deposit" )
{
// var holding parameter 0 as an integer (chat-box holds it as a string)
local iarg = arg.len() > 0 ? IsNum( arg[ 0 ] ) ? arg[ 0 ].tointeger() : arg[ 0 ] : null;

if ( arg.len() == 0 ) MessagePlayer( "no params", player );
else if ( !IsNum( arg[ 0 ] ) )  MessagePlayer( "Invalid Amount!", player );
else if ( iarg > cash.get( player ) ) MessagePlayer( "You cannot deposit more than you have!", player );
else
{
bank.deposit( player, iarg );
MessagePlayer( " You Deposited " + money.formatInteger(iarg), player );
}
}
else if ( command == "withdraw" )
{
// var holding parameter 0 as an integer (chat-box holds it as a string)
local iarg = arg.len() > 0 ? IsNum( arg[ 0 ] ) ? arg[ 0 ].tointeger() : arg[ 0 ] : null;

if ( arg.len() == 0 ) MessagePlayer( "no params", player );
else if ( !IsNum( arg[ 0 ] ) )  MessagePlayer( "Invalid Amount!", player );
else if ( iarg > bank.balance( player ) ) MessagePlayer( "You cannot withdraw more than you have!", player );
else
{
bank.withdraw( player, iarg );
MessagePlayer( " You withdrew " + money.formatInteger(iarg), player );
}
}
else if ( command == "payday" )
{
cash.inc( player, 500000 ); // looking forward to driving that ferrari!!
MessagePlayer( " Pay day arrived, check your cash with !money", player );
}
}
Lol
| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].

Retard

Quote from: Mohamed on Feb 03, 2018, 09:46 AM
Quote from: Ali Ahmed on Feb 03, 2018, 07:53 AMEverything is explained in the code.Copy paste it correct and it will work + it is tested
function onScriptLoad( )
{
// Create/load the database
  database <- ConnectSQL( "vcmp.db" );

// Create the table
QuerySQL( database, "CREATE TABLE IF NOT EXISTS money ( nick UNIQUE COLLATE NOCASE, cash INT, bank INT)" );

// Create an array to store the money information for each online player.
MoneySlot <- array( GetMaxPlayers(), null );

print( "=== Loaded SQLite Money Example ===" );
}


// Create a base class
class money {

constructor(player, icash, ibank) {

nick  = player;
cash = icash;
bank     = ibank;
}

function load(player)
{
// Create a class instance and store the object in our array.
MoneySlot[ player.ID ] = money( player, 0, 0 );

// Check for previously saved data
if ( money.find_plr(player) )
{
local query = ::QuerySQL( database, "SELECT cash, bank FROM money WHERE nick='" + player + "'" );

      if ( ::GetSQLColumnData( query, 0 ) != null )
      {         
          // Fetch the asked values from the columns of the returned row into the array index
          MoneySlot[ player.ID ].cash = ::GetSQLColumnData( query, 0 );
          MoneySlot[ player.ID ].bank = ::GetSQLColumnData( query,1 );
}

// Ask for the next row to finalize the query result
      ::GetSQLNextRow( query );
}

// sets the player's cash in-game
player.Cash = MoneySlot[ player.ID ].cash;
}

function save(player)
{
// Grab our player data from the array
local
    plr = player.Name,
        cash = MoneySlot[ player.ID ].cash,
        bank = MoneySlot[ player.ID ].bank;

  // If player had any data in the database, update the old entry
if ( money.find_plr(player) )
{
          // Format the query string using 'format'
local query = format( "UPDATE money SET cash=%i, bank=%i WHERE nick='%s'", cash, bank, plr );

          // Then execute the query
          ::QuerySQL( database, query );
      }
     
      // Else let's just make a new entry
      else
      {
          local query = format( "INSERT INTO money (nick, cash, bank) VALUES ('%s', %i, %i)", plr, cash, bank );
          ::QuerySQL( database, query );
      }
     
      // Reset the array slot
      MoneySlot[ player.ID ] = null;
}

function SetDisplay(player)
{
// Sets the players in-game money display
player.Cash = MoneySlot[ player.ID ].cash;
}

function formatInteger(number) // by Boylett
{
    local string = "";
    local newnum = number % 1000;
    while(newnum != number)
    {
      string = format(",%03d", (newnum < 0 ? -newnum : newnum)) + string;
      number /= 1000;
      newnum = number % 1000;
    }
    string = newnum.tostring() + string;
    return "$" + string;
}

function find_plr(player)
{
local query = ::QuerySQL( database, "SELECT nick FROM money WHERE nick='" + player + "'" );
return ::GetSQLColumnData( query, 0 ) != null ? true : false;
}

nick  = null;
cash = null;
bank     = null;
}

// Derived class declaration ( A derived class inherits all members and properties of it's base )
class bank extends money {

function deposit(player, amount)
{
// Amend the 'bank' property of our class instance.
MoneySlot[ player.ID ].bank += amount;

// Subtract amount from the 'cash' value.
MoneySlot[ player.ID ].cash -= amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function withdraw(player, amount)
{
// Amend the bank property value
MoneySlot[ player.ID ].bank -= amount;

// Add amount to the 'cash' value.
MoneySlot[ player.ID ].cash += amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function balance(player)
{
return MoneySlot[ player.ID ].bank;
}
}

class cash extends money {

function inc(player, amount)
{
// Amend the cash property of our class instance.
MoneySlot[ player.ID ].cash += amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function dec(player, amount)
{
// Amend the cash propery of our class instance.
MoneySlot[ player.ID ].cash -= amount;

// Set players in-game money display.
money.SetDisplay(player);
}
function get(player)
{
return MoneySlot[ player.ID ].cash;
}
}

/*
     ------------
       EVENTS
     ------------
*/

function onPlayerChat( player, text )
{
local backupText = text;
local firstChar = backupText.slice( 0, 1 );
if (  firstChar == "!" )
{
// The following function makes any ! commands go to the OnPlayerCommand handler
local splittext = text.slice( 1 );
local params = split( splittext, " " );
if ( params.len() == 1 ) onPlayerCommand( player, params[ 0 ], "" );
else
{
splittext = splittext.slice( splittext.find( " " ) + 1 );
onPlayerCommand( player, params[ 0 ], splittext );
}
}
}

function onPlayerJoin( player )
{
    money.load( player ); // add to login function
}

function onPlayerSpawn( player )
{
    money.load( player ); // IsLoggedIn check needed
}

function onPlayerPart( player, reason )
{
    money.save( player ); // add to logout function
}
 
function onPlayerCommand( player, command, text )
{
// Identify the player
local
            plr = text != "" ? IsNum( text ) ? FindPlayer( text.tointeger() ) : FindPlayer( text ) : player,
    arg = split( text, " " );

if ( command == "money" )
{
if ( !plr ) MessagePlayer( "Player " + text + " is not online.", player );
else
{
local cash  =  cash.get( plr );
local bank  =  bank.balance( plr );
MessagePlayer( plr.Name + "'s money:" + " Cash: " + money.formatInteger(cash) + ", Bank: " + money.formatInteger(bank), player );
}
}
else if ( command == "deposit" )
{
// var holding parameter 0 as an integer (chat-box holds it as a string)
local iarg = arg.len() > 0 ? IsNum( arg[ 0 ] ) ? arg[ 0 ].tointeger() : arg[ 0 ] : null;

if ( arg.len() == 0 ) MessagePlayer( "no params", player );
else if ( !IsNum( arg[ 0 ] ) )  MessagePlayer( "Invalid Amount!", player );
else if ( iarg > cash.get( player ) ) MessagePlayer( "You cannot deposit more than you have!", player );
else
{
bank.deposit( player, iarg );
MessagePlayer( " You Deposited " + money.formatInteger(iarg), player );
}
}
else if ( command == "withdraw" )
{
// var holding parameter 0 as an integer (chat-box holds it as a string)
local iarg = arg.len() > 0 ? IsNum( arg[ 0 ] ) ? arg[ 0 ].tointeger() : arg[ 0 ] : null;

if ( arg.len() == 0 ) MessagePlayer( "no params", player );
else if ( !IsNum( arg[ 0 ] ) )  MessagePlayer( "Invalid Amount!", player );
else if ( iarg > bank.balance( player ) ) MessagePlayer( "You cannot withdraw more than you have!", player );
else
{
bank.withdraw( player, iarg );
MessagePlayer( " You withdrew " + money.formatInteger(iarg), player );
}
}
else if ( command == "payday" )
{
cash.inc( player, 500000 ); // looking forward to driving that ferrari!!
MessagePlayer( " Pay day arrived, check your cash with !money", player );
}
}
Lol
why
Quotelol

Mohamed Boubekri

Good Job ;D
But The Cash Save System Its Already Posted.
I Think Fuzzie Account System Also Save Cash And Bank + Kills And Deaths.
Anyways Good Luck. ;D
| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].

Retard

Quote from: Mohamed on Feb 03, 2018, 03:13 PMGood Job ;D
But The Cash Save System Its Already Posted.
I Think Fuzzie Account System Also Save Cash And Bank + Kills And Deaths.
Anyways Good Luck. ;D
Everyone doesnt uses fuzzies account system.people uses aniks too and this has bank system too + this one is scripted in a different way

Mohamed Boubekri

i'm Using Fuzzie Account System Lol
I Know Many Using Fuzzie Account.
Lol ya ;D
| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].

Retard

Quote from: Mohamed on Feb 03, 2018, 06:24 PMi'm Using Fuzzie Account System Lol
I Know Many Using Fuzzie Account.
Lol ya ;D
You use , People use , but everyone doesnt use

!

Quote from: Mohamed on Feb 03, 2018, 06:24 PMi'm Using Fuzzie Account System Lol
I Know Many Using Fuzzie Account.
Lol ya ;D
With respect all would be your mates just like you ::)

Discord: zeus#5155

=RK=MarineForce

Thank you but you can save it on my server stats system and cash and buy wep costQ! Plese