Skip to content

Commit

Permalink
V0.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
domhnallmorr committed Apr 6, 2024
1 parent 89a99f4 commit 457c9cd
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
customtkinter.set_default_color_theme("blue") # Themes: blue (default), dark-blue, green

app = customtkinter.CTk() # create CTk window like you do with the Tk window
app.title("Race Engine V0.0.6")
app.title("Race Engine V0.0.7")
controller = race_engine_controller.RaceEngineController(app)

app.after(0, lambda:app.state("zoomed"))
Expand Down
3 changes: 3 additions & 0 deletions src/race_engine_controller/race_engine_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ def end_session(self, session):
def auto_simulate_session(self, session):
self.model.simulate_session(session)
self.end_session(session)

def send_player_car_out(self, driver_name, fuel_load_laps, number_laps_to_run):
self.model.send_player_car_out(driver_name, fuel_load_laps, number_laps_to_run)
6 changes: 5 additions & 1 deletion src/race_engine_model/race_engine_car_model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@


class RaceEngineCarModel:
def __init__(self, name, speed, color):
def __init__(self, name, speed, color, circuit_model):
self.name = name
self.speed = speed
self.color = color
self.circuit_model = circuit_model

self.fuel_load = 155 # kg
self.tyre_wear = 0 # time lost (in ms) due to tyre wear
Expand All @@ -27,4 +28,7 @@ def fuel_effect(self):
'''
return int(self.fuel_load * 30)

def calculate_required_fuel(self, number_of_laps):
number_of_laps = float(number_of_laps) + 0.5 # ensure half a lap of fuel is added for conservatism
return int(number_of_laps * self.circuit_model.fuel_consumption)

10 changes: 8 additions & 2 deletions src/race_engine_model/race_engine_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def setup_participants(self):
driver_speed = random.randint(min_driver_speed, max_driver_speed)
driver_count += 1

car = race_engine_car_model.RaceEngineCarModel(team_name, team_speed, color)
car = race_engine_car_model.RaceEngineCarModel(team_name, team_speed, color, self.circuit_model)
driver = race_engine_driver_model.RaceEngineDriverModel(driver_name, driver_speed, driver_status)
self.participants.append(race_engine_particpant_model.RaceEngineParticpantModel(driver, car, self.circuit_model, driver_count))

Expand Down Expand Up @@ -427,7 +427,9 @@ def setup_session(self, session):
self.time_left = 120 * 60 # 2 hours in seconds

for participant in self.participants:
participant.generate_practice_runs(self.time_left)
participant.setup_session()
if participant not in [self.player_driver1, self.player_driver2]:
participant.generate_practice_runs(self.time_left)
participant.status = "in_pits"

# SET STAUS COLUMN TO "PIT"
Expand All @@ -453,3 +455,7 @@ def simulate_session(self, session):
self.advance()
if len(self.commentary_to_process) > 0:
self.commentary_to_process.pop(0)

def send_player_car_out(self, driver_name, fuel_load_laps, number_laps_to_run):
particpant_model = self.get_particpant_model_by_name(driver_name)
particpant_model.send_player_car_out(self.time_left, fuel_load_laps, number_laps_to_run)
11 changes: 9 additions & 2 deletions src/race_engine_model/race_engine_particpant_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@ def update_player_pitstop_laps(self, data):
self.pit2_lap = data["pit2_lap"]
self.pit3_lap = data["pit3_lap"]

def generate_practice_runs(self, session_time):
def setup_session(self):
self.practice_laps_completed = 0
self.practice_runs = [] # [[time_left, fuel, number_laps]]

def generate_practice_runs(self, session_time):

time_left = session_time

while time_left > 0:
Expand Down Expand Up @@ -205,4 +207,9 @@ def update_practice(self, time_left):
self.update_next_update_time(time_left)

def update_next_update_time(self, time_left):
self.next_update_time = time_left - 90
self.next_update_time = time_left - 90

def send_player_car_out(self, time_left, fuel_load_laps, number_laps_to_run):
fuel_load_kg = self.car_model.calculate_required_fuel(fuel_load_laps)
# TODO fix hard coding of -10 seconds below
self.practice_runs.append([time_left-10, fuel_load_kg, number_laps_to_run])
115 changes: 99 additions & 16 deletions src/race_engine_view/custom_widgets/strategy_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@


class StrategyEditor:
def __init__(self, parent, view, driver_name, start_col, start_row):
def __init__(self, parent, view, driver_name, start_col, start_row, session):
self.parent = parent
self.view = view
self.start_col = start_col
#TODO make circuit specific
self.number_laps = 56
self.session = session
self.driver_name = driver_name

self.one_third_distance = int(self.number_laps / 3)
self.half_distance = int(self.number_laps / 2)
Expand All @@ -28,13 +31,24 @@ def __init__(self, parent, view, driver_name, start_col, start_row):
self.plus_buttons = []

row = start_row + 1
for idx in [0, 1, 2]:
self.setup_edit_widgets(idx, row)
row += 2

self.set_default_pit_laps(1)
if self.session in ["FP1"]:
self.setup_practice_edit_widgets(row)
self.change_fuel_event(change=None)
self.change_laps_to_run_event(change=None)
else: # RACE
for idx in [0, 1, 2]:
self.setup_race_edit_widgets(idx, row)
row += 2

self.set_default_pit_laps(1)

def setup_variables(self):
# PRACTICE VARIABLES
self.fuel_load_laps = int(self.number_laps / 2) # in laps
self.number_laps_to_run = 5

# RACE VARIABLES
self.pit1_lap = None
self.pit2_lap = None
self.pit3_lap = None
Expand All @@ -45,30 +59,67 @@ def setup_variables(self):

self.pit_number_vars = [self.pit1_var, self.pit2_var, self.pit3_var]

def setup_edit_widgets(self, idx, row):
btn_width = 40
self.btn_width = 40
self.reduced_pady = 1

def setup_race_edit_widgets(self, idx, row):

reduced_pady = 1
l = customtkinter.CTkLabel(self.parent, text="Some Lap")
l.grid(row=row, column=self.start_col + 2, sticky="EW", padx=self.view.padx, pady=(self.view.pady, reduced_pady))
l.grid(row=row, column=self.start_col + 2, sticky="EW", padx=self.view.padx, pady=(self.view.pady, self.reduced_pady))
self.lap_labels.append(l)

m = customtkinter.CTkButton(self.parent, text= "-", width=btn_width, command=lambda idx=idx:self.minus_lap_event(idx))
m.grid(row=row+1, column=self.start_col + 1, sticky="EW", padx=self.view.padx, pady=(reduced_pady, self.view.pady))
m = customtkinter.CTkButton(self.parent, text= "-", width=self.btn_width, command=lambda idx=idx:self.minus_lap_event(idx))
m.grid(row=row+1, column=self.start_col + 1, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))
self.minus_buttons.append(m)

c = customtkinter.CTkCheckBox(self.parent, text=f"{idx + 1} Stop", variable=self.pit_number_vars[idx], onvalue="on", offvalue="off", width=10,
command=lambda idx=idx: self.pit_strategy_combo_event(idx))
c.grid(row=row, column=self.start_col, rowspan=2, sticky="SE", padx=self.view.padx, pady=(reduced_pady, self.view.pady))
c.grid(row=row, column=self.start_col, rowspan=2, sticky="SE", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

p = customtkinter.CTkProgressBar(self.parent, orientation="horizontal")
p.grid(row=row+1, column=self.start_col + 2, sticky="EW", padx=self.view.padx, pady=(reduced_pady, self.view.pady))
p.grid(row=row+1, column=self.start_col + 2, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))
self.progress_bars.append(p)

p = customtkinter.CTkButton(self.parent, text= "+", width=btn_width, command=lambda idx=idx:self.plus_lap_event(idx))
p.grid(row=row+1, column=self.start_col + 3, sticky="EW", padx=self.view.padx, pady=(reduced_pady, self.view.pady))
p = customtkinter.CTkButton(self.parent, text= "+", width=self.btn_width, command=lambda idx=idx:self.plus_lap_event(idx))
p.grid(row=row+1, column=self.start_col + 3, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))
self.plus_buttons.append(p)

def setup_practice_edit_widgets(self, row):
# FUEL LOAD
self.fuel_load_label = customtkinter.CTkLabel(self.parent, text="%")
self.fuel_load_label.grid(row=row+1, column=self.start_col + 2, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

customtkinter.CTkLabel(self.parent, text="Fuel Load:").grid(row=row+2, column=self.start_col, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

m = customtkinter.CTkButton(self.parent, text= "-", width=self.btn_width, command=lambda change="minus": self.change_fuel_event(change))
m.grid(row=row+2, column=self.start_col + 1, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

self.fuel_load_progress_bar = customtkinter.CTkProgressBar(self.parent, orientation="horizontal")
self.fuel_load_progress_bar.grid(row=row+2, column=self.start_col + 2, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

p = customtkinter.CTkButton(self.parent, text= "+", width=self.btn_width, command=lambda change="plus": self.change_fuel_event(change))
p.grid(row=row+2, column=self.start_col + 3, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

# NUMBER OF LAPS TO RUN
self.number_laps_to_run_label = customtkinter.CTkLabel(self.parent, text="X Laps")
self.number_laps_to_run_label.grid(row=row+3, column=self.start_col + 2, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

customtkinter.CTkLabel(self.parent, text="No. Laps To Run:").grid(row=row+4, column=self.start_col, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

m = customtkinter.CTkButton(self.parent, text= "-", width=self.btn_width, command=lambda change="minus": self.change_laps_to_run_event(change))
m.grid(row=row+4, column=self.start_col + 1, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

self.laps_to_run_progress_bar = customtkinter.CTkProgressBar(self.parent, orientation="horizontal")
self.laps_to_run_progress_bar.grid(row=row+4, column=self.start_col + 2, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

p = customtkinter.CTkButton(self.parent, text= "+", width=self.btn_width, command=lambda change="plus": self.change_laps_to_run_event(change))
p.grid(row=row+4, column=self.start_col + 3, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))

# SEND OUT BUTTOn
b = customtkinter.CTkButton(self.parent, text= "Send Out", width=self.btn_width, command=self.send_out)
b.grid(row=row+5, column=self.start_col, sticky="EW", padx=self.view.padx, pady=(self.reduced_pady, self.view.pady))


def pit_strategy_combo_event(self, idx_clicked):
for idx, var in enumerate(self.pit_number_vars):
if idx == idx_clicked:
Expand Down Expand Up @@ -203,4 +254,36 @@ def get_data(self):
"pit3_lap": self.pit3_lap,
}

return data
return data

def change_fuel_event(self, change):
if change == "plus" and self.fuel_load_laps < self.number_laps:
self.fuel_load_laps += 1

if change == "minus" and self.fuel_load_laps > 2:
self.fuel_load_laps -= 1

percentage = int((self.fuel_load_laps / self.number_laps) * 100)
self.fuel_load_progress_bar.set(percentage/100)

self.fuel_load_label.configure(text=f"Lap {self.fuel_load_laps} / {self.number_laps} ({percentage}%)")

# ensure we don't try to run more laps than fuel in car
if self.fuel_load_laps < self.number_laps_to_run:
self.number_laps_to_run = self.fuel_load_laps
self.change_laps_to_run_event(change=None)

def change_laps_to_run_event(self, change):
if change == "plus" and self.number_laps_to_run < 15:
if self.number_laps_to_run + 1 <= self.fuel_load_laps:
self.number_laps_to_run += 1

if change == "minus" and self.number_laps_to_run > 3:
self.number_laps_to_run -= 1

self.laps_to_run_progress_bar.set(self.number_laps_to_run/15)

self.number_laps_to_run_label.configure(text=f"{self.number_laps_to_run} Laps")

def send_out(self):
self.view.controller.send_player_car_out(self.driver_name, self.fuel_load_laps, self.number_laps_to_run)
8 changes: 4 additions & 4 deletions src/race_engine_view/timing_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def setup_labels(self):


# driver labels
customtkinter.CTkLabel(self.driver1_frame, text="Rosberg").grid(row=1, column=1, sticky="EW")
customtkinter.CTkLabel(self.driver2_frame, text="Schumacher").grid(row=1, column=1, sticky="EW")
customtkinter.CTkLabel(self.driver1_frame, text="Nico Rosberg").grid(row=1, column=1, sticky="EW")
customtkinter.CTkLabel(self.driver2_frame, text="Michael Schumacher").grid(row=1, column=1, sticky="EW")

self.driver1_fuel_label = customtkinter.CTkLabel(self.driver1_frame, text="0.0kg")
self.driver1_fuel_label.grid(row=2, column=1, sticky="EW")
Expand Down Expand Up @@ -153,11 +153,11 @@ def setup_widgets(self):
self.driver1_pit_vars = [self.driver1_pit1_var, self.driver1_pit2_var, self.driver1_pit3_var]

# STRATEGY EDITORS
self.strategy_editor_driver1 = strategy_editor.StrategyEditor(self.strategy_frame, self.view, "Rosberg", 1, 1)
self.strategy_editor_driver1 = strategy_editor.StrategyEditor(self.strategy_frame, self.view, "Nico Rosberg", 1, 1, self.session)

# hack to create a seperator
customtkinter.CTkFrame(self.strategy_frame, width=10).grid(column=6, row=1, rowspan=20, padx=(100, 10), sticky="NSEW")
self.strategy_editor_driver2 = strategy_editor.StrategyEditor(self.strategy_frame, self.view, "Schumacher", 7, 1)
self.strategy_editor_driver2 = strategy_editor.StrategyEditor(self.strategy_frame, self.view, "Michael Schumacher", 7, 1, self.session)
customtkinter.CTkFrame(self.strategy_frame, width=10).grid(column=12, row=1, rowspan=20, padx=50, sticky="NSEW")

def setup_plots(self):
Expand Down

0 comments on commit 457c9cd

Please sign in to comment.