Vice City: Multiplayer

Server Development => Scripting and Server Management => Snippet Showroom => Topic started by: Anik on Dec 25, 2015, 11:04 AM

Title: [ADVANCED]Loan System saved in DB with auto deduction ( UPDATED )
Post by: Anik on Dec 25, 2015, 11:04 AM
With this snippet you will be able to take loan.

This UPDATE uses database to store loan info.
Also, if the player doesn't pay the loan in 1 week. It will be taken from his account automatically.

First add it anywhere in the script
class PStats
{
loanamount = null;
}

Now on scriptload
function onScriptLoad( )
{
status <- array( GetMaxPlayers(), null );
LoanDB <- ConnectSQL( "loandb.db" );
QuerySQL( LoanDB, "CREATE TABLE IF NOT EXISTS Loan ( Name TEXT, Amount TEXT, Time TEXT )" );
}

Now on playerjoin
function onPlayerJoin( player )
{
                // Set the cash of the player here. I didn't store the amount of cash the player have. Use your own.
status[ player.ID ] = PStats( );
local q = QuerySQL( LoanDB, "SELECT Amount, Time FROM Loan WHERE Name = '" + player.Name + "'");
if( q )
{
status[ player.ID ].loanamount = GetSQLColumnData( q, 0);
local time2 = GetSQLColumnData( q, 1 ).tointeger();
if( ( time() - time2 ) > 604800 ) //604800 = 7 days
{
QuerySQL( LoanDB, "DELETE FROM Loan WHERE Name = '" + player.Name + "'");
player.Cash -= status[ player.ID ].loanamount.tointeger();
ClientMessage ( "$"+status[ player.ID ].loanamount+" has been deducted from your account as you didn't pay the loan!", player, 255, 0, 255);
status[ player.ID ].loanamount = null;
}
}
FreeSQLQuery( q );
}

