def web_create_game(): """ Called when the user wants to create a new game. Request Fields: name (str): The publicly displayed name of the `Game` we want to create. join_code (str): The password to join the `Game` we want to create. Returns: If successful, this function will return the URL to the lobby (and later, gameplay) of the new `Game`. If not, we return an error header with a message stating what went wrong in the body. """ # Declare global variables global MIN_JOINCODE_LENGTH global MAX_JOINCODE_LENGTH from interstellarage import current_user user = current_user() if user is None: return "Not logged in", 400 # Get the given join code and name. join_code = request.form['join_code'] name = request.form['name'] assert len(join_code) in irange(MIN_JOINCODE_LENGTH, MAX_JOINCODE_LENGTH) assert len(name) in irange(MIN_NAME_LENGTH, MAX_NAME_LENGTH) # Ensure that the faction code looks good faction_code = int(request.form['faction']) # TODO # Create the game and add the player who made it. new_game = Game(name, join_code) new_game.add_user(user, faction_code, creator=True) # Return the URL return "/game/{0}".format(str(new_game.unique))
def __init__(self, game, generate=False): """ Not only is this the constructor for the `Galaxy` class, this also will generate a random `Galaxy` if and only if the correct and precise keyword arguments are passed in (hint: to generate a `Galaxy`, set `generate` to `True`). Args: game (Game): The `Game` which this `Galaxy` will be used for. Keyword Args: generate (boolean): Set to `True` if this `Galaxy` is to be generated at random. Note: It is the responsibility of the caller of this constructor to save this galaxy to disk. """ # Assign attributes. self.game = game # Escape gase. if not generate: return # Declare global variables. global GALAXY_START_JSON global GALAXY_DEFAULT_RANGE global GALAXY_LENGTH global GALAXY_WIDTH global GALAXY_HEIGHT global STARS_PER_CUBIC_LY # Open the JSON import os current_directory = os.path.dirname(__file__) json_f = open(current_directory+GALAXY_START_JSON) json_contents = json_f.read() data = json.loads(json_contents) json_f.close() # Append default systems. for system in data: # Parse the system dict and assign the system object a unique ID. self._system_unique_counter += 1 system_obj = system_lib.system_from_dict(system, game) system_obj.unique = self._system_unique_counter system_obj.galaxy = self # Assign the planets of this system unique identifiers system_planets = system_obj.flat_planets() for planet in system_planets: self._planet_unique_counter += 1 planet.unique = self._planet_unique_counter self.systems.append(system_obj) # The dimensions of the galaxy. adj_dim = lambda x: (x - 1) / 2 abs_sum = lambda x, y, z: abs(x) + abs(y) + abs(z) width = irange(-adj_dim(GALAXY_WIDTH), adj_dim(GALAXY_WIDTH)) length = irange(-adj_dim(GALAXY_LENGTH), adj_dim(GALAXY_LENGTH)) height = irange(-adj_dim(GALAXY_HEIGHT), adj_dim(GALAXY_HEIGHT)) positions = [(x, y, z) for x in width for y in length for z in height] gdr = GALAXY_DEFAULT_RANGE in_default_range = lambda x, y, z: (abs_sum(x, y, z) <= gdr) in_discover_range = lambda x, y, z: (abs_sum(x, y, z) <= gdr + 4) # Loop through the galatic grid. For the positions outside the range of # default systems, randomly create new ones. generated = 0 for (x, y, z) in positions: if in_default_range(x, y, z): continue elif coinflip(STARS_PER_CUBIC_LY): generated += 1 self._system_unique_counter += 1 new_sys = self._create_system(x, y, z) new_sys.unique = self._system_unique_counter if in_discover_range(x, y, z): new_sys.discovered_by = set(game.players) # Assign the new planets unique identifiers. new_sys_planets = new_sys.flat_planets() for planet in new_sys_planets: self._planet_unique_counter += 1 planet.unique = self._planet_unique_counter self.systems.append(new_sys) # Save to disk print "Generated {0} systems".format(str(generated))