1
\$\begingroup\$

I am developing a mobile strategy game like Clash of Kings. There are scheduled events in the game (like constructing buildings, recruiting troops, and attacking to other settlements) which happen when a specified time comes.

I was wondering what is a good way to handle these kind of events?

In my current design, I am using AWS products everywhere. A load balancer redirects a user request to one of the http servers which runs a nodejs code. Nodejs script creates an event record which contains required data to do the event later.(For example it contains number of soldiers, defender settlement, event time etc. in case of attacks.) It then inserts a record to a table called Events in DynamoDB(NoSQL DB in AWS). DynamoDB supports something like a callback system using AWS Lambda and DynamoDB Streams, which basically allows me to run a serverless code whenever new data is inserted to a table. I am planning to write a lambda function that connects to an internal worker computer through an internal load balancer, and gives event data to the worker for it to process. Worker runs a java code, and uses a priority queue to hold events. At each second, it pops events whose time has come from priority queue and spawns threads to handle them. Threads does the required calculations, inserts the results to DynamoDB, and possibly sends notifications to users through AWS SNS.

The reason I did not connect directly to workers from http servers was to use http servers to answer user queries as fast as possible, and carrying load to the background system.

Do you think handling timed events like this is a good solution? Is there a standard or a better way to approach this problem?

My main concern is that I will be running a loop that sleeps for 1 sec and polls the priority queue. If I was able to design a callback like system, which calls a function when the event time comes, I would be using less resources. Is there such an architecture or a system that makes this easy?

As an alternative, I read that some browser strategy games do not have background processes, and they do available events on user requests. Before handling each request, they run a code that does due events. Would that be a better approach?

As a footnote, I want to develop a system that is easily scalable which can support number of users in the order of millions.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.