-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloor.py
137 lines (112 loc) · 4.09 KB
/
floor.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
from debug import Debug as DB
class ButtonPressed():
"""
Represents a class that contains information about the buttons pressed on a floor
:param move_up: Whether the move up button is pressed
:type move_up: bool
:param move_down: Whether the move down button is pressed
:type move_down: bool
:param last_pressed_up: The last time the move up button was pressed
:type last_pressed_up: int
:param last_pressed_down: The last time the move down button was pressed
:type last_pressed_down: int
"""
def __init__(self):
self.move_up = False
self.move_down = False
self.last_pressed_up = -1
self.last_pressed_down = -1
def __str__(self) -> str:
return DB.str(
"Class", "ButtonPressed", kwargs=[
self.move_down, self.move_up], desc=[
"move_down", "move_up"])
def set_move_up(self, value, time):
"""
Set the move up button to the given value and update the last pressed time
:param value: The value to set the button to
:type value: bool
:param time: The current time
:type time: int
"""
if (value and not self.move_up):
self.last_pressed_up = time
elif (not value):
self.last_pressed_up = -1
self.move_up = value
def set_move_down(self, value, time):
"""
Set the move down button to the given value and update the last pressed time
:param value: The value to set the button to
:type value: bool
:param time: The current time
:type time: int
"""
if (value and not self.move_down):
self.last_pressed_down = time
elif (not value):
self.last_pressed_down = -1
self.move_down = value
def update_time(self, direction, time):
if direction == 1:
self.last_pressed_up = time
else:
self.last_pressed_down = time
class Floor():
"""
Represents a floor in the building
:param number: The number of the floor
:type number: int
:param passenger_list: The list of passengers on the floor
:type passenger_list: list
:param button_pressed: The buttons pressed on the floor
:type button_pressed: ButtonPressed
"""
def __init__(self, number):
self.passenger_list = []
self.button_pressed = ButtonPressed()
self.number = number
def __str__(self) -> str:
return DB.str(
"Class", "Floor", kwargs=[
self.passenger_list, self.button_pressed, self.number], desc=[
"passenger list", "buttons pressed", "number"])
def spawn_passenger(self, passenger, time):
"""
Adds a passenger to the floor and updates the floor buttons
:param passenger: The passenger to add
:type passenger: Passenger
:param time: The current time
:type time: int
"""
if (DB.flr_passenger_appended):
DB.pr("Class", "Floor", message="passenger appended")
self.passenger_list.append(passenger)
# Do not update time since no elevator arrived, only spawn
self.update_floor_buttons(time)
def remove_passenger(self, passenger, time):
"""
Removes a passenger from the floor and updates the floor buttons
:param passenger: The passenger to remove
:type passenger: Passenger
:param time: The current time
:type time: int
"""
self.passenger_list.remove(passenger)
# Update Time since elevator arrived
self.update_floor_buttons(time)
def update_floor_buttons(self, time):
"""
Updates the floor buttons based on the passengers on the floor
:param time: The current time
:type time: int
"""
up = False
down = False
for p in self.passenger_list:
if (p.end_level > self.number):
up = True
else:
down = True
self.button_pressed.set_move_up(up, time)
self.button_pressed.set_move_down(down, time)