From 8dd8ed9bca1cf9e575deb1c88895082d0987a69d Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Mon, 15 Apr 2024 15:19:42 -0700 Subject: [PATCH] Move SQL tables into data module. I think it makes more sense to be able to see them together in one place, eh? --- data.py | 43 ++++++++++++++++++++++++++++++++++++++++--- game.sqlite | Bin 389120 -> 389120 bytes population.py | 12 ------------ server.py | 10 +++++++--- stars.py | 30 ------------------------------ 5 files changed, 47 insertions(+), 48 deletions(-) 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 54b5b4bb1fca84a465e52f129070cfed8c0ed9a6..85b246defc0ff04c2fc35227356be4b7a8476d10 100644 GIT binary patch delta 487 zcmZp8Al~pme1bG1>qHr6aaIPsq6NHsdl*=GRx&P*w3U1%B uk@@|4rYAAmmnO3CPGI_cdi$1-Ebr`@HYaaiV#s>dk;#14_TpmJ-WmY45u8K- delta 460 zcmXYqPiPZC7{%xNcE)UQv%6K>HBH%VG+@3Z2$_P=aUK{MD2*YyfWM#rsgX;ZBk8_7^&uW z-KMeqD1DldafBi-&w4y4RsEW~FwYl!8jC|agPqWwp>7nM^A?Q<9-d7wl1045y~~jZfJSMv2A%Kq&0yszHdkl2=I*RRv)*8GUH} z+Q(e`$@(J5^+nWb`1GIL+{3@)pvtgM*~#KsQLaoVFau-qu}}&&=5ETZ_sae|{MnLk fI8C+i=)c@Lq`$9YdriK+%C>Ld 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.')