Jump to content
MakeWebGames

Change the bank interest without crons?


Recommended Posts

So.... i came across the post Cron-less Energy/Will/Brave/HP Regen + Admin Panel by Cronus2(link below)

 

And i was wondering if it would be possible to make or do something like that for Banks and their interest system?

Being able to allow admins to make adjustments to bank interest for donators and non-donators

 

I am asking because i have no idea if it can be done nor where to start to really figure that out, i do not have the knowledge on those kind of details yet nor have that great of a understanding of the crons or timestamps yet

 

 

LINK: 

 

Link to comment
Share on other sites

I think I did something similar in a mc2 game, I think I used a cron like if donator then interest = if non donator interest =. In terms of a cronless cron in MC2 that is an interesting point and I suppose it could be done. In terms of GL I would create new settings so they can be updated via the admin panel and in the code do a if this then else do that for showing the interest rate. In the hook file I think I'd do something similar to determine which interest rate applied.

Link to comment
Share on other sites

1 hour ago, Canjucks said:

I think I did something similar in a mc2 game, I think I used a cron like if donator then interest = if non donator interest =. In terms of a cronless cron in MC2 that is an interesting point and I suppose it could be done. In terms of GL I would create new settings so they can be updated via the admin panel and in the code do a if this then else do that for showing the interest rate. In the hook file I think I'd do something similar to determine which interest rate applied.

lol, yeah... i know how to do the IF in the crons and stuff but i would like to be able to changes to the rate/percentage without having to edit so much for all the calculations

 

so i was thinking having someway to do it in an admin panel would be something new, different and would help alot with keeping things in order.... at least it gave you something new aswell

Link to comment
Share on other sites

5 hours ago, MajikalJoker said:

lol, yeah... i know how to do the IF in the crons and stuff but i would like to be able to changes to the rate/percentage without having to edit so much for all the calculations

 

so i was thinking having someway to do it in an admin panel would be something new, different and would help alot with keeping things in order.... at least it gave you something new aswell

Wasn't meaning to do any help of IFs just playing out what I did in the past. But yes will be something different and you should post your solution if you get it working.

Link to comment
Share on other sites

You can do it all through the header file using timestamps. 

Set up your queries for players who have donated and non-donated designation for the bank.

Then, when the game time gets to the first timestamp you set for the interest to be added it will run.

Just make sure that when you do your update queries for this, you also include the updated timestamp for 24 hours later.

 

 

  • Like 2
Link to comment
Share on other sites

1 hour ago, newttster said:

You can do it all through the header file using timestamps. 

Set up your queries for players who have donated and non-donated designation for the bank.

Then, when the game time gets to the first timestamp you set for the interest to be added it will run.

Just make sure that when you do your update queries for this, you also include the updated timestamp for 24 hours later.

 

 

From what i am hearing is it is possible to be done.... just gotta learn about timestamps more and understand how to implement it, Thank You!

 

any recommendations on where to start?

Link to comment
Share on other sites

1 hour ago, MajikalJoker said:

From what i am hearing is it is possible to be done.... just gotta learn about timestamps more and understand how to implement it, Thank You!

 

any recommendations on where to start?

There's a lot to it but I'll give you the general outline to help you.

 

1. You'll need to create or append a settings table with the value you want for interest for donators and non-donators

2. You need a place to store the "last run" timestamp...id highly recommend a new field in the users table and handle all users individually, but you could also have one global timestamp

(I'll be explaining as if you're doing the user table route from now on for ease of explanation)

3. Whenever you fetch the user run checks against the last run timestamp. For the timestamp if you have a 0 set the original to floor(time()/86400)*86400 to make it happen right at midnight server time. Want it every 24 hours? Do the following check for example

 

if(timeNow-timestamp > 86400{

//Run it but store the above different so you know how many times to run it.

//Increase the saved last run by 86400*timeToRun

}

 

3a. If you have a users class it's best to put it there. If not put it in the main file (not sure what it's called) where you fetch the user id cookie or whatever or wherever you fetch the user information. You can fetch just the timestamp with a query and run the checks mentioned above

 

Hopefully that makes sense and helps

Just now, gamble said:

 

(Sorry on mobile don't know how to delete quote k meant to edit)

 

Edit:

I meant to mention I recommend you store and handle each user separately for the sake of speed. Easier to update one at a time as needed vs 300000 at once when there's only one user online if that makes sense? Only do what's needed rather than have one handle everyone 

Edited by gamble
Additional info
Link to comment
Share on other sites

Crons are really not needed when simple arithmetic covers it nicely. You could, for example, have different rates per user by storing the APR (annual percentage rate) and the date deposited in the user table. After that, it's just simple case of basic arithmetic to determine the PV (present value).

Example: -- assuming the date deposited is a integer from the PHP function time():

$principal = $ir["bankmoney"];
$apr = $ir["apr"];

$daysSinceDeposited = (time() - $ir["deposited"]) / 86400;  /* 86400 seconds in a day */
$dailyRate = pow(1 + $apr / 100, 1 / 365.2425);             /* 365.2425 days in a year */

$presentValue = pow($dailyRate, $daysSinceDeposited) * $principal;

So assuming an APR of 40%, a principal of $100, after 10 days we'd have a computer present value of $100.925, after 100 days it would be $109.650, after 1 year it would be $140.00 (exactly 40% APR).

Taking money out of the bank would necessitate computing the present value, subtracting the withdrawal amount and placing the result in your bankmoney field, deposits are done by storing the present value plus the deposit amount in the bankmoney field.

Edited by Jax
Adjusted # of days in year
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

window.onload = function() {
    // Call your function immediately if needed
    executeQuery();

    // Then set it to run every 5 minutes
    setInterval(function() {
        executeQuery();
    }, 300000); // 300000 milliseconds = 5 minutes
};

function executeQuery() {
    console.log("Query executed at: " + new Date().toLocaleTimeString());
    // Your query or code goes here
}
 

Edited by rockwood
Link to comment
Share on other sites

  • 5 months later...
On 2/29/2024 at 5:30 PM, newttster said:

You can do it all through the header file using timestamps. 

Set up your queries for players who have donated and non-donated designation for the bank.

Then, when the game time gets to the first timestamp you set for the interest to be added it will run.

Just make sure that when you do your update queries for this, you also include the updated timestamp for 24 hours later.

 

 

this is the perfect way to do it and wouldn't take much code

Link to comment
Share on other sites

On 8/24/2024 at 8:53 PM, SwiftGameR said:

this is the perfect way to do it and wouldn't take much code

Exactly!

The way I have mine done is by setting up a separate table ... "intpayable_inprogress".

I use the header file to pull the needed info from that table and based off of the time needed for the action to happen and the UNIX_TIMESTAMP() being > than the time to be implemented and userid being the unique identifier, you run your queries. 

Because the userid is used as the identifier, the "intpayable_inprogress" table should not have any more rows than the users table. Assuming interest is paid once every 24 hours, the table will be updated in your queries.

$interest= $db->query("SELECT * FROM  intpayable_inprogress WHERE UNIX_TIMESTAMP() > endtime and userid = {$ir['userid']}");
    while($intpaid= $db->fetch_row($interest)) 

{

$intamountfordon=$ir['money'] /100 * 3; // 3 being the amount of interest being paid to donator

$intamountfornon=$ir['money'] /100 * 2; // 2 being the amount of interest being paid to non-donator

$time = (time() + 86400); // 24 hour variable

      if ($int['donator'] >= 1) //verifies if user is a donator, this info should be a part of your intpayable_inprogress table

    { 

           $db->query("UPDATE users SET money= money+ {$intamountfordon} WHERE userid = {$ir['userid']}");

           $db->query("UPDATE intpayable SET endtime = {$time} WHERE userid = {$ir['userid']}");

           event_add($ir['userid'], "Congratulations! You have earned {$intamountfordon} in interest today.");

    }

      if ($int['nondonator'] <=0) //verifies if user is a non-donator, this info should be a part of your intpayable_inprogress table

    { 

           $db->query("UPDATE users SET money= money+ {$intamountfornon} WHERE userid = {$ir['userid']}");

           $db->query("UPDATE intpayable SET endtime = {$time} WHERE userid = {$ir['userid']}");

           event_add($ir['userid'], "Congratulations! You have earned {$intamountfornon} in interest today.");

    }

}  //closes while loop

 

Table name, variables, etc., should obviously match the necessary information in your game. To make it as simple as possible, I've based the interest on the current amount of money the user has, regardless of deposits and withdrawals that have taken place in the last 24 hours. If you want to factor that information in, you can go right ahead and work out the math on that. 

I use the header.php file to do all of this because 1) it reduces the load on the server and 2) the action only takes place once the user is online and the 24 hours has passed. If the user hasn't been online for 3 days, there are ways to compensate for that. However, to encourage players to log on at least once a day, I make it clear the interest is only paid under the condition that 24 hours have passed and that they are online to receive it. They'll know when their 24 hours is due based on the last event notification they received. 

If you want it to run daily, then you'd have to set the time to pay out at a set Timestamp daily, regardless if the player is active and you'd have to set the query based on all user ids as opposed to the individual.

This is fairly basic but it gives you a good head start on what you're looking for. Hope it helps.

 

Edited to add: You can also use the same idea to notify players if they are in jail, the hospital, the clan is at war, etc. by using an echo statement. 

 

 

 

 

Edited by newttster
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...