From d62edb7573903291afc8f1e0a9934f935a5ba229 Mon Sep 17 00:00:00 2001 From: Pier-Luc Brault Date: Sat, 2 Sep 2023 22:42:37 -0400 Subject: [PATCH 1/4] Add progress bar to running processes --- src/game_objects/process.py | 4 ++++ src/game_objects/views/process_view.py | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/game_objects/process.py b/src/game_objects/process.py index 45077b88..dcfa108f 100644 --- a/src/game_objects/process.py +++ b/src/game_objects/process.py @@ -64,6 +64,10 @@ def starvation_level(self): def display_blink_color(self): return self._display_blink_color + @property + def current_state_duration(self): + return self._current_state_duration + def use_cpu(self): if not self.has_cpu: for cpu in self._process_manager.cpu_list: diff --git a/src/game_objects/views/process_view.py b/src/game_objects/views/process_view.py index b7a45f56..042f67c1 100644 --- a/src/game_objects/views/process_view.py +++ b/src/game_objects/views/process_view.py @@ -87,3 +87,16 @@ def draw(self, surface): if self._process.is_waiting_for_io: surface.blit(_waiting_for_io_emoji, (self._x + 27, self._y + 32)) + + if self._process.has_cpu and not self._process.is_blocked and not self._process.has_ended: + pygame.draw.rect(surface, Color.BLUE, pygame.Rect( + self._x + 2, + self._y + self.height - 4, + min( + (self.width - 4), + (self.width - 4) + - (5 - self._process.current_state_duration) + * (self.width - 4) / 5, + ), + 2 + )) From fa38550bbd02b38befd38ff725fba22ef266844b Mon Sep 17 00:00:00 2001 From: Pier-Luc Brault Date: Sat, 2 Sep 2023 23:08:45 -0400 Subject: [PATCH 2/4] Update progress bar continuously --- src/game_objects/process.py | 20 ++++++++++++-------- src/game_objects/views/process_view.py | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/game_objects/process.py b/src/game_objects/process.py index dcfa108f..a63e5493 100644 --- a/src/game_objects/process.py +++ b/src/game_objects/process.py @@ -1,4 +1,4 @@ -from math import sqrt +from math import floor, sqrt from random import randint from lib import event_manager @@ -21,6 +21,7 @@ def __init__(self, pid, game): self._starvation_level = 1 self._last_update_time = 0 + self._last_event_check_time = 0 self._current_state_duration = 0 self._display_blink_color = False @@ -191,6 +192,11 @@ def _on_click(self): self.toggle() def update(self, current_time, events): + self._current_state_duration += current_time - self._last_update_time + self._last_update_time = current_time + + current_state_duration_seconds = floor(self._current_state_duration / 1000) + if not self._check_if_in_motion(): for event in events: if self._check_if_clicked_on(event): @@ -204,10 +210,8 @@ def update(self, current_time, events): pages_in_swap += 1 self._set_waiting_for_page(pages_in_swap > 0) - if current_time >= self._last_update_time + 1000: - self._last_update_time = current_time - - self._current_state_duration += 1 + if current_time >= self._last_event_check_time + 1000: + self._last_event_check_time = current_time if self.has_cpu and not self.is_blocked: if randint(1, 100) <= self._io_probability_numerator: @@ -218,14 +222,14 @@ def update(self, current_time, events): new_page.in_use = True event_manager.event_page_new( new_page.pid, new_page.idx, new_page.in_swap, new_page.in_use) - elif self._current_state_duration >= 1 and randint(1, 100) == 1: + elif current_state_duration_seconds >= 1 and randint(1, 100) == 1: self._terminate_gracefully() - elif self._current_state_duration == 5: + elif current_state_duration_seconds == 5: self._starvation_level = 0 event_manager.event_process_starvation(self._pid, self._starvation_level) else: - if self._current_state_duration > 0 and self._current_state_duration % 10 == 0: + if current_state_duration_seconds > 0 and current_state_duration_seconds % 10 == 0: if self._starvation_level < 5: self._starvation_level += 1 event_manager.event_process_starvation( diff --git a/src/game_objects/views/process_view.py b/src/game_objects/views/process_view.py index 042f67c1..d79c66d7 100644 --- a/src/game_objects/views/process_view.py +++ b/src/game_objects/views/process_view.py @@ -95,8 +95,8 @@ def draw(self, surface): min( (self.width - 4), (self.width - 4) - - (5 - self._process.current_state_duration) - * (self.width - 4) / 5, + - (5000 - self._process.current_state_duration) + * (self.width - 4) / 5000, ), 2 )) From 85be2497ef473d5a17ec63c70afc06421441852b Mon Sep 17 00:00:00 2001 From: Pier-Luc Brault Date: Sat, 2 Sep 2023 23:36:41 -0400 Subject: [PATCH 3/4] Add is_progresisng_to_happiness property to Process --- src/game_objects/process.py | 9 +++++++++ src/game_objects/views/process_view.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/game_objects/process.py b/src/game_objects/process.py index a63e5493..77c71fd0 100644 --- a/src/game_objects/process.py +++ b/src/game_objects/process.py @@ -69,6 +69,15 @@ def display_blink_color(self): def current_state_duration(self): return self._current_state_duration + @property + def is_progressing_to_happiness(self): + return ( + self.has_cpu + and self.starvation_level > 0 + and not self.is_blocked + and not self.has_ended + ) + def use_cpu(self): if not self.has_cpu: for cpu in self._process_manager.cpu_list: diff --git a/src/game_objects/views/process_view.py b/src/game_objects/views/process_view.py index d79c66d7..22720f88 100644 --- a/src/game_objects/views/process_view.py +++ b/src/game_objects/views/process_view.py @@ -88,7 +88,7 @@ def draw(self, surface): if self._process.is_waiting_for_io: surface.blit(_waiting_for_io_emoji, (self._x + 27, self._y + 32)) - if self._process.has_cpu and not self._process.is_blocked and not self._process.has_ended: + if self._process.is_progressing_to_happiness: pygame.draw.rect(surface, Color.BLUE, pygame.Rect( self._x + 2, self._y + self.height - 4, From 58120a7fac2e631e036e73c4a299d6374e8100a7 Mon Sep 17 00:00:00 2001 From: Pier-Luc Brault Date: Sun, 3 Sep 2023 00:33:16 -0400 Subject: [PATCH 4/4] Fix pylint error --- src/game_objects/process.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/game_objects/process.py b/src/game_objects/process.py index 77c71fd0..5cbc2d55 100644 --- a/src/game_objects/process.py +++ b/src/game_objects/process.py @@ -238,7 +238,8 @@ def update(self, current_time, events): event_manager.event_process_starvation(self._pid, self._starvation_level) else: - if current_state_duration_seconds > 0 and current_state_duration_seconds % 10 == 0: + if (current_state_duration_seconds > 0 + and current_state_duration_seconds % 10 == 0): if self._starvation_level < 5: self._starvation_level += 1 event_manager.event_process_starvation(