import objects from './mechanics/objects';

let state = {};

function init() {
	objects.map(({ key }) => {
		state[key] = {
			owner: null,
			health: 0,
		};
	});
}

function get(key) {
	return state[key];
}

function attack(player, key) {
	if (!state[key]) {
		return;
	}
	
	if (state[key].owner === player.id) {
		return;
	}
	
	state[key].health -= 1;
	state[key].health = Math.max(0, state[key].health);
	
	if (state[key].health === 0) {
		state[key].owner = null;
	}
}

function reinforce(player, key) {
	if (!state[key]) {
		return;
	}
	
	if (!state[key].owner) {
		state[key].owner = player.id;
	}
	
	if (state[key].owner !== player.id) {
		return;
	}
	
	state[key].health += 1;
	state[key].health = Math.min(5, state[key].health);
}

export default {
	init,
	get,
	attack,
	reinforce,
};

@@ -77,26 +77,33 @@
  %7B%0A%09
-objects.map((%7B key
+let clean = (%7B key, moons
  %7D) 
@@ -160,16 +160,88 @@
 ,%0A%09%09%7D;%0A%09
+%09%0A%09%09if (moons) %7B%0A%09%09%09moons.map(clean);%0A%09%09%7D%0A%09%7D;%0A%09%0A%09clean(%7B moons: objects 
 %7D);%0A%7D%0A%0Af

fix state to include moons