Created
July 21, 2014 13:41
-
-
Save diegoeche/9ee02beac884a29125b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def updateResourceProgress(state: State, timestamp: Long): State = { | |
val secondsSinceLastUpdate = (timestamp - state.metaData.lastUpdate) | |
logger.debug(s"secondsSinceLastLogin $secondsSinceLastUpdate") | |
val updatedResources: Map[String, Resource] = state.resources.map { | |
case (resourceName, resource) => { | |
val modifier = resource.productionRate * resource.rateModifier / 1000 | |
val productionRate = resource.productionRate + modifier | |
val addedResources = (productionRate.toFloat / 3600.toFloat * secondsSinceLastUpdate).ceil.toLong | |
logger.debug(s"adding $addedResources to $resourceName for $secondsSinceLastUpdate s - ${productionRate}/h") | |
(resourceName -> resource.copy(stock = min(resource.stock + addedResources, resource.cap))) | |
} | |
} | |
state.copy(resources = updatedResources). | |
copy(metaData = state.metaData.copy(lastUpdate = (System.currentTimeMillis / 1000))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment