game/data.py

79 lines
1.9 KiB
Python

# -*- 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 <http://www.gnu.org/licenses/>.
#
from random import randint, expovariate, seed
import sqlite3
from poisson import poisson
import stars
FILENAME = './game.sqlite'
conn = None
def initialize_db_tables(filename):
conn = open_db(filename)
c = conn.cursor()
for statement in stars.TABLES:
c.execute(statement)
c.close()
conn.commit()
return conn
def close_db():
global conn
if conn:
conn.close()
conn = None
else:
raise RuntimeError('db connection already closed')
def open_db(filename=FILENAME):
global conn
if conn is not None:
raise RuntimeError('db connection already open')
conn = sqlite3.connect(filename)
return conn
def main(filename):
print('Initializing db file', filename)
conn = initialize_db_tables(filename)
stars.init_db(conn)
##def stars():
## global conn
## c = conn.cursor()
## c.execute('select x, y, radius from stars')
## yield from c.fetchall()
## c.close()
if __name__ == '__main__':
import pathlib
if not pathlib.Path(FILENAME).exists():
main(FILENAME)
else:
print(FILENAME, 'already exists! Not overwriting.')