def place_tile(self, coord, tile, orientation): cell = Cell.from_coord(coord) if cell == CHICAGO_CELL or tile.is_chicago: raise ValueError( "Since Chicago ({}) is a special tile, please use Board.place_chicago()." .format(CHICAGO_CELL)) if int(orientation) not in range(0, 6): raise ValueError( "Orientation out of range. Expected between 0 and 5, inclusive. Got {}." .format(orientation)) old_tile = self.get_space(cell) self._validate_place_tile_space_type(tile, old_tile) self._validate_place_tile_neighbors(cell, tile, orientation) if old_tile: self._validate_place_tile_upgrade(old_tile, cell, tile, orientation) self._placed_tiles[cell] = PlacedTile.place( old_tile.name, cell, tile, orientation, stations=old_tile.stations, port_value=old_tile.port_value, meat_value=old_tile.meat_value) else: self._placed_tiles[cell] = PlacedTile.place( None, cell, tile, orientation)
def _get_orientations(coord, tile_id): if not coord or not tile_id: return None try: cell = Cell.from_coord(coord) except ValueError: return None tile = tiles.get_tile(tile_id) if not tile: return None orientations = [] for orientation in range(0, 6): try: _BASE_BOARD._validate_place_tile_neighbors(cell, tile, orientation) _BASE_BOARD._validate_place_tile_upgrade(_get_space(coord), cell, tile, orientation) except ValueError: continue orientations.append(orientation) return orientations
def create(coord, name, phase=0, edges=[], value=0, capacity=0, is_z=False, port_value=0, meat_value=0): cell = Cell.from_coord(coord) neighbors = {cell.neighbors[side] for side in edges} if cell == CHICAGO_CELL: paths = {cell.neighbors[side]: [] for side in edges} return Chicago(phase, paths, neighbors, value, capacity, port_value=port_value, meat_value=meat_value) else: paths = {neighbor: list(neighbors - {neighbor}) for neighbor in neighbors} return City(name, cell, phase, paths, neighbors, value, capacity, is_z, False, port_value=port_value, meat_value=meat_value)
def create(coord, edges, phase=None): cell = Cell.from_coord(coord) paths = collections.defaultdict(list) for start_edge, end_edge in edges: start_cell = cell.neighbors[start_edge] end_cell = cell.neighbors[end_edge] paths[start_cell].append(end_cell) paths[end_cell].append(start_cell) return Track(cell, phase, paths)
def create(coord, name, edges, values, is_east=False, is_west=False, port_value=0, meat_value=0): cell = Cell.from_coord(coord) paths = {cell.neighbors[side]: [] for side in edges} neighbors = set(paths.keys()) if is_east: return EastTerminalCity(name, cell, paths, neighbors, values, port_value=port_value, meat_value=meat_value) elif is_west: return WestTerminalCity(name, cell, paths, neighbors, values, port_value=port_value, meat_value=meat_value) else: return TerminalCity(name, cell, paths, neighbors, values, port_value=port_value, meat_value=meat_value)
def place_station(self, coord, railroad): cell = Cell.from_coord(coord) if cell == CHICAGO_CELL: raise ValueError( "Since Chicago ({}) is a special tile, please use Board.place_chicago_station()." .format(CHICAGO_CELL)) tile = self.get_space(cell) if not tile.is_city: raise ValueError( "{} is not a city, so it cannot have a station.".format(cell)) tile.add_station(railroad)
def load(board, railroads_rows): railroads = {} for railroad_args in railroads_rows: trains_str = railroad_args.get("trains") if trains_str and trains_str.lower() == "removed": name = railroad_args["name"] if name not in REMOVABLE_RAILROADS: raise ValueError( "Attempted to remove a non-removable railroad.") railroad = RemovedRailroad.create(name) else: railroad = Railroad.create(railroad_args["name"], trains_str) if railroad.name in railroads: raise ValueError(f"Found multiple {railroad.name} definitions.") railroads[railroad.name] = railroad if railroad.name not in RAILROAD_HOME_CITIES: raise ValueError("Unrecognized railroad name: {}".format( railroad.name)) # Place the home station board.place_station(RAILROAD_HOME_CITIES[railroad.name], railroad) station_coords_str = railroad_args.get("stations") if station_coords_str: station_coords = [ coord.strip() for coord in station_coords_str.split(",") ] for coord in station_coords: if coord and coord != RAILROAD_HOME_CITIES[ railroad.name] and Cell.from_coord( coord) != CHICAGO_CELL: board.place_station(coord, railroad) if str(CHICAGO_CELL) in station_coords: chicago_station_exit_coord = str( railroad_args.get("chicago_station_exit_coord", "")).strip() if not chicago_station_exit_coord: raise ValueError( "Chicago is listed as a station for {}, but not exit side was specified." .format(railroad.name)) board.place_chicago_station(railroad, int(chicago_station_exit_coord)) return railroads
def place_seaport_token(self, coord, railroad): if railroad.is_removed: raise ValueError( "A removed railroad cannot place Steamboat Company's token: {}" .format(railroad.name)) current_cell = Cell.from_coord(coord) for cell in board_cells(): space = self.get_space(cell) if space and space.port_token and cell != current_cell: raise ValueError( "Cannot place the seaport token on {}. It's already been placed on {}." .format(current_cell, cell)) self.get_space(current_cell).place_seaport_token(railroad)
def board_tile_info(): coord = request.args.get("coord") chicago_neighbor = request.args.get("chicagoNeighbor") tile_id = request.args.get("tileId") tile = tiles.get_tile(tile_id) if tile_id else _BASE_BOARD.get_space( Cell.from_coord(coord)) default_offset = {"x": 0, "y": 0} offset_data = STATION_DATA["tile"] if tile_id else STATION_DATA["board"] offset = offset_data.get(coord, {}).get("offset", default_offset) if chicago_neighbor: offset = offset[chicago_neighbor] info = {"capacity": tile.capacity, "offset": offset, "phase": tile.phase} return jsonify({"info": info})
def paths(self, enter_from=None, railroad=None): paths = list(super(Chicago, self).paths(enter_from)) if railroad: enter_from_station = self.exit_cell_to_station.get(enter_from) if enter_from_station: if enter_from_station.railroad != railroad: paths = [] else: if not enter_from: station = self.get_station(railroad.name) paths = [ self.get_station_exit_cell(station), Cell.from_coord("C5") ] if station else [] else: for exit in paths: station = self.exit_cell_to_station.get(exit) if station and station.railroad != railroad: paths.remove(exit) return tuple(paths)