-
Notifications
You must be signed in to change notification settings - Fork 1
/
Controller.cpp
326 lines (266 loc) · 9.06 KB
/
Controller.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "Controller.h"
Controller::Controller(int argc, char** argv):_commandsMap(),_model(Model::get()),_view(make_shared<View>()),_files() {
_files.insert(_files.end(),argv, argv + argc);
checkWareHouse(); // check and add the warehouses.
checkTrucks(); // check and add the trucks.
mapInit();
cout << fixed << showpoint;
cout << setprecision(3);
}
void Controller::getCommand() {
string command;
cout << "Time " << _model.getTime() << " Enter Command " << endl;
getline(cin,command);
analyzeCmd(command);
}
void Controller::analyzeCmd(string& command) {
string cmd;
stringstream ss(command);
if (command == "exit")
exit(0);
ss >> cmd; // getting the first command
try
{
if (_model.findVehicle(cmd)) { // if first word in the command is vehicle name
vehicleCmd(ss, cmd);
return;
}
doCommand(ss, cmd);
}
catch(MyException& e){}
catch(ExitException& e){}
catch(exception &e)
{
cerr << "Invalid Arguments"<< endl;
}
}
void Controller::run() {
while (true){
_view->update();
getCommand();
}
}
void Controller::vehicleCmd(stringstream& ss, string& vehicleName) {
string cmd;
ss >> cmd;
vehiclePtr vehicle = _model.findVehicle(vehicleName);
if (!vehicle)
throw MyException("Vehicle Not Found");
switch (_commandsMap.at(cmd)) {
case 0: // Course
{
if(vehicle->getType() != "Chopper")
throw MyException("Function only valid for Choppers");
int deg, speed;
ss >> deg >> speed;
deg = (450 - deg) % 360;
if(speed > 170)
throw MyException("Maximum speed for Chopper is 170 KM/H");
_model.course(deg, speed, vehicleName);
break;
}
case 1: // Position
{
string x, y, speed;
ss >> x >> y;
stoi(x); // checking if valid input
stoi(y);
if (vehicle->getType() == "Chopper") {
ss >> speed;
if(stoi(speed) > 170)
throw MyException("Maximum speed for Chopper is 170 KM/H");
_model.position(x, y, vehicle,stoi(speed));
return;
}
_model.position(x, y, vehicle);
break;
}
case 2: // Destination
{
string wareHouse;
ss >> wareHouse;
if (!_model.findWareHouse(wareHouse))
throw MyException("WareHouse Not Found");
_model.destination(wareHouse, vehicle);
break;
}
case 3: // Attack
{
string truckName;
ss >> truckName;
_model.attack(truckName,vehicle);
break;
}
case 4: // Stop
{
_model.stop(vehicle);
break;
}
}
}
void Controller::checkWareHouse() {
string line, name, corX, corY, file;
int inventory;
//getting iterator to point at warehouse
for(auto iter = _files.begin(); iter != _files.end(); iter++) {
if (*iter == "-w") {
file = *++iter; // iter is now on warehouse file.
break;
}
}
ifstream wareFile(file);
while(getline(wareFile,line)){
try {
stringstream ss(line);
ss >> name >> corX >> corY >> inventory;
name = name.substr(0, name.size() - 1);
corX = corX.substr(1, corX.size() - 2);
corY = corY.substr(0, corY.size() - 2);
Point warePoint(stof(corX), stof(corY));
_model.addWareHouse(name,warePoint,inventory);
}
catch (...){ // catching stof exception
cerr << "Invalid WareHouse file" << endl;
exit(1);
}
}
_model.addWareHouse("Frankfurt",Point(40,10),100000);
}
void Controller::doCommand(stringstream& ss, string &cmd) {
switch (_commandsMap.at(cmd)) {
case 5: {
_view->setDefault();
break;
}
case 6: {
int size;
ss >> size;
if(ss.fail())
throw MyException("Not an integer");
if(size < 6 || size > 30)
throw MyException("This size is not allowed.");
_view->setSize(size);
break;
}
case 7: {
float scale;
ss >> scale;
if(scale < 0)
throw MyException("Map scale must be positive.");
_view->setScale(scale);
break;
}
case 8: {
int orgX;
int orgY;
ss >> orgX >> orgY;
if(ss.fail())
throw MyException("Not an integer");
_view->setOriginX(orgX);
_view->setOriginY(orgY);
break;
}
case 9: {
_view->print();
break;
}
case 10: {
_model.getStatus();
break;
}
case 11: {
string vehicleName, type, corX, corY, startWarehouse;
ss >> vehicleName >> type;
if (vehicleName.size() > 12)
throw MyException("Vehicle name too long");
if (type == "Chopper") {
ss >> corX >> corY;
if(corX[0] != '(' || corY[corY.size()-1] != ')')
throw MyException("Not correct format");
corX = corX.substr(1, corX.size() - 2);
corY = corY.substr(0, corY.size() - 1);
Point startPoint(stof(corX), stof(corY));
_model.createChopper(vehicleName, startPoint);
break;
}
ss >> startWarehouse;
_model.createTrooper(vehicleName, startWarehouse);
break;
}
case 12:{
_model.go();
}
}
}
void Controller::mapInit()
{
_commandsMap.insert({"course",0});
_commandsMap.insert({"position",1});
_commandsMap.insert({"destination",2});
_commandsMap.insert({"attack",3});
_commandsMap.insert({"stop",4});
_commandsMap.insert({"default",5});
_commandsMap.insert({"size",6});
_commandsMap.insert({"zoom",7});
_commandsMap.insert({"pan",8});
_commandsMap.insert({"show",9});
_commandsMap.insert({"status",10});
_commandsMap.insert({"create",11});
_commandsMap.insert({"go",12});
}
void Controller::checkTrucks() {
string line, truckName, nextStop, arriveTime, leaveTime, prevLeaveTime, startingPoint;
int index, crates = 0;
double time;
vector < pair<string, pair<double, int> > > routes; // hold the rout of the truck.
vector< pair<string,string> > times; // holds arrive times and leave times.
index = getFirstTruckIndex(); // gets the truck file index.
while(index != _files.size()){
ifstream truckFile(_files[index]); // getting next file.
parseFirstLine(line ,index, truckName, startingPoint, truckFile); // name and first line.
stringstream ss(line);
ss >> startingPoint;
prevLeaveTime = "00:00";
while (getline(truckFile,line)){
parseLine(line, nextStop, arriveTime, crates, leaveTime);
_model.findWareHouse(nextStop);
time = getTime(prevLeaveTime,arriveTime , leaveTime); // calculate travel time to dest.
prevLeaveTime = leaveTime;
routes.emplace_back(nextStop, make_pair(time,crates)); //adds next destination in correct order.
times.emplace_back(arriveTime,leaveTime);
}
_model.addTruck(startingPoint, truckName, routes, times);
index++;
}
}
int Controller:: getFirstTruckIndex(){
int index = 0;
for(auto & _file : _files) {
index++;
if (_file == "-t")
return index; // index is now on first truck file.
}
return index;
}
double Controller::getTime( string &startTime, string &endTime, string & leaveTime) {
startTime.erase(remove(startTime.begin(), startTime.end(), ':'), startTime.end());
endTime.erase(remove(endTime.begin(), endTime.end(), ':'), endTime.end());
leaveTime.erase(remove(leaveTime.begin(), leaveTime.end(), ':'), leaveTime.end());
int sTime = stoi(startTime);
int eTime = stoi(endTime);
// turning times into minutes
int first_time_min = sTime / 100 * 60 + sTime % 100;
int second_time_min = eTime / 100 * 60 + eTime % 100;
double diff_time_min = second_time_min - first_time_min;
return diff_time_min / 60;
}
void Controller::parseLine(string &line, string &nextStop, string &arriveTime, int &crates, string &leaveTime) {
std::replace( line.begin(), line.end(), ',', ' ');
stringstream ss(line);
ss >> nextStop >> arriveTime >> crates >> leaveTime;
}
void Controller::parseFirstLine(string& line, int index, string &truckName, string &startingPoint, ifstream &truckFile) {
truckName = _files[index].substr(0,_files[index].size()-4); // name of truck
getline(truckFile,line);
std::replace( line.begin(), line.end(), ',', ' ');
}