def build_ship(player, ship_type): """ Schedules a ship for being built on a player's home system. Throws utils.NotEnoughMoneyException if the player requires more money """ cost = game_constants.ship_cost(player, ship_type) if cost is None: return # oops if player.resources < cost: raise NotEnoughMoneyException() ship = Ship(game=player.game, owner=player, on_build_queue=True, ship_type=ship_type, attack_tech=game_constants.ship_attack(player, ship_type), range_tech=game_constants.ship_range(player, ship_type), moves=0, system=player.home) player.resources -= cost player.save() ship.save() return ship
def cancel_ship(player, ship_type): """ Cancels a queued ship and refunds the cost """ ships = player.ship_set.filter(ship_type=ship_type, on_build_queue=True) if not ships: raise ValueError("No such ship to cancel") ships[0].delete() cost = game_constants.ship_cost(player, ship_type) player.resources += cost player.save()
def get_prices(player): """Build a dict of ship prices""" return dict([(ship_type, game_constants.ship_cost(player, ship_type)) for ship_type in game_constants.SHIP_TYPES])