Weekly task reset

Not favoritedFavorited Favorited 0 favourites
  • 5 posts
From the Asset Store
Chess puzzle game about checkmate. The player's task is to find the right move so that the opponent can no longer make a
  • Hello everyone,

    I've been struggling for a while to write code that resets weekly tasks. I'd like the tasks to reset every Monday, but if someone logs in on Tuesday (or any later day), the reset should still happen – and then not occur again until the following Monday.

    Below I am inserting my code, which unfortunately does not work

    Does anyone know of a better option or what's wrong here? Thanks in advance for your help!

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I think it would be easier to save the timestamp for 0:00 on the most recent Monday in Local Storage. Then, each time the game runs, check if more than 7 full days have passed since that timestamp (7×24×60×60 seconds). If so, reset the tasks and save a new timestamp — again, rounded to 0:00 on Monday.

    ChatGPT gave this formula:

    const MILLIS_IN_WEEK = 7 * 86400000;
    const MONDAY_OFFSET = 4 * 86400000;
    const mondayMidnight = Date.now() - ((Date.now() - MONDAY_OFFSET) % MILLIS_IN_WEEK);
    
    
    Date.now() - 345600000 shifts the epoch so that Monday 00:00 aligns with multiples of 604800000.
    Taking modulo % 604800000 gives the time since the last Monday midnight.
    Subtracting that gives the timestamp of the most recent Monday at 00:00, cleanly.
    

    But I think it would be for GST, you'll need to adjust it to local time zone.

  • Basically, what dop2000 said. Fortunately, the Date object provides some useful expressions for this:

    1. Get the last reset timestamp
    2. Check if the difference between current timestamp and last reset is greater than 7 days. If it is:
      1. Do the reset
      2. Set another variable to a copy of the current timestamp, call it monday
      3. Set monday to Date.ChangeDay(monday, 1)
      4. Set monday to Date.ChangeHours(monday, 0)
      5. Set monday to Date.ChangeMinutes(monday, 0)
      6. Set monday to Date.ChangeSeconds(monday, 0)
      7. Set monday to Date.ChangeMilliseconds(monday, 0)
      8. If the current day is 0, subtract 1 week
      9. Save this as your new last reset

    Let us know if that works.

  • Thank you for your replies and sorry for the late reply. I managed to do it another way (see the ss). Overall, if there was a Date.GetWeek option, it would be much simpler :)

  • The biggest problem was how to deal with the fact that someone started the game, for example, on Friday and entered again on Tuesday (skipping all Mondays), but so far the formula works well

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)