Now on Command
function onPlayerCommand( player, cmd, text )
{
switch( cmd )
{
case "loan":

local Pos = player.Pos,
checkpoly = InPoly( Pos.x, Pos.y, -898.2357,-326.6091,-898.2196,-355.5072,-936.2309,-355.5205,-939.2854,-352.5587,-952.3001,-342.9138,-957.1079,-341.7898,-966.5380,-337.4671,-966.5401,-328.1766 );
if ( text )
{
if ( IsNum( text.tostring() ) )
{
if ( status[ player.ID ].loanamount == null )
{
if( checkpoly )
{
if(text.tointeger()<= 50000 && text.tointeger()>0)
{
ClientMessage ( "You have took $"+text+" loan! Be sure to payloan before 1 week.", player, 255, 255, 0);
player.Cash += text.tointeger();
status[ player.ID ].loanamount = text.tointeger();
QuerySQL( LoanDB, "INSERT INTO Loan ( Name, Amount, Time ) VALUES ( '" + player.Name + "', "+text.tointeger()+", "+time()+" )")
}
else ClientMessage ( "Error - You can't take more than 50k as loan!.", player, 255, 0, 255);
}
else ClientMessage("Error - You need To Be At Bank to take loan.", player, 255, 0, 255 );
}
else ClientMessage("Error - You have already taken Loan!", player, 255, 0, 255 );
}
else ClientMessage("Usage: /"+cmd+" <Amount>", player, 255, 0, 255 );
}
else ClientMessage("Usage: /"+cmd+" <Amount>", player, 255, 0, 255 );

break;

case "payloan":

local Pos = player.Pos,
checkpoly = InPoly( Pos.x, Pos.y, -898.2357,-326.6091,-898.2196,-355.5072,-936.2309,-355.5205,-939.2854,-352.5587,-952.3001,-342.9138,-957.1079,-341.7898,-966.5380,-337.4671,-966.5401,-328.1766 );
if ( status[ player.ID ].loanamount != null )
{
if( checkpoly )
{
if ( player.Cash >= status[ player.ID ].loanamount.tointeger() )
{
QuerySQL( LoanDB, "DELETE FROM Loan WHERE Name = '" + player.Name + "'");
player.Cash -= status[ player.ID ].loanamount.tointeger();
ClientMessage ( "You have paid your loan of $"+status[ player.ID ].loanamount+"!", player, 255, 0, 255);
status[ player.ID ].loanamount = null;
}
else ClientMessage("Error - You need $" + status[ player.ID ].loanamount + " to pay loan.", player, 255, 0, 255 );
}
else ClientMessage("Error - You need To Be At Bank to take loan.", player, 255, 0, 255 );
}
else ClientMessage("Error - You have not taken any loan!", player, 255, 0, 255 );

break;

}
}
If you find any bug feel free to tell.
Title: Re: [ADVANCED]Loan And Payloan System
Post by: Williams on Dec 25, 2015, 11:46 AM
Great! Anik but if player take loan and he reconnect so player can take loan again without paying last last loan.
Title: Re: [ADVANCED]Loan And Payloan System
Post by: DizzasTeR on Dec 25, 2015, 01:07 PM
[Error] the index 'loan' does not exist
[Error] the index 'loanammount' does not exist
[Error] the index 'status' does not exist
Title: Re: [ADVANCED]Loan And Payloan System
Post by: KAKAN on Dec 25, 2015, 02:21 PM
Quote from: Doom_Kill3R on Dec 25, 2015, 01:07 PM[Error] the index 'loan' does not exist
[Error] the index 'loanammount' does not exist
[Error] the index 'status' does not exist
[Error] the index 'IsReg' doesn't exist
[Error] the index 'IsLogged' doesn't exist
[Error] the index 'ORANGE' doesn't exist
I can say more are there.
@Anik save them to a DB. load them when a player joins, add them when he/she takes loan, remove when they pay it off.
Title: Re: [ADVANCED]Loan And Payloan System
Post by: Anik on Dec 25, 2015, 02:59 PM
Quote from: Doom_Kill3R on Dec 25, 2015, 01:07 PM[Error] the index 'loan' does not exist
[Error] the index 'loanammount' does not exist
[Error] the index 'status' does not exist
Have you added "loan = false;" and "loanammount = 0;" in class playerstats??? As I mentioned in the topic.
Title: Re: [ADVANCED]Loan And Payloan System
Post by: Anik on Dec 25, 2015, 03:15 PM
Quote from: Williams on Dec 25, 2015, 11:46 AMGreat! Anik but if player take loan and he reconnect so player can take loan again without paying last last loan.
@Williams I know. But when they will reconnect the loan they took will also went on.
Title: Re: [ADVANCED]Loan And Payloan System
Post by: Anik on Dec 25, 2015, 03:25 PM
Quote from: KAKAN on Dec 25, 2015, 02:21 PM
Quote from: Doom_Kill3R on Dec 25, 2015, 01:07 PM[Error] the index 'loan' does not exist
[Error] the index 'loanammount' does not exist
[Error] the index 'status' does not exist
[Error] the index 'IsReg' doesn't exist
[Error] the index 'IsLogged' doesn't exist
[Error] the index 'ORANGE' doesn't exist
I can say more are there.
@Anik save them to a DB. load them when a player joins, add them when he/she takes loan, remove when they pay it off.
@KAKAN I will add it later When I get time. But 'IsLogged' and 'IsReg' if doesn't exist then add your Register and Loggedin function
Title: Re: [ADVANCED]Loan And Payloan System
Post by: KAKAN on Dec 25, 2015, 04:56 PM
Quote from: Anik on Dec 25, 2015, 03:25 PM@KAKAN I will add it later When I get time. But 'IsLogged' and 'IsReg' if doesn't exist then add your Register and Loggedin function
I very nub in scripting. Please tell me how to.

