I am working on a browser game where there are citys and their resource productions. Each player has their own production rate for each resource.Lets say stone as example. I have production rate,capacity and timestamp(when production started for that resource) for stone in db.I am pulling production rate from db and updating production value on client(incrementing it) but not in db since updating db value each second is not a good idea.So each time user refreshes page or does some action releated to stone,server checks timestamp and production rate and calculates what should be the value of stone in that time,i mean real value and updates client.
"stone" : {
"capacity" : 10,
"count" : 0,
"timestamp" : 1531470433, // when production started.
"rate" : 20, //stone production in a minute
}
This calculates users current stone value and updates db.
var allStoneProduction= Math.floor((currentTime - stoneDbTime) * stoneProductionRate /60)
The problem is with capacity.Players are able to increase capacity of each resource.
I can detect when stone capacity going to be full in server since i know production rate and production start time.Then i am making stonecount equal to capacity.
This code checks if storage is full.If its not it sets value of stone production.
var storageFullTime = stoneDbTime+(stoneCapacity/stoneProductionRate)*60
if(currentTime < storageFullTime || currentTime == storageFullTime){
console.log("storage is full!")
Resources.update({ownerId:userid},{$set:{"stone.count":stoneCapacity}})
}else{
Resources.update({ownerId:userid},{$set:{"stone.count":allStoneProduction}})
}
As you see allStoneProduction is actually always going up without checking capacity since its only based on rate and start timestamp.This is ok if there is no max capacity.
If stone count reaches its capacity i stop incrementing on client side by checking stonecount,stonecapacity.(10/10) lets say.
If i change capacity from 10 to 20 on db,client gets new capacity value and starts to increment it 11,12,13... until capacity 20.
But server cant follow up this.Should i also write timestamp of when capacity changed ? I couldnt really write the correct if else logic for this.
So how do i compute this capacity change in server side ? allStoneProduction
value doesnt care about reaching max capacity and increasing capacity.So if change capacity and refresh page,i get a wrong stone count.(since in server logic production never stops)