0

Let’s say a person can set a limit to play a game for N minutes a day. The player may increase their limit, but the increase doesn’t take effect until the next day.

Example, person sets a limit of 10 minutes a day on Monday. They increase it on Monday to 20 minutes. For the rest of Monday, they may only play 10 minutes.

How could this active and pending limit be modeled? Is this already a solved problem?

3
  • 1
    You have Current and Next and Worker that updates Current with Next every 24h. That's all there is. Doesn't seem too difficult to be honest. Although I have a feeling that this is an XY problem. Commented Aug 18, 2024 at 16:58
  • 3
    Store a date when the pending limit takes effect, so you can query the limit based on the current date. Or have a separate process that updates the limits at midnight. There may be more solutions. Which solution to choose depends on a lot of factors that you didn’t include, that’s why someone voted to close as the question currently lacks these details. Commented Aug 18, 2024 at 17:03
  • 2
    "Is it a solved problem" is counterproductive for these kinds of questions as the contextual integration of an existing solution would be more costly than building what is a very trivial design. Rather than search for existing answers, come up with a solution yourself. It is not that hard of a problem, and given the small scale of the unknown, solving it on your own is an essential milestone on your developer journey. Hint: today's limit and the configured limit are clearly not identical at all times. But you probably already have a half-solution here regarding tracking a day's limit usage. Commented Aug 19, 2024 at 1:28

3 Answers 3

2

The easiest way is to use a count-down clock for keeping track of how much time a person has left to play that day. At midnight, the clock is reset to the limit that is then current.

If the player changes the limit during a day, then the limit value is updated, but the clock is not touched. At the start of the next day (i.e. midnight), the clock is reset to the most recently set value of the limit. This even works if the limit gets changed multiple times during a day.

5
  • Midnight? When did you play games as a kid? Reset it at noon. Commented Aug 19, 2024 at 13:21
  • @candied_orange, which timezone? UTC or UTC+12 ;-) Without joking, you can read "midnight" as shorthand for "the time of day at which the new limit gets applied." Midnight just matches better with the concept of a daily usage limiter in my mind. And even as a teen, I valued my sleep too much to have regular all-nighters. Commented Aug 19, 2024 at 13:35
  • Admirable, but hardly makes you the poster child. Think about who this is for. Commented Aug 19, 2024 at 13:39
  • @candied_orange, look at the limit that the OP talked about. This feature is for the parents that don't want their kids playing games all night, so the limit is likely low enough that it has been consumed well before midnight ;-) That said, I don't really care when exactly the reset time of the usage timer is. Commented Aug 19, 2024 at 13:47
  • Parents? OP explicitly said, "The player may increase their limit". So sure, if the parents are players. Commented Aug 19, 2024 at 14:47
2

Ok, so you model "active" and "pending" and "when" (a datetime).

One approach is that you can make a simple formula that computes the limit based on four inputs: active, pending, when, and current datetime.  Nothing further do do here — no housekeeping needed, as this formula will work into the future.  (That formula is currentDateTime <= when ? active : pending).

Then at some point need to switch over from active to pending, but only when user wants the limit is increased once again: copy pending to active (if it is pending is up to date, i.e. currentDateTime > when) and make pending the new request to increase, while setting the new when.  If pending is not currently in effect then can error: ask the user to try again, e.g. tomorrow.

So, you only need housekeeping when the user wants to update their setting — otherwise nothing to do on a daily basis.


If you want to accept as series of increases that are then to happen day over day that is another simple matter of programming involving a queue an some user interfaces to modify it.

2

A day at school or work is not the same as a day playing games.

Why don't we throw out the whole idea of a day? 24 hours. You want change the play time? Do it 24 hours ahead of time.

Otherwise I can ask for more time at 11:45, go to the bathroom, and be back at it while the toilet is still flushing.

This way the limit is always the one that was set closest to at least 24 hours ago.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.