# -*- coding: utf-8 -*- # # Copyright © 2024 Simon Forman # # This file is part of game # # game is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # game is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with game. If not see . # ''' ''' from random import sample # Database stuff. SQL, etc. TABLES = [ '''\ create table populations ( id INTEGER PRIMARY KEY AUTOINCREMENT, species TEXT, pop INTEGER, planet INTEGER, FOREIGN KEY(planet) REFERENCES planets(id) )''', ] INITIAL_POP = 1000 def init_db(conn): print('Generating population data.') c = conn.cursor() c.execute('''\ select id, bio, industrial_capacity from planets where bio > 10000 and industrial_capacity > 10000 ''') home_worlds = sample(list(c.fetchall()), 12) for planet_id, bio, industrial_capacity in home_worlds: 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) people_name += '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)) print(planet_id, bio, industrial_capacity) c.close() conn.commit() def get_name_of_planets_star(db_cursor, planet_id): db_cursor.execute('''\ select stars.name from stars join planets on stars.id = planets.star and planets.id = ? ''', (planet_id,) ) return db_cursor.fetchone()[0]