Cron Job Last Time Increment


#1

I am developing a simple oldschool browser game where there are cities and their productions.

Lets say that for level 1 city stone production is 1 per minute.

For level 5 city stone production is 60 per minute.

The problem is currently i have a setInterval function in server with 0 interval which is looping through db checking cities and their productions and incremeting(?) them.

So how do i increment correctly ? Since all cities has different productions per minute what is the correct aproach ? Do i need to store last increment time of each city in db then check if enough time is passed and then increment ?

Also since i am real time updating data from db to client,i would like to see incrementing 1 by 1 not +60 after 1 minute pass.


#2

My gut feeling is that updating the database every second for every user will overload it quite quickly

What you want is to calculate the resources lazily (ie. only when necessary, to perform an action)

  • On first page load, you would send the current balance of each resource and the rate of change to the client.
  • The client can increment the balance in the browser without touching the database.
  • When the user tries to do something that costs resources, the server calculates the correct balance at that time and responds.

This way the client sees a ticking number, but your server only has to do work when it matters.

The harder part is the function that calculates the current balance.
For ease I would probably save the balance with a timestamp to the db, then when it needs to calculate a new balance it can just add up the events and number of seconds since timestamp * production rate for resources and save that balance back to the db

Of course when resources are expended, you would calculate the balance, spend the resources and save the end result


#3

Oh and good luck!
I used to love those old school browser games, so it sounds like a fun project


#4

Currently i am incrementing values only in client side based on rate which i take from db but when should i write them to db what if user closes browser window i mean when actually im gonna write users resource data to db ??


#5

Only when there is an action happening.
Example:
User A has:

{
  ...
  wood: {
    amount: 100,
    at: Tue Jul 03 2018 14:46:49 GMT+0200 <use an actual timestamp>,
    increaseBy: 0.5,
  },
}

That means that User A has 100 wood and will receive another wood every two seconds.

When the user logs in and sees his dashboard, he will subscribe to this data. When the subscription is done, you can start a timer (with Meteor.setInterval(func, delay)) that counts the seconds. And every second you call a function (increaseWood()) and in that function you increase the amount of wood LOCALLY(!) that means only on the client side.

If the user wants to build something, then he will have to click on something (e.g. a picture of a wooden house) and this will trigger a Meteor.call(). In this sever function you can now get the value from the database (that is still 100 wood at 14:46:49 …) and the you can get the difference between now and the timestamp in seconds, multiply it by the increaseBy value and you have the correct amount of wood.Then you can substract the amount of wood a wooden house costs (50 wood) and then store the new value, with the new timestamp, int the database.


#6

Yeah that would be my first approach

If they close the window, the server still has the record of the last update and can calculate the difference when the page is next loaded.
So opening the page or reloading would also be considered an action that triggers a recalculation and saving to db

Also important to note that there’s probably not a right and wrong way to build this, and these are just my thoughts :slight_smile:


#7

thanks guys you helped me a lot.