Passed
Push — main ( 38b826...e5e666 )
by Rahn20
03:51
created

simulation.Simulation.end()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
#!/usr/bin/python3
2
3
"""
4
Mainprogram for scooter simulation
5
"""
6
7
8
import time
9
from src.scooter import Scooter
10
from src.api import ApiData
11
from main import Handler
12
13
14
15
class Simulation():
16
    """ Simulation class """
17
18
    # saves scooter id in an array
19
    _scooter_id = []
20
21
    # saves log id in an array
22
    _log_id = []
23
24
    # saves station id in an array
25
    _station_id = []
26
27
    # saves city info in an array
28
    _city_data = []
29
30
    def __init__(self):
31
        """ Initialize class """
32
        self.scooter = Scooter()
33
        self.handler = Handler()
34
        self.api = ApiData(1)
35
36
37
    def move(self) -> None:
38
        """ Move scooters around the cities. Simulation time is 2 minutes. """
39
        end_time = time.time() + 120    # 2 minutes
40
41
        print("\nStep 2 - Moving scooters . . . . . . . .")
42
43 View Code Duplication
        while time.time() <= end_time:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
44
            count = 0
45
46
            while count < len(self._scooter_id):
47
                data = self.api.get_scooter_data(self._scooter_id[count])
48
49
                self.scooter.add_scooter_to_dict(data)
50
                self.scooter.add_city_to_dict(self._city_data[count])
51
                self.scooter.set_station_id(self._station_id[count])
52
                
53
                count = self.run(count)
54
55
                count += 1
56
                time.sleep(0.003)
57
58
59
    def run(self, count: int) -> int:
60
        """
61
        Check if the scooter is inside the city and check battery level, if TRUE
62
        the scooter will be returned otherwise the scooter will continue moving.
63
        """
64
        id = self._scooter_id[count]
65
66
        if self.scooter.check_scooter_in_city() is False:
67
            print("\nScooter {} is outside of the city\n".format(id))
68
            print("Scooter {} will be returned.".format(id))
69
70
            self.end(count)
71
72
            del self._scooter_id[count]
73
            del self._log_id[count]
74
            del self._station_id[count]
75
            del self._city_data[count]
76
77
            count -= 1
78
        elif self.scooter.check_battery():
79
            print("\n\033[1;31m*\033[1;0m Low battery!! the scooter {} needs to be charged.".format(id))
80
            print("Scooter {} will be returned.".format(id))
81
82
            self.end(count)
83
84
            del self._scooter_id[count]
85
            del self._log_id[count]
86
            del self._station_id[count]
87
            del self._city_data[count]
88
89
            count -= 1
90
        else:
91
            self.scooter.change_location()
92
            self.scooter.move_scooter()
93
            self.api.update_scooter()
94
95
        return count
96
97
    def end(self, count):
98
        """
99
        Scooter will be checked if it's outside the city/has low battery level or need
100
        meantience. The scooter will be returned and the log will be uppdated.
101
        """
102
        self.handler.end_rental()
103
        self.api.update_scooter()
104
105
        self.api.log_id = self._log_id[count]
106
        self.api.update_log()
107
108
109
110
    def return_scooters(self):
111
        """ All scooters will be returned when simulation time ends. """
112
        count = 0
113
114
        print("\nStep 3 - Returning scooters . . . . . . . .")
115
        print("\nThe simulation has finished, the scooters will be returned.\n")
116
117 View Code Duplication
        while count < len(self._scooter_id):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
118
            data = self.api.get_scooter_data(self._scooter_id[count])
119
120
            self.scooter.add_scooter_to_dict(data)
121
            self.scooter.add_city_to_dict(self._city_data[count])
122
            self.scooter.set_station_id(self._station_id[count])
123
            self.end(count)
124
125
            count += 1
126
            time.sleep(0.003)
127
128
129
    def main(self, total:int):
130
        """ Start simulation program. """
131
        print("\n************ Welcome to Scooter simulation program **************\n")
132
        print("\nThe simulation takes around 3 minutes, do not try to break/stop the program.\n")
133
        print("\nStep 1 - Renting scooters . . . . . . . .")
134
135
        all_customers = self.api.get_all_customers()
136
137
        count = 1
138
        customer = 0
139
140
        while count <= total:
141
            data = self.api.get_scooter_data(scooter_id = count)
142
143
            if self.scooter.check_scooter_status(data):
144
                # get city
145
                city = self.api.get_city_data()
146
                self.scooter.add_city_to_dict(city)
147
                self._city_data.append(city)
148
                self._scooter_id.append(count)
149
150
                # create log
151
                self.api.user_id = str(all_customers[customer]["id"])
152
                self.api.create_log()
153
                self._log_id.append(self.api.log_id)
154
155
                # get station
156
                zone = self.scooter.get_zone_id()
157
                station = self.api.get_station(zone)
158
                self._station_id.append(station)
159
160
                customer += 1
161
            else:
162
                print("\n\033[1;31m*\033[1;0m Scooter {} is not available.\n".format(count))
163
164
            count += 1
165
166
        self.move()
167
        self.return_scooters()
168
169
170
171
if __name__ == "__main__":
172
    Simulation().main(1000)