It is pretty simple and straight forward really. You simply set up certain player actions to poll to see if a change is required. These polls, or "agents of change" (AC) can occur where sensible. While there are more simple methods to deal with this problem, we can use OldRod's problem in another thread about wanting to have a fictional calendar in his game that moved differently from the real calendar (likely faster). We will just say it needs to update every 2 hours. To achieve this, you would need to have a TIMESTAMP field in the table that controls the date (it must be set to display the time as well, if your logic requires it; the calendar in this example would). That way, the last time the item was altered in the database, it would be marked. Then, when a player tries to pull up the calendar, you would need to check that timestamp before displaying it. If it has been over 2 hours since it was last altered, a script runs to update it in the database for everyone. Then if another player loads the calendar and it has not been 2 hours since it was last updated, they simply get the date with no alteration to the database. As long as the timestamp field is in the same table as what you are querying for, there should be no MySQL-connection performance loss when an update is not required. You will be pulling the timestamp out on the same query you would need to make anyway, so all you would have to do is check it in the array. Only if an update to the data is needed would you need to run another query (well, 2 more queries actually, one to update the db, and another to fetch the updated info, but it still shouldn't matter provided you aren't running updates several times a minute).
One other consideration that needs to be taken is that if you are incrementing a value, which may be the case with the calendar (incrementing a date and month for example), you need to take into account that there will likely be times when no players will be on. There needs to be code that runs before the query to update the variable that checks to see if multiple periods have passed without an update.
For example, here is pseudo-code for the calendar solution:
Code: Select all
//Running code to check calendar to display to player.
//gameDate table assumed to only have 1 entry, the in-game date info and timestamp, so no WHERE clause needed.
$query = "SELECT * from gameDate";
$queryResult = mysqli_query($db, $query) or die ("Issue pulling up date. Please try again later.");
$queryArray = mysqli_fetch_assoc($queryResult);
//Check the date of last change (psuedo-code; would need cleaning up)
$timeDifference = current_time() - $queryArray['timestamp'] ;
$hoursSince = $timeDifference / 60 / 60; //divide first for minutes, then hours
$daysNeeded = $hoursSince / 2; //find out how many 2 hour periods have occured
//Cast the "days needed" variable to an integer to ensure truncating off any decimal
$daysUpdating = (integer)$daysNeeded;
//Check to see if update required, and do the update and return a fresh date variable from the db to display if needed
if ($daysUpdating >= 1) {
/////Logic to alter any other date fields, such as month or year, based on the $daysUpdate variable goes here
////This may include month, year, etc. Do calculations and prepare variables to update
////this info in the below UPDATE query
//update the date
$query2 = "UPDATE query to add $daysUpdating to the current date and update anything else";
mysqli_query($db, $query);
//Get a fresh date array from the db
$query3 = "SELECT * from date";
$queryResult3 = mysqli_query($db, $query3) or die ("Could not get new date variable");
$queryArray3 = mysqli_fetch_assoc($queryResult3);
//Print the date in calendar format. Will only echo date here:
echo "Today's game world date is ".$queryArray3['date']."!";
} else {
//If there was no update needed, display the date with the original, correct date array variable
echo "Today's game world date is ".$queryArray['date']."!";
}