# Browser Based Games: Back-end for battles

This topic is 810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone, I am a new member here. I'm also part of a team that is currently trying to develop a browser-based game like Tribal Wars 2 or Travian.

There are actually a few things that I want help but my number one question right now is about how we should approach executing battles or attacks to an empire in the game real-time.

Right now, we currently execute the battles every 1 minute using a cron job. It works pretty much fine but along the way we realized a few things:

1. Attacks are time-based so if we execute the battle every minute there will be delay in completing the battle job.
2. Attacks are also simultaneous, so a particular empire can be attacked many times at the same time.
3. There will be waiting time for a battle to complete (around 3-5 seconds), so when an attack to the same empire that is just 1-2 seconds behind the previous attack will also have to be delayed.

##### Share on other sites

Well I'm certainly no expert when it comes to structuring a RTS game, but why every minute?

I would imagine you would organize all of your empires in some kind of construct, and iterate through them. Instead of having a pre-allocated job run once every minute, empires should attack at any time their AI determines that they have enough soldiers, artillery, and morale based upon the correct (or incorrect) information gathered from their scout unit.

while (true)
{
for (int i = 0; i < empires.size(); i++)
empires.at(i).update(/*some delta value, and other necessary arguments*/);
}


I could be misunderstanding the question. But, if your saying what I think your saying; it would seem odd to limit offensives from enemy empires based upon some arbitrary one minute delimiter.

Marcus Hansen
Edited by markypooch

##### Share on other sites

Any particular reason why you can't run a continuous process that handles battles (or waits if there are none)? Would firing the battle job every second do the trick, for example?

Note that you didn't state how you want it handled, ie what is the desired behaviour that you want to have?

Deriving desired behaviour from disadvantages of a current solution is a bit hard :)

##### Share on other sites
How to handle this depends somewhat on the server technology. Sometimes a cron job is used because it has to be done via a web request, eg. for PHP games, or if the server is entirely event-driven. Knowing what language is being used would help here.

I think the issue around simultaneous attacks goes away as long as you resolve each attack in the order they appear, one after the other. If that is a problem, it implies some other constraint or requirement that isn't specified above. Either way, it's probably simplest to push attacks onto a queue and have some other process pull attacks off the queue to process them in order. How to implement the queue again depends on the technology (although a simple DB table usually suffices).

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 10
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632647
• Total Posts
3007640
• ### Who's Online (See full list)

There are no registered users currently online

×