diff --git a/data.py b/data.py index f31995b..aefc348 100644 --- a/data.py +++ b/data.py @@ -29,14 +29,6 @@ conn = None TABLES = [ - '''\ - create table populations ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - species TEXT, - pop INTEGER, - planet INTEGER, - FOREIGN KEY(planet) REFERENCES planets(id) - )''', '''\ create table stars ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -45,26 +37,34 @@ TABLES = [ radius INTEGER, name TEXT UNIQUE )''', - ('''\ + ( + '''\ create table planets ( id INTEGER PRIMARY KEY, ordo INTEGER,''' # The order from the star, counting from 1. # I use "ordo" instead of "order" because "order" is a keyword # in SQL (or at least it caused a syntax error in SQLite.) - # These capacities are fixed and reflect the conditions on the # planet in a very abstract way. Bio and Industry should be # low/high near and far from the star and high/low in the mid- # range, but for now I'm just going to use random numbers. - '''bio_capacity INTEGER, + '''bio_capacity INTEGER, bio INTEGER DEFAULT 0, industrial_capacity INTEGER, industry INTEGER DEFAULT 0, - ''' - '''star INTEGER, + star INTEGER, FOREIGN KEY(star) REFERENCES stars(id) - )'''), - ] + )''' + ), + '''\ + create table populations ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + species TEXT, + pop INTEGER, + planet INTEGER, + FOREIGN KEY(planet) REFERENCES planets(id) + )''', +] def initialize_db_tables(filename): diff --git a/population.py b/population.py index fc13b95..2eea1fc 100644 --- a/population.py +++ b/population.py @@ -29,11 +29,13 @@ def init_db(conn): # Find the not-too-crappy planets. c = conn.cursor() - c.execute('''\ + c.execute( + '''\ select id, bio, industrial_capacity from planets where bio > 10000 and industrial_capacity > 10000 - ''') + ''' + ) # Note that bio is initialized to full capacity whereas industry is 0. # Select some and set initial population and industry. @@ -42,8 +44,14 @@ def init_db(conn): bio -= INITIAL_POP # We know bio is above 10000 from the query above. ind = min(INITIAL_POP, industrial_capacity) people_name = get_name_of_planets_star(c, planet_id) + 'ians' - c.execute('update planets set bio = ?, industry = ? where id = ?', (bio, ind, planet_id)) - c.execute('insert into populations(species, pop, planet) values (?, ?, ?)', (people_name, INITIAL_POP, planet_id)) + c.execute( + 'update planets set bio = ?, industry = ? where id = ?', + (bio, ind, planet_id), + ) + c.execute( + 'insert into populations(species, pop, planet) values (?, ?, ?)', + (people_name, INITIAL_POP, planet_id), + ) print(planet_id, bio, industrial_capacity) c.close() conn.commit() @@ -51,12 +59,13 @@ def init_db(conn): def get_name_of_planets_star(db_cursor, planet_id): # My first ever JOIN statement! Squeeeee! - db_cursor.execute('''\ + db_cursor.execute( + '''\ select stars.name from stars join planets on stars.id = planets.star and planets.id = ? ''', - (planet_id,) + (planet_id,), ) return db_cursor.fetchone()[0] diff --git a/server.py b/server.py index 8af7265..32ed776 100644 --- a/server.py +++ b/server.py @@ -34,12 +34,15 @@ def turn(): BIO_GROWTH = 0.03 POP_GROWTH = 0.0125 + def life_grows(db_cursor): print('life_grows') - db_cursor.execute(''' + db_cursor.execute( + ''' select id, bio, bio_capacity from planets where bio > 0 and bio < bio_capacity - ''') + ''' + ) planets = list(db_cursor.fetchall()) # Get all the results before updating some in this loop. for planet_id, bio, bio_capacity in planets: @@ -58,20 +61,24 @@ def life_grows(db_cursor): bio = bio + new_bio assert bio <= bio_capacity - print(f'setting planet {planet_id} bio to {bio} increment of {new_bio}, excess_bio {excess_bio}') + print( + f'setting planet {planet_id} bio to {bio} increment of {new_bio}, excess_bio {excess_bio}' + ) db_cursor.execute('update planets set bio = ? where id = ?', (bio, planet_id)) def population_grows(db_cursor): print('population_grows') - db_cursor.execute(''' + db_cursor.execute( + ''' select populations.id, populations.pop, planets.id, planets.bio from populations join planets on populations.planet = planets.id - ''') - # and populations.home_star = planets.star - # People are only legally allowed to reproduce in their home - # stellar systems. + ''' + ) + # and populations.home_star = planets.star + # People are only legally allowed to reproduce in their home + # stellar systems. pops = list(db_cursor.fetchall()) for pop_id, pop, planet_id, bio in pops: if pop > bio: @@ -91,6 +98,7 @@ def population_grows(db_cursor): def industry_produces(): print('industry_produces') + def vessels_move(): print('vessels_move') diff --git a/stars.py b/stars.py index a49b56f..9b46eee 100644 --- a/stars.py +++ b/stars.py @@ -98,8 +98,8 @@ def get_planets_for_star_named(conn, name): ' where "star" = ?' ' order by ordo' ' limit 29', # Because we only have 29 Roman numbers. - (star_id,) - ) + (star_id,), + ) # Note that fetchone() returns a tuple value suitable for passing # to execute() as the values tuple. I destructure the result tuple # and build a new one soley because it would be slightly obscure to @@ -138,7 +138,7 @@ def get_planets_for_star_named(conn, name): def generate_stars(width, height, minimum_distance_between_stars): unique_names = set() - for (x, y) in poisson(width, height, minimum_distance_between_stars): + for x, y in poisson(width, height, minimum_distance_between_stars): name = generate_name() while name in unique_names: name = generate_name() @@ -168,15 +168,15 @@ def generate_planets_for_star(c): for ordo in range(1, how_many_planets() + 1): bio_capacity = round(lognormvariate(10, 5)) bio = bio_capacity if has_life() else 0 - c.execute('insert into planets' - '(ordo, star, bio_capacity, bio, industrial_capacity)' - ' values (?, ?, ?, ?, ?)', - ( - ordo, - star_id, - bio_capacity, - bio, - round(lognormvariate(10, 5)), - ) - ) - + c.execute( + 'insert into planets' + '(ordo, star, bio_capacity, bio, industrial_capacity)' + ' values (?, ?, ?, ?, ?)', + ( + ordo, + star_id, + bio_capacity, + bio, + round(lognormvariate(10, 5)), + ), + )