[ADVANCED]Loan System saved in DB with auto deduction ( UPDATED )

Started by Anik, Dec 25, 2015, 11:04 AM

Previous topic - Next topic

Anik

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.

Williams

Great! Anik but if player take loan and he reconnect so player can take loan again without paying last last loan.
Hey, i m always at IRC and Forum you can comtact me at IRC channel :- #GDM @LUNet and Forum :- http://pro-fighter.tk/index.php

DizzasTeR

[Error] the index 'loan' does not exist
[Error] the index 'loanammount' does not exist
[Error] the index 'status' does not exist

KAKAN

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.
oh no

Anik

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.

Anik

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.

Anik

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

KAKAN

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
oh no

Anik

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?

KAKAN

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.
oh no

MacTavish


Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

[VM_U]Spectra.PhantoM^

Quote from: Kusanagi on Dec 26, 2015, 11:54 AMRead Me Please I Am Important Post
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)
Wanna Talk To Me? Come At Irc #Jugo@LUNet

[VM_U]Spectra.PhantoM^

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?
Wanna Talk To Me? Come At Irc #Jugo@LUNet

Anik

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();

KAKAN

Quote from: Anik on Dec 27, 2015, 08:53 AMUSE THIS;
status[player.ID] = PlayerStats();
The index 'playerstats' doesn't exist -_-
oh no