def create_connection(start_hex: str = "A1", end_hex: str = "A2", conn_id: int = 1) -> Connection: """Creates a new connection with the provided ids""" return Connection(start_hex=Coordinate.from_hex(start_hex), end_hex=Coordinate.from_hex(end_hex), conn_id=conn_id)
def connected(self, planet): if planet: linked = link.execute_read_query(f""" SELECT name, coordinate FROM planets WHERE coordinate IN ( SELECT end_hex FROM planetary_connections WHERE start_hex='{planet.coordinates}' ) """) if linked: print(f"{planet.name} connects to:") print(f"{'Name':<16}Distance") for name, coord in linked: coordinate = Coordinate.from_hex(coord) steps_to_column = abs(coordinate.x - planet.coordinates.x) steps_to_row = abs(coordinate.y - planet.coordinates.y) even_column = (coordinate.x % 2 == 0) moving_up = (coordinate.y - planet.coordinates.y < 0) if moving_up ^ even_column: saved_steps_from_columns = math.floor( (steps_to_column + 1) / 2) else: saved_steps_from_columns = math.floor(steps_to_column / 2) steps_to_row = max(0, steps_to_row - saved_steps_from_columns) distance = steps_to_column + steps_to_row print(f"{name:<16}{distance}")
def test_add_addsCorrectly(converter: ConnectionConverter, sql_connection): sql_connection.set_execute_read_results([[("A2", "B2", 1)]]) returned_value = converter.add("A2", "B2") expected_query = """ INSERT INTO planetary_connections ( start_hex, end_hex ) VALUES ( 'A2', 'B2' ); SELECT * FROM planetary_connections WHERE start_hex='A2' AND end_hex='B2'""" queries = sql_connection.get_queries() assert strip_whitespace(expected_query) == strip_whitespace(queries[0]) assert returned_value.start_hex == Coordinate.from_hex("A2") assert returned_value.end_hex == Coordinate.from_hex("B2") assert returned_value.connection_id == 1
def test_deserialize_returnsExpectedPlanet(): serialized = (1, "TestPlanet", "A2", 2, 3, 4, 5, 6, "A planet", "notes") planet = Planet.deserialize(serialized) assert planet.ID == 1 assert planet.name == "TestPlanet" assert planet.coordinates == Coordinate.from_hex("A2") assert planet.tl == 2 assert planet.biosphere == 3 assert planet.atmosphere == 4 assert planet.temperature == 5 assert planet.population == 6 assert planet.description == "A planet" assert planet.notes == "notes"
def do_add(self, inp): ''' usage: add name coordinates [tl=0] [bio=1] [atmos=2] [temp=3] [pop=4] Adds a new planet to the database at the specified coordinate and name ''' try: args = inp.split(' ') name = args[0] coords = Coordinate.from_hex(args[1]) optionals = self.parse(args[2:]) self.converters.planet.add(name=name, coords=coords, **optionals) self.link.commit() except Exception as e: print("Error padding new planet: ", e)
def __init__(self, coords="A2"): self.coordinates = Coordinate.from_hex(coords)
def __init__(self, start, end, conn): self.start_hex = Coordinate.from_hex(start) self.end_hex = Coordinate.from_hex(end) self.connection_id = conn