def create_game(self): params = self.request.POST profile, is_new = self.get_user_profile() map_width = map_height = int(params['map_size']) game_map = Map(map_width, map_height) game_map.add_planets(int(float(params['density']) * map_width * map_height)) profile.game = Game(game_map) race = races[params['race']] player = Player(1, profile.name, race, params['color']) profile.game.add_player(player) profile.player = player active_games.append(profile.game) from random import randint, choice from gravita.ship import Ship for i in range(5): x = randint(0, map_width - 1) y = randint(0, map_height - 1) game_map[x, y].ship = ship = Ship(player, game_map, (x,y), choice(race.ship_specs)) return profile.game.as_dict(player)
def test_add_planets(self): from gravita.map import Map from gravita import planet map = Map(12, 3) ps = map.add_planets(4) self.assertEqual(len(ps), 4) for p in ps: self.assertTrue(isinstance(p, planet.Planet)) x, y = p.location self.assert_(0 <= x < 12, x) self.assert_(0 <= y < 3, y) for dx in (-1, 0, 1): for dy in (-1, 0, 1): if dx or dy: try: self.assert_(map[x+dx, y+dy].planet is None, [s[1].planet for s in sorted(map._sectors.items()) if s[1].planet]) except KeyError: pass