From b049f588ab713a7de8dff9f3be9cee181c1d7833 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sun, 14 Apr 2024 20:56:07 -0700 Subject: [PATCH] Display planet info in a Tree widget. --- README.md | 22 ++++++++++++++++++++++ stars.py | 41 +++++++++++++++++++++++++++++++++++++++++ ui.py | 15 ++++++++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 023f382..3d15c35 100644 --- a/README.md +++ b/README.md @@ -204,3 +204,25 @@ to: +Sun Apr 14 20:45:17 PDT 2024 + +Added some planets to stars and stats to planets. There are two kinds of +capacity for each planet: "bio" and industrial. The bio capacity is the +most bio production (of "spices" and population) possible and the industrial +capacity is for production of seeders and ships and bases and gates etc. + +Most planets start out w/o life and most planets that have life do not +have sentient species. + +A population on a homeworld grows at a certain rate, industrial +production is limited by population (up to the max: planet capacity) but +you can build "multipliers" that can make pop 10x - 1000x more effective. + +biomass w/o pop growth, so you need at least two numbers: life and pop. +life goes up automatically but pop cannot go up on non-homeworlds (by bug +law) except by immigration. + +bio is (well, shall be) in the planet table but pop should have it's own +table. + + diff --git a/stars.py b/stars.py index a7819f5..e898334 100644 --- a/stars.py +++ b/stars.py @@ -35,6 +35,7 @@ Stars have what qualities? ♈ ♉ ♊ ♋ ♌ ♍ ♎ ♏ ♐ ♑ ♒ ♓ ''' +from math import log10 from random import randint, expovariate, seed, gauss, lognormvariate from poisson import poisson from wordlist import generate_name @@ -109,6 +110,46 @@ def iter_stars(conn): c.close() +def get_planets_for_star_named(conn, name): + c = conn.cursor() + c.execute('select id from stars where "name" = ?', (name,)) + (star_id,) = c.fetchone() + c.execute( + 'select ordo, bio_capacity, industrial_capacity from planets' + ' where "star" = ?' + ' order by ordo' + ' limit 29', # Because we only have 29 Roman numbers. + (star_id,) + ) + # Note that fetchone() returns a tuple value suitable for passing + # to execute() as the values tuple. I destructure the result tuple + # and build a new one soley because it would be slightly obscure to + # do otherwise. I might use a variable named, say, star_id_tuple or + # star_id_row and pass it directly to execute(), or even go full + # cowboy and put the call to fetchone() in the parameter position in + # the call to execute() and eschew a variable altogether. But if + # somehow the query is made for a star name that isn't in the DB then + # the traceback would be unnecessarily unclear. + # + # In the event, having gone to this length to explain the situation + # I'll probably come back and switch to reusing the result tuple. + # I hate the waste. I bet if you looked at the bytecode the work is + # there: unpacking and repacking the id int. Bleah. Then again, + # this is running in response to a user event and the extra work is + # done once and it's not a lot compared to all the other work that's + # about to happen (drawing these planet data into the GUI) so it + # would be daft to worry about it (after working this all out, that + # is.) + + for ordo, bio_capacity, industrial_capacity in c.fetchall(): + assert bio_capacity >= 0 + assert industrial_capacity >= 0 + b = round(log10(bio_capacity), 1) if bio_capacity else 0 + i = round(log10(industrial_capacity), 1) if industrial_capacity else 0 + yield f'{name}-{ROMAN_NUMBERS[ordo]}', b, i + c.close() + + # Procedural Generation of solar system data diff --git a/ui.py b/ui.py index 85902f7..5cd0bc3 100755 --- a/ui.py +++ b/ui.py @@ -19,7 +19,7 @@ # along with game. If not see . # from tkinter import * -from tkinter.ttk import Notebook +from tkinter.ttk import Notebook, Treeview import data, stars @@ -84,6 +84,16 @@ class App: star_y_label = self.star_y_label = Label(star_system_frame, text='y', bg='green', fg='white', anchor=W) star_y_label.grid(row=2, column=0, sticky=E+W) + planets_list = self.planets_list = Treeview(star_system_frame, columns=('Name', 'BIO', 'IND')) + planets_list.column('#0', width=10, minwidth=10, stretch=False) + planets_list.column('Name', width=140, stretch=False) + planets_list.column('BIO', width=50, stretch=False, anchor=CENTER) + planets_list.column('IND', width=50, stretch=False, anchor=CENTER) + planets_list.heading('Name', text='Name') + planets_list.heading('BIO', text='BIO', anchor=CENTER) + planets_list.heading('IND', text='IND', anchor=CENTER) + planets_list.grid(row=3, column=0, sticky=E+W) + # Notebook Tabs notebook.add(frame, text='Star Map', underline=5) @@ -98,6 +108,9 @@ class App: self.star_name_label['text'] = name self.star_x_label['text'] = f'x: {x}' self.star_y_label['text'] = f'y: {y}' + self.planets_list.delete(*self.planets_list.get_children()) + for values in stars.get_planets_for_star_named(data.conn, name): + self.planets_list.insert('', END, values=values) self.notebook.select(self.star_system_tab_id) data.open_db()