Move SQL tables into data module.
I think it makes more sense to be able to see them together in one place, eh?
This commit is contained in:
parent
1eaf8d5e9b
commit
8dd8ed9bca
43
data.py
43
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()
|
||||
|
|
|
|||
BIN
game.sqlite
BIN
game.sqlite
Binary file not shown.
|
|
@ -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
|
||||
|
||||
|
|
|
|||
10
server.py
10
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))
|
||||
|
|
|
|||
30
stars.py
30
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.')
|
||||
|
|
|
|||
Loading…
Reference in New Issue