Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress bar to running processes #99

Merged
merged 5 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/game_objects/process.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from math import sqrt
from math import floor, sqrt
from random import randint

from lib import event_manager
Expand All @@ -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
Expand Down Expand Up @@ -64,6 +65,19 @@ 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

@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:
Expand Down Expand Up @@ -187,6 +201,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):
Expand All @@ -200,10 +219,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:
Expand All @@ -214,14 +231,15 @@ 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(
Expand Down
13 changes: 13 additions & 0 deletions src/game_objects/views/process_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.is_progressing_to_happiness:
pygame.draw.rect(surface, Color.BLUE, pygame.Rect(
self._x + 2,
self._y + self.height - 4,
min(
(self.width - 4),
(self.width - 4)
- (5000 - self._process.current_state_duration)
* (self.width - 4) / 5000,
),
2
))
Loading