In many free-to-play games, play will be rate limited in some manner by a resource that you naturally earn on a timed interval, but can opt to increase artificially through buy-ins.
A current example of this is the popular game Candy Crush, which refills 1 life (retry) on some set interval.
- The client should be able to accurately predict the time to the next resource, such that it might display a countdown, etc.
- Resource gain is not regular. It can be offset by, at least, resource buy-ins, or perhaps varying intervals between users.
- Resource gain is capped, and thus not continual. E.g. maximum "lives" might be 5.
- The server is the resource authority, obviously. The client might predict resource generation without actual notification as to such, but the resource count is determined server side.
Naively, one might simply implement this as a database update across the collection on the interval (if the interval is constant), but this would surely not scale. It would become more and more out of sync with the client timers as the dataset scales and the operation takes more time. Not to mention, it would be wasteful, as it's likely that only the percentage of your membership currently online needs to see realtime updates.
It seems like it would be possible to calculate the resource as a function of time since creation, time spent uncapped, resources earned, and resources spent. Would this be a viable option?
The question is, what is the best way to implement this mechanism to scale?