-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatient.py
161 lines (119 loc) · 5.38 KB
/
patient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import threading
import random
import time
from datetime import datetime
from Location import Location
import Statistics
from Phase import Phase
class Patient(threading.Thread):
max_hp = 100
def __init__(self, id, hp, name, receptionists, chairs, statistics):
super(Patient, self).__init__()
self.id = id
self.name = name
self.health_points = hp
self.receptionists = receptionists
self.chairs = chairs
self.phase = Phase.START
self.points = self.max_hp - hp
self.doctors = []
self.doctors_needed = 0
self.current_doctors_number = 0
self.current_receptionist = None
self.time_to_start_waiting_for_the_surgery = None
self.surgery_room = None
self.number_of_queue_change = 0
self.current_chair = None
self.lock = threading.Lock()
self.statistics: Statistics = statistics
self.kill = False
# Thread method
# 1) Patient should first choose queue to receptionist
# 2) Patient should register himself
# 3) Patient should choose the chair
# 4) Patient should waiting to surgery
def run(self):
while 0 < self.health_points < Patient.max_hp and not self.kill:
self.lock.acquire()
if self.phase == Phase.START:
self.statistics.new_patient()
time.sleep(random.uniform(1, 3))
self.queue_selection()
self.phase = Phase.QUEUE
if self.phase == Phase.QUEUE:
if self.current_receptionist.current_patient != self.id:
self.behavior_in_the_registration_queue()
time.sleep(random.uniform(1, 3))
else:
self.current_receptionist.current_patient_name = self.name
self.phase = Phase.REGISTRATION
if self.phase == Phase.REGISTRATION:
if self.points < 100:
self.points += 10
time.sleep(random.uniform(1, 3))
else:
self.doctors_needed = self.current_receptionist.registration(
self.health_points)
self.phase = Phase.CHAIR_SELECION
if self.phase == Phase.CHAIR_SELECION:
for chair in self.chairs:
if chair.sit_down(self):
self.current_chair = chair
self.current_receptionist.exit_registration(self.id)
self.time_to_start_waiting_for_the_surgery = datetime.now()
self.phase = Phase.WAITING_FOR_SURGERY
break
time.sleep(random.uniform(1, 3))
if self.phase == Phase.WAITING_FOR_SURGERY:
time.sleep(random.uniform(1, 3))
if self.phase == Phase.SURGERY:
if self.current_chair:
self.current_chair.release_chair()
self.current_chair = None
time.sleep(random.uniform(1, 3))
self.manage_patient_health_point()
if self.health_points >= self.max_hp or self.health_points <= 0:
for doctor in self.doctors:
doctor.choosen_patient = None
if doctor.surgery_room:
doctor.surgery_room.complete_surgery()
doctor.surgery_room = None
doctor.location = Location.CORRIDOR
def manage_patient_health_point(self):
if self.health_points >= Patient.max_hp:
self.phase = Phase.HEALED
if self.phase != Phase.DEAD and self.phase != Phase.HEALED and self.phase != Phase.SURGERY:
self.health_points -= 1
if self.health_points <= 0:
if self.phase == Phase.QUEUE or self.phase == Phase.REGISTRATION:
self.current_receptionist.exit_queue(self.id)
if self.current_receptionist.get_position_in_queue(self.id) == 0:
self.current_receptionist.exit_registration(self.id)
elif self.phase == Phase.CHAIR_SELECION:
if self.current_receptionist.get_position_in_queue(self.id) == 0:
self.current_receptionist.exit_registration(self.id)
if self.current_chair:
self.current_chair.release_chair()
self.current_chair = None
self.phase = Phase.DEAD
if self.phase == Phase.HEALED:
self.statistics.patient_healed()
if self.phase == Phase.DEAD:
self.statistics.patient_died()
self.lock.release()
def queue_selection(self):
self.current_receptionist = min(
self.receptionists, key=lambda r: r.get_length_queue())
self.current_receptionist.join_queue(self.id)
def behavior_in_the_registration_queue(self):
for receptionist in self.receptionists:
if self.current_receptionist.get_position_in_queue(self.id) \
- self.number_of_queue_change > receptionist.get_length_queue():
self.change_queue(receptionist)
self.number_of_queue_change += 1
break
def change_queue(self, new_queue):
if self.current_receptionist:
self.current_receptionist.exit_queue(self.id)
self.current_receptionist = new_queue
self.current_receptionist.join_queue(self.id)