Okay okay, I add Register Loggedin, but what about the error: "The index 'status' doesn't exist'
I add it to my Class too, but it nei works
Title: Re: [ADVANCED]Loan And Payloan System
Post by: Anik on Dec 25, 2015, 05:33 PM
Just add this on scriptload
status <- array( GetMaxPlayers(), null );And this on playerjoin
status[ player.ID ] = PlayerData( player.Name );That's it. @KAKAN you know scripting better than me why then asking this kind of question?
Title: Re: [ADVANCED]Loan And Payloan System
Post by: KAKAN on Dec 25, 2015, 06:30 PM
Quote from: Anik on Dec 25, 2015, 05:33 PMJust add this on scriptload
status <- array( GetMaxPlayers(), null );And this on playerjoin
status[ player.ID ] = PlayerData( player.Name );That's it. @KAKAN you know scripting better than me why then asking this kind of question?
Do you know why this board was made for?
Well, this was made for "copy-paste-able" snips which can be added to any server script( even if a blank one ).
Your script won't work, you need to do it completely!
Like creating player class, then creating the array etc etc.
Title: Re: [ADVANCED]Loan And Payloan System
Post by: MacTavish on Dec 26, 2015, 11:54 AM
Read Me Please I Am Important Post (http://forum.vc-mp.org/?topic=1936.msg14444#msg14444)
Title: Re: [ADVANCED]Loan And Payloan System
Post by: [VM_U]Spectra.PhantoM^ on Dec 27, 2015, 04:19 AM
Quote from: Kusanagi on Dec 26, 2015, 11:54 AMRead Me Please I Am Important Post (http://forum.vc-mp.org/?topic=1936.msg14444#msg14444)
Well at least someone searches the whole vcmp forum to know 2 same posts :P. This is a hardworking Kid/Boy/Man (Sorry man i dont know how old u are :P)
Title: Re: [ADVANCED]Loan And Payloan System
Post by: [VM_U]Spectra.PhantoM^ on Dec 27, 2015, 04:23 AM
Quote from: Anik on Dec 25, 2015, 05:33 PMJust add this on scriptload
status <- array( GetMaxPlayers(), null );And this on playerjoin
status[ player.ID ] = PlayerData( player.Name );That's it. @KAKAN you know scripting better than me why then asking this kind of question?
Also anik when i add playerdata it says index playerdata doesnt exist now what?
Title: Re: [ADVANCED]Loan And Payloan System
Post by: Anik on Dec 27, 2015, 08:53 AM
Quote from: {ultimatejugo] on Dec 27, 2015, 04:23 AM
Quote from: Anik on Dec 25, 2015, 05:33 PMJust add this on scriptload
status <- array( GetMaxPlayers(), null );And this on playerjoin
status[ player.ID ] = PlayerData( player.Name );That's it. @KAKAN you know scripting better than me why then asking this kind of question?
Also anik when i add playerdata it says index playerdata doesnt exist now what?
@{ultimatejugo] what kind of noob r u??? How will it exist if u r using class Playerstats instead of class Playerdata???
USE THIS;
status[player.ID] = PlayerStats();
Title: Re: [ADVANCED]Loan And Payloan System
Post by: KAKAN on Dec 27, 2015, 08:55 AM
Quote from: Anik on Dec 27, 2015, 08:53 AMUSE THIS;
status[player.ID] = PlayerStats();
The index 'playerstats' doesn't exist -_-
Title: Re: [ADVANCED]Loan And Payloan System(UPDATED)
Post by: Anik on Dec 27, 2015, 09:02 AM
DID you add it there???
class PlayerStats
Title: Re: [ADVANCED]Loan And Payloan System(UPDATED)
Post by: Cool on Jan 01, 2016, 08:40 AM
Thanks I want to create that system but i am little weak in scripting so i have some probs in making this thanks for it
Title: Re: [ADVANCED]Loan And Payloan System(UPDATED)
Post by: Anik on Jan 01, 2016, 09:34 AM
Quote from: King on Jan 01, 2016, 08:40 AMThanks I want to create that system but i am little weak in scripting so i have some probs in making this thanks for it
it's OK. Did ufind any bug??
Title: Re: [ADVANCED]Loan And Payloan System(UPDATED)
Post by: Cool on Jan 01, 2016, 10:15 AM
No Bug but the system is not fully complete its needs to be updated
And its needs changes like islogged isreg its the 0.3 things in now very less people using
Title: Re: [ADVANCED]Loan And Payloan System(UPDATED)
Post by: W3aPoN^ on Dec 06, 2016, 02:34 AM
Hmm BUG When i Reconnect And /loan 5000 They Give Me Loan When i Reconnect 2nd Time And Do This Again They give Me Load 2nd Time instead Of You Already Took Loan LOL Please Solve Thanks
Title: Re: [ADVANCED]Loan And Payloan System(UPDATED)
Post by: Kewun on Dec 06, 2016, 03:53 AM
nice bump
Title: Re: [ADVANCED]Loan System saved in DB with auto deduction ( UPDATED )
Post by: Anik on Jan 23, 2017, 01:29 PM
Snippet UPDATED

This UPDATE uses database to store loan info.
Also, if the player doesn't pay the loan in 1 week. It will be taken from his account automatically.
Title: Re: [ADVANCED]Loan System saved in DB with auto deduction ( UPDATED )
Post by: Kewun on Jan 23, 2017, 04:36 PM
taken
What if he has 0$ ? bankrupts?
Title: Re: [ADVANCED]Loan System saved in DB with auto deduction ( UPDATED )
Post by: KAKAN on Jan 23, 2017, 04:36 PM
A bit of suggestion: You don't need a class for having just one variable.
Also, don't set the value( loanamount ) to null. As far as I know, I had some strange problems with that, it might be fixed, but why to take risk :D?
Title: Re: [ADVANCED]Loan System saved in DB with auto deduction ( UPDATED )
Post by: Anik on Jan 23, 2017, 05:18 PM
:P
Quote from: KAKAN on Jan 23, 2017, 04:36 PMA bit of suggestion: You don't need a class for having just one variable.
Also, don't set the value( loanamount ) to null. As far as I know, I had some strange problems with that, it might be fixed, but why to take risk :D?
No, I didn't face any problem by setting it to null. And I used class cuz many people use it to store players data
Title: Re: [ADVANCED]Loan System saved in DB with auto deduction ( UPDATED )
Post by: ! on Jan 24, 2017, 09:50 AM
Quote from: Kewun on Jan 23, 2017, 04:36 PMtaken
What if he has 0$ ? bankrupts?

Replace
function onPlayerJoin( player )
 {
                // Set the cash of the player here. I didn't store the amount of cash the player have. Use your own.
  status[ player.ID ] = PStats( );
  local q = QuerySQL( LoanDB, "SELECT Amount, Time FROM Loan WHERE Name = '" + player.Name + "'");
  if( q )
  {
   status[ player.ID ].loanamount = GetSQLColumnData( q, 0);
   local time2 = GetSQLColumnData( q, 1 ).tointeger();
   if( ( time() - time2 ) > 604800 ) //604800 = 7 days
   {
    QuerySQL( LoanDB, "DELETE FROM Loan WHERE Name = '" + player.Name + "'");
    player.Cash -= status[ player.ID ].loanamount.tointeger();
    ClientMessage ( "$"+status[ player.ID ].loanamount+" has been deducted from your account as you didn't pay the loan!", player, 255, 0, 255);
    status[ player.ID ].loanamount = null;   
   }
  }
  FreeSQLQuery( q );
 }
   With(Untested)
function onPlayerJoin( player )
 {
                // Set the cash of the player here. I didn't store the amount of cash the player have. Use your own.
  status[ player.ID ] = PStats( );
  local q = QuerySQL( LoanDB, "SELECT Amount, Time FROM Loan WHERE Name = '" + player.Name + "'");
  if( q )
  {
   status[ player.ID ].loanamount = GetSQLColumnData( q, 0);
   local time2 = GetSQLColumnData( q, 1 ).tointeger();
   if( ( time() - time2 ) > 604800 ) //604800 = 7 days
   {
   if ( player.Cash != 0 && player.Cash < status[ player.ID ].loanamount.tointeger() )
   {
    local loanleft = player.Cash - status[ player.ID ].loanamount.tointeger();
 QuerySQL( LoanDB, "UPDATE Loan SET Amount='" + loanleft.tointeger() + "' WHERE Name='" + player.Name + "'");
    player.Cash = 0;
    ClientMessage ( "$"+loanleft+" has been deducted from your account as you didn't pay the loan!", player, 255, 0, 255);
    status[ player.ID ].loanamount = loanleft.tointeger();   
   }

  else if ( player.Cash >= status[ player.ID ].loanamount.tointeger() )
   {
    QuerySQL( LoanDB, "DELETE FROM Loan WHERE Name = '" + player.Name + "'");
    player.Cash -= status[ player.ID ].loanamount.tointeger();
    ClientMessage ( "$"+status[ player.ID ].loanamount+" has been deducted from your account as you didn't pay the loan!", player, 255, 0, 255);
    status[ player.ID ].loanamount = null;   
   }
  }
  }
  FreeSQLQuery( q );
 }
Well its just an example now some of the players are going to say what if player deposit all of their money.
My answer is create a function which will do the same as given above and add in both on player join and on player request class.
Second option is
 to add in  deposit cmd.
To decrease spaming of msgs use this
if ( player.Cash > 1000 && player.Cash < status[ player.ID ].loanamount.tointeger() )