Repository: jbradberry/universe

Issue:
Owned planets that have no population should lose ownership
Also, an explicit 0 population should be removed.

Discussion:

Patch:
diff --git a/universe/engine.py b/universe/engine.py
index 08765e2..73fc86d 100644
--- a/universe/engine.py
+++ b/universe/engine.py
@@ -24,12 +24,14 @@ class Entity:
         for component in self.__dict__.get('_components', {}).values():
             if name in component._fields:
                 self.__dict__['_data'][name] = value
+                return
         self.__dict__[name] = value
 
     def __delattr__(self, name):
         for component in self.__dict__.get('_components', {}).values():
             if name in component._fields:
                 del self.__dict__['_data'][name]
+                return
         del self.__dict__[name]
 
     def __contains__(self, key):
diff --git a/universe/systems.py b/universe/systems.py
index 7a7043b..f06579c 100644
--- a/universe/systems.py
+++ b/universe/systems.py
@@ -119,3 +119,6 @@ class PopulationGrowthSystem:
             growth_rate = 1 + Decimal(species.growth_rate) / 100
             population *= growth_rate
             entity.population = int(population.to_integral_value())
+            if entity.population <= 0:
+                del entity.population
+                del entity.owner_id


Test patch:
diff --git a/tests/test_systems.py b/tests/test_systems.py
index 96ef1ce..4d07dc0 100644
--- a/tests/test_systems.py
+++ b/tests/test_systems.py
@@ -67,3 +67,38 @@ class PopulationTestCase(unittest.TestCase):
                 }
             }
         )
+
+    def test_lose_ownership(self):
+        state = {
+            'turn': 2500,
+            'width': 1000,
+            'entities': {
+                0: {
+                    'type': 'species',
+                    'name': 'Human',
+                    'plural_name': 'Humans',
+                    'growth_rate': 15,
+                },
+                1: {
+                    'type': 'planet',
+                    'population': 0,
+                    'x': 480,
+                    'y': 235,
+                    'gravity': 50,
+                    'temperature': 50,
+                    'radiation': 50,
+                    'ironium_conc': 50,
+                    'boranium_conc': 50,
+                    'germanium_conc': 50,
+                    'owner_id': 0,
+                    'queue': [],
+                },
+            }
+        }
+
+        S = engine.GameState(state, {})
+        results = S.generate()
+
+        self.assertEqual(len(results['entities']), 2)
+        self.assertNotIn('population', results['entities'][1])
+        self.assertNotIn('owner_id', results['entities'][1])