# Efficiently calculating increase in money over a period of time

## Recommended Posts

I'm currently working on the server side logic of a real-time online browser game build using NodeJS. Currently the logic works like this...

1. Receive an event from a player (queue something for production)

2. Calculate when the next event will happen in the game (look at when the next production will complete or when the next research project will finish)

3. Set a timeout so that the server effectively sleeps until the next event happens (unless interrupted by a player sending another event)

This seems to work very well so far. When the server wakes up from the timeout it updates the game with the amount of time passed and all production and research etc. is updated by the ellapsed time.

The problem I'm having at the moment is that the amount of money generated for a player is based on how many workers they have. Worker numbers slowly increase over time until they reach a population cap. Given the following facts...

- The player has 1 worker

- The player has no money

- A new working is created every minute

- A worker produces 100 credits per minute

How many credits does the user have after an hour? I have no idea how to predict the number of credits produced as the number of workers increases over time

I hope that makes sense!

#### Share this post

##### Share on other sites

Oh hell, I can't remember the proper name for it, but it's done with sigma notation.

Edit:

http://en.wikipedia.org/wiki/Summation

The formula for a sequence starting at 1 is

s = n(n+1) / 2

For sequences starting at numbers higher than one it's:

s = (n(n+1) / 2) - (m(m+1) / 2)

Where m is one less than the starting point for the sequence and n is the ending point.

For instance:

1+2+3+4+5 = 15

1+2 = 3 +3 = 6 +4 = 10 +5 = 15

vs

5(5+1) / 2

5(6) / 2

30 / 2

15

or

3+4+5

3+4 = 7 +5 = 12

vs

5(5+1) / 2 = 15

2(2+1) / 2 = 3

15 - 3 = 12

Edited by Khatharr

#### Share this post

##### Share on other sites

I'm not really seeing how to apply this to my problem in a more general manner. Your solution does provide the answer for 1 hour where the increase in workers is 1p/m. How to I alter this to handle 2,3,4 hours or a worker increase of 2 p/m?

#### Share this post

##### Share on other sites
What are all the possible parameter settings in the problem?

#### Share this post

##### Share on other sites

The player has a known number of workers (eg. 10, 20, 123)

Each worker will produce 100 credits per minute

A new worker is generated periodically (eg. every 1 minute, every 1.5 minutes, every 30 minutes) based on other variables

A period of time passed (eg. 1 minute, 4 minutes, 2034 minutes)

How to I calculate how many credits have been produced in the time period?

example:

The player has 20 workers

The worker spawn rate is 1 worker every 2 minutes

20 minutes have passed since the last update

How many credits have been generated in this time frame

#### Share this post

##### Share on other sites
This is untested, but at first glance it would be:

m = starting_population
n = m + cycles_elapsed
worker_cycles = summation(m, n) * workers_spawned_per_cycle
produced = worker_cycles * production_per_worker_cycle

#### Share this post

##### Share on other sites

example:

The player has 20 workers

The worker spawn rate is 1 worker every 2 minutes

20 minutes have passed since the last update

How many credits have been generated in this time frame

You need to be much more precise than that. For instance, does a worker produce 100 credits the minute that it is spawned? Or is it one minute after? In your example, when is the next worker going to be produced? Right away, or in one minute, or in two minutes?

I suggest you write reference code that iterates over minutes (or whatever other time unit you want) and computes things in a naive way. You can then try to optimize that code by using summation formulas. But you have to know what it is you are trying to compute.

#### Share this post

##### Share on other sites

Given the following facts...

- The player has 1 worker
- The player has no money
- A new working is created every minute
- A worker produces 100 credits per minute

How many credits does the user have after an hour?

The complex code isn't necessary.  Unless you have some serious problems, you could do something along these lines:

if(timeElapsed > kMaxElapsedTime)
{
ShowMessage( Messages::TooMuchTimeElapsed );
timeElapsed = kMaxTimeElapsed;
}
for(int i=0; i<timeElapsed; i++)
{
SimulateOneTimeUnit();
}



If you need more, create two simulators.  One is an online simulator, the other an offline simulator.

Edited by frob

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now