Display planet info in a Tree widget.

This commit is contained in:
Simon Forman 2024-04-14 20:56:07 -07:00
parent 7219d6b336
commit b049f588ab
3 changed files with 77 additions and 1 deletions

View File

@ -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.

View File

@ -35,6 +35,7 @@ Stars have what qualities?
''' '''
from math import log10
from random import randint, expovariate, seed, gauss, lognormvariate from random import randint, expovariate, seed, gauss, lognormvariate
from poisson import poisson from poisson import poisson
from wordlist import generate_name from wordlist import generate_name
@ -109,6 +110,46 @@ def iter_stars(conn):
c.close() 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 # Procedural Generation of solar system data

15
ui.py
View File

@ -19,7 +19,7 @@
# along with game. If not see <http://www.gnu.org/licenses/>. # along with game. If not see <http://www.gnu.org/licenses/>.
# #
from tkinter import * from tkinter import *
from tkinter.ttk import Notebook from tkinter.ttk import Notebook, Treeview
import data, stars 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 = 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) 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 Tabs
notebook.add(frame, text='Star Map', underline=5) notebook.add(frame, text='Star Map', underline=5)
@ -98,6 +108,9 @@ class App:
self.star_name_label['text'] = name self.star_name_label['text'] = name
self.star_x_label['text'] = f'x: {x}' self.star_x_label['text'] = f'x: {x}'
self.star_y_label['text'] = f'y: {y}' 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) self.notebook.select(self.star_system_tab_id)
data.open_db() data.open_db()