I am creating a browser-based MMO that has time-based events: I.E. the construction of a building will take 23 hours.
I can log that in my MySQL database and then loop through an events table (or equivilent) using a cron script or watch (this question was immensely helpful).
There seems to be a general consensus that I can either:
(1) Run a cron job every second for a script that processes the events and their related triggers.
(2) Run a cron job every minute that loops every second for 59 iterations that processes the events and their related triggers.
(3) Use watch to run a php script every second.
Essentially, upon completion of an event, the script will process some triggers that range anywhere from changing states, updating databases, triggering other “events” that are queued after the recent event, and so on.
My biggest concerns come where there is the potential to have 10,000+ events and triggers to process every second. What are some strategies I could use to avoid performance concerns? Are these all of the solutions, or are there better alternatives?
Example events:
Construction of Building A - 00:43:21
Fast forward 2601 seconds and the following triggers are executed:
Place Building A on map at Location x,y
Update resource total given by building
Start timer on next building in the queue
Construction of Building B - 02:10:49
To better explain: In the early stages of the game these events will take anywhere from 5 minutes to an hour. In the later stages of the game these events will be spaced apart by hundreds of hours. I need these events to process in real time because they will impact other aspects of the game (Such as a resource structure improving the amount of resources given every hour). Thus, these events need to be processed by the server, regardless of whether or not the user is active. The theoretical script would check for expired events, and then based on the event, run several other methods related to the expiration of said event.
https://github.com/prggmr/prggmr
Sounds like an event library might help you tremendously here,
Instead of trying to have a “catch-all” that monitors all events to take place in the future you could write a simple interval in prggmr that will pick up events that need to happen in the future and register them into the event engine to signal at that time.
Something like the following might work,
And to run this code say it is stored in event_detect.php
You would generally want to have the process managed by another app such as runit.
With this approach you could build in all events that can happen into your application as registered “signal handlers” and have an actual gaming engine running in the background that waits for and monitors events as they happen in real-time without any user interaction.
We are actually using this approach for a similar situation for a game we are currently developing … logically quite different but non-user interactive event processing all the same.
Also for performance you can very easily handle 10,000 events in a second or less depending on your server.