diff --git a/data.py b/data.py index 3aa74d2..f31995b 100644 --- a/data.py +++ b/data.py @@ -28,12 +28,49 @@ FILENAME = './game.sqlite' 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, + x INTEGER, + y INTEGER, + 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 INTEGER DEFAULT 0, + industrial_capacity INTEGER, + industry INTEGER DEFAULT 0, + ''' + '''star INTEGER, + FOREIGN KEY(star) REFERENCES stars(id) + )'''), + ] + + def initialize_db_tables(filename): conn = open_db(filename) c = conn.cursor() - for statement in stars.TABLES: - c.execute(statement) - for statement in population.TABLES: + for statement in TABLES: c.execute(statement) c.close() conn.commit() diff --git a/game.sqlite b/game.sqlite index 54b5b4b..85b246d 100644 Binary files a/game.sqlite and b/game.sqlite differ diff --git a/population.py b/population.py index 40b003e..fc13b95 100644 --- a/population.py +++ b/population.py @@ -20,18 +20,6 @@ from random import sample -TABLES = [ - '''\ - create table populations ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - species TEXT, - pop INTEGER, - planet INTEGER, - FOREIGN KEY(planet) REFERENCES planets(id) - )''', - ] - - INITIAL_POP = 1000 INITIAL_NUMBER_OF_SENTIENT_PEOPLES = 12 diff --git a/server.py b/server.py index 8780cad..8af7265 100644 --- a/server.py +++ b/server.py @@ -69,16 +69,20 @@ def population_grows(db_cursor): 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. pops = list(db_cursor.fetchall()) for pop_id, pop, planet_id, bio in pops: if pop > bio: # famine and death! Woe! + print('WARNING population crash on planet {planet_id}!') pop = bio >> 1 bio //= 10 else: - bio -= pop # Everyone eats. - new_pop = round(pop * POP_GROWTH) - pop += new_pop + # Everyone eats. + bio -= pop + pop += round(pop * POP_GROWTH) print(f'population {pop_id} of planet {planet_id} is now {pop}, bio is {bio}') db_cursor.execute('update planets set bio = ? where id = ?', (bio, planet_id)) db_cursor.execute('update populations set pop = ? where id = ?', (pop, pop_id)) diff --git a/stars.py b/stars.py index 1270a41..a49b56f 100644 --- a/stars.py +++ b/stars.py @@ -67,36 +67,6 @@ WIDTH, HEIGHT = 10240, 7680 # Database stuff. SQL, etc. -TABLES = [ - '''\ - create table stars ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - x INTEGER, - y INTEGER, - 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 INTEGER DEFAULT 0, - industrial_capacity INTEGER, - industry INTEGER DEFAULT 0, - ''' - '''star INTEGER, - FOREIGN KEY(star) REFERENCES stars(id) - )'''), - ] - def init_db(conn): print('Generating star data.')