Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    80
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Game Timer & Long Tasks on Interval

    I'm designing a 'Game' which will do a set number of tasks at the end of each month. I don't want a user to stall the month by refreshing the page so my approach is going to be

    1. JS Interval every second to update a PHP Cookie incrementing an integer
    2. When that Cookie integer gets to say 150 a new month rolls over.
    3. When that month rolls over it calls an array of PHP pages to run those scripts.

    The idea that while waiting for a month to end, if the user refreshes the page it'll pick up where the last cookie was.

    My questions are

    1. Is this the best way to wait for a month.
    2. How can I redirect a page at the end of the month - I don't want it to be abrupt in case the user in the middle of something
    3. How can I stall my interval from restarting or the rest of my page from working until the end of month scripts have finished.

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    8,040
    Thanks
    37
    Thanked 1,076 Times in 1,072 Posts
    cookies are a horrible way of doing this.
    You have PHP so use CRON to set a time -> The first day of the month, because it's easier to use the first day rather then the last day(which changes) and most importantly your player MUST be done with their tasks and things won't be abrupt to do your task.

    The crown settings should be: * * 1 * * <script to be executed>
    Last edited by sunfighter; Apr 17th, 2019 at 06:48 PM.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,378
    Thanks
    4
    Thanked 484 Times in 472 Posts
    1) Client side scripting can be easily overridden/hacked/dicked with. It is NOT an answer.

    2) Cookies containing relevant information slows down the page, and can be easily overridden/hacked/dicked with which is why the most you would want in a cookie is a randomized token, aka exactly what PHP sessions provide. The window in which the cookie can be abused can also be reduced using session_regenerate_id();

    Assuming these monthly tasks are supposed to halt all user activities until completed, and assuming multiple users, I would have each user set a value where the software can access it, such as "last active" as a date in a database. Then another field the system can set to say "stop user actions".

    Set "stop user actions" to true when you want to start the monthly activity. Any future user attempts at doing things can either be cached, or flat out rejected (telling the user such) when that's set. Then wait for all "last active" to be older than however long the average "turn" or "user action" takes so you can be sure users are "done" and everything is inactive... once that's done, do what you want to do for updates.

    When the updates are complete, turn "stop user actions" false and let things continue.

    As @sunfighter suggested, setting the trigger for the updates as a cron job is a really good idea, and you might want to lower the PHP execution timeout for that cron as well depending on how big/long those updates take.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •