I'm working on a game where players can setup villages, which can contain defending units. Any of these units (each on their own tiles) can be set to "campaign" which means they are no longer defending but can now be used to attack other villages. And each unit on a tile can have up to a 100 health. So far so good. Oh and it's all asynchronous so even though the server will be aware that your village is being attacked, you won't be until the attack is over.

The issue I'm struggling with, is the following situation.

Let's say a unit on a tile is being attacked by a player from another village. The other player see's your village and is attacking your units. You don't know this is happening though, so you set your unit to campaign and off you go to attack another village, with the unit which itself is actually being attacked by this other player. The other player stops attacking your village and leaves your unit with say a health of 1, which is then saved to the server. You however have this same unit are attacking another village with it, but now you discover that even though it started off with a 100 health, now mysteriously it only has 1...

Solutions? Ideas?


The simplest solutions are often the best. I referred to Clash of clans below, well after a bit more digging it seems that in CoC you can only attack players that are offline! ha, that almost solves the problem. I say almost because there's still the situation where a players village could be in the process of being attacked when they come back online, still need to address that.

Edit 2

A solution to the "What happens when a player is attacking your village and you come online" issue, could be the attacking player just get's kicked out of the village at that point and just get's whatever they had won up to that point, it's a bit of a fudge but it might work.

  • \$\begingroup\$ So technically this is not really turn based then? \$\endgroup\$ Commented Jun 27, 2013 at 23:01
  • \$\begingroup\$ I've removed the turn-based tag. Battles are explained as above. \$\endgroup\$
    – Phil
    Commented Jun 27, 2013 at 23:12
  • \$\begingroup\$ This is related to concurrency, which means you're in for some real fun times. I think the better way to resolve this would be to just mark armies as 'at home' or 'away' - you can't effect armies that are on campaign (which is a difficult thing to do in the real world), you could only go after the 'at home' ones; although if you could mess with supply lines.... Look at Optimistic Locking, as I think that'll yield better results. \$\endgroup\$ Commented Jun 28, 2013 at 20:01
  • \$\begingroup\$ Thanks for the info, in the end I've decided to just go with the CoC approach where you can't attack players which are offline. \$\endgroup\$
    – Phil
    Commented Jun 28, 2013 at 23:27

1 Answer 1


Some ideas to resolve this situation and possibly report the events to the user.

Idea 1

You can display a kind of event log to the player that he knows what happend to their units in the meantime.

Idea 2

The units which got attacked and which health are below a threashold say 50 HP don't take part in the next battle and must heal themself or stay at your village for fighting for death.

Idea 3

Implement a 3 state logic of the units

  • Defending
  • Pending
  • Attacking/campaign

so the 2nd state is used to

  • resolve all taken damages ( Defending -> Attacking/campaign )
  • resolve all damages given (possibly not needed?)

so to change the mode the state must pass the "pending"/resolving state.

With that this problem doesn't appear.

  • \$\begingroup\$ The problem is that as soon as an enemy is attacking the village, the units can be effected, the only way without changing things a lot is the player has to be informed by the unit being changed to pending mode or something that it's been attacked, the problem with that is then the player can try to do something (or might want to even if they can't) to stop the current attack and now it's become a real time battle game :) It would seem that the only way out of this problem is to not allow attackers to attack units, they can only attack buildings. Which I think is how CoC does it. \$\endgroup\$
    – Phil
    Commented Jun 28, 2013 at 0:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .