-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.cpp
408 lines (361 loc) · 9.91 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//
// Created by Admin on 6/18/2021.
//
#include "Controller.h"
#include "Cruiser.h"
#include "Freighter.h"
#include <fstream>
#include <algorithm>
Controller::Controller(View *viewPtr) {
_view_ptr = viewPtr;
_commandList.emplace_back("create"); // 1
_commandList.emplace_back("status"); // 2
_commandList.emplace_back("go"); // 3
_commandList.emplace_back("show"); // 4
_commandList.emplace_back("pan"); // 5
_commandList.emplace_back("zoom"); // 6
_commandList.emplace_back("size"); // 7
_commandList.emplace_back("default"); // 8
_commandList.emplace_back("course"); // 9
_commandList.emplace_back("position"); // 10
_commandList.emplace_back("destination"); // 11
_commandList.emplace_back("load_at"); // 12
_commandList.emplace_back("unload_at"); // 13
_commandList.emplace_back("dock_at"); // 14
_commandList.emplace_back("refuel"); // 15
_commandList.emplace_back("attack"); // 16
_commandList.emplace_back("stop"); // 17
}
void Controller::run() {
bool sea_craft_cmd = true;
vector<string> cmd;
int index;
while (true) {
sea_craft_cmd = true;
std::cout << "Time " << _time << ": Enter command: ";
//get Command and store it in the variable command
string command;
getline(cin, command);
cmd = tokenize(command, ' ');
if (cmd.size() == 1 && cmd[0] == "exit")
return;
//get the first word in the string command
switch (getCommandIndex(command.substr(0, command.find(' ')))) {
/* this switch case is for the View & Model*/
case 0:
cout << "create " << endl;
//command CreateObject
sea_craft_cmd = false;
CreateObject(command);
break;
case 1:
cout << "status" << endl;
sea_craft_cmd = false;
//command get status
//todo add virtual method print status to simObject
Model::get_Instance().status();
break;
case 2:
cout << "go" << endl;
sea_craft_cmd = false;
// command go
Model::get_Instance().update();
_time++;
break;
case 3:
cout << "show" << endl;
sea_craft_cmd = false;
_view_ptr->show();
break;
case 4:
cout << "pan " << endl;
sea_craft_cmd = false;
pan(command);
break;
case 5:
cout << "zoom" << endl;
sea_craft_cmd = false;
zoom(command);
break;
case 6:
cout << "size " << endl;
sea_craft_cmd = false;
size(command);
break;
case 7:
cout << "default" << endl;
sea_craft_cmd = false;
defaultSettings();
break;
default:
break;
}
/* if the command is for the sea crafts then
* the next switch case will work */
if (sea_craft_cmd) {
switch (getCommandIndex(cmd[1])) {
case 8:
cout << "course" << endl;
cmd = tokenize(command, ' ');
if (cmd.size() != 4) {
cerr << "command not valid ! " << endl;
return;
}
try {
index = Model::get_Instance().getShipByName(
cmd[0].substr(0, 2));
Model::get_Instance().getShipVector()[index]->setAngle(
stod(cmd[2]));
Model::get_Instance().getShipVector()[index]->setSpeed(
stod(cmd[3]));
} catch (...) {
cerr << "command not valid ! " << endl;
return;
}
break;
case 9:
cout << "position " << endl;
if (cmd.size() != 5) {
cerr << "command not valid ! " << endl;
return;
}
try {
// ship position 1 3 200
Model::get_Instance().position(cmd[0].substr(0, 2),
stod(cmd[2]), stod(cmd[3]), stod(cmd[4]));
} catch (...) {
cerr << "command not valid ! " << endl;
return;
}
break;
case 10:
cout << "destination" << endl;
if (cmd.size() != 4) {
cerr << "command not valid ! " << endl;
return;
}
try {
// Emma destination PearlHarbor 10
Model::get_Instance().destination(cmd[0].substr(0, 2),
cmd[2].substr(0, 2), stod(cmd[3]));
} catch (...) {
cerr << "command not valid ! " << endl;
return;
}
break;
case 11:
// Emma load_at PearlHarbor
cout << "load_at" << endl;
if (cmd.size() != 3) {
cerr << "command not valid ! " << endl;
return;
}
try {
Model::get_Instance().load_at(cmd[0].substr(0, 2),
cmd[2].substr(0, 2));
} catch (...) {
cerr << "command not valid ! " << endl;
return;
}
break;
case 12:
// Emma unload_at Valdez 100
cout << "unload_at" << endl;
if (cmd.size() != 4) {
cerr << "command not valid ! " << endl;
return;
}
try {
Model::get_Instance().unload_at(cmd[0].substr(0, 2),
cmd[2].substr(0, 2), stoi(cmd[3]));
} catch (...) {
cerr << "command not valid ! " << endl;
return;
}
break;
case 13:
// Emma dock_at PearlHarbor
cout << "dock_at" << endl;
if (cmd.size() != 3) {
cerr << "command not valid ! " << endl;
return;
}
try {
index = Model::get_Instance().getShipByName(
cmd[0].substr(0, 2));
Model::get_Instance().destination(cmd[0].substr(0, 2),
cmd[2].substr(0, 2),
Model::get_Instance().getShipVector()[index]->getSpeed());
} catch (...) {
cerr << "command not valid ! " << endl;
return;
}
break;
case 14:
// Emma refuel
cout << "refuel" << endl;
if (cmd.size() != 2) {
cerr << "command not valid ! " << endl;
return;
}
try {
// port, cruiser, freighter, patrol
index = Model::get_Instance().getShipByName(
cmd[0].substr(0, 2));
if (Model::get_Instance().getShipVector()[index]->getType()
== port
|| Model::get_Instance().getShipVector()[index]->getType()
== cruiser) {
cerr << "command not valid ! " << endl;
return;
}
/* there is no need to use the refuel for the patrol boat
* because the update function of the ports & Boat_patrol will do the job */
if (Model::get_Instance().getShipVector()[index]->getType()
== freighter) {
dynamic_cast<Freighter*>(Model::get_Instance().getShipVector()[index].get())->refuel(
900);
/* 900 is the full capacity of the fuel tank of the freighter ship*/
}
} catch (...) {
cerr << "command not valid ! " << endl;
return;
}
break;
case 15:
// Emma attack ship
cout << "attack" << endl;
if (cmd.size() != 3) {
cerr << "command not valid ! " << endl;
return;
}
try {
// port, cruiser, freighter, patrol
index = Model::get_Instance().getShipByName(
cmd[0].substr(0, 2));
Model::get_Instance().getShipByName(cmd[2]); // just to check if the is found (exists)
if (Model::get_Instance().getShipVector()[index]->getType()
== cruiser) {
dynamic_cast<Cruiser*>(Model::get_Instance().getShipVector()[index].get())->attack(
cmd[2]);
} else {
cerr << "command not valid ! " << endl;
return;
}
} catch (...) {
cerr << "command not valid ! " << endl;
return;
}
break;
case 16:
break;
default:
/* if the command isn't for the view,model or sea crafts
* then the command isn't valid */
cerr << "Invalid command ! " << endl;
return;
}
}
}
}
void Controller::CreateObject(string command) {
char d1, d2, d3;
int arg1, arg2;
double x, y;
string create, name, type;
stringstream ss(command);
if ((ss >> create >> name >> type >> d1 >> x >> d2 >> y >> d3 >> arg1)
&& d1 == '(' && d2 == ',' && d3 == ')' && create == "create") { // verify that separators are correct
if (type == "Port") {
ss >> arg2;
Model::get_Instance().addPort(name, x, y, arg1, 0, arg2);
} else if (type == "Cruiser") {
ss >> arg2;
Model::get_Instance().addCruiser(name, Point(x, y), arg1, arg2);
} else if (type == "Freighter") {
ss >> arg2;
Model::get_Instance().addFreighter(name, Point(x, y), arg1, arg2);
} else if (type == "Patrol_boat")
Model::get_Instance().addBoatPatrol(name, Point(x, y), arg1);
else
throw "Type not legal";
}
}
void Controller::init(std::string Filepath) {
ifstream inData;
inData.open(Filepath); // opens the file
//check if file is open
if (!inData) {
cerr << "Error: file could not be opened" << endl;
exit(1);
}
//CreateObject variable to house data in file
char d1, d2, d3;
int newFPH, CurrentFuel;
double x, y;
string portName;
while (!inData.eof()) { // keep reading until end-of-file
//check if line is in the correct format and add the port accordingly
if ((inData >> portName >> d1 >> x >> d2 >> y >> d3 >> CurrentFuel
>> newFPH) && d1 == '(' && d3 == ')' && d2 == ',') // verify that separators are correct
Model::get_Instance().addPort(portName, x, y, CurrentFuel, 0,
newFPH);
}
//close file
inData.close();
}
int Controller::getCommandIndex(string first) {
return distance(_commandList.begin(),
find_if(_commandList.begin(), _commandList.end(),
[&](const string &val) {
return (first == val);
}));
}
const list<string>& Controller::getCommandList() const {
return _commandList;
}
void Controller::setCommandList(const list<string> &commandList) {
_commandList = commandList;
}
int Controller::getTime() const {
return _time;
}
void Controller::setTime(int time) {
_time = time;
}
View* Controller::getViewPtr() const {
return _view_ptr;
}
void Controller::setViewPtr(View *viewPtr) {
_view_ptr = viewPtr;
}
void Controller::pan(string command) {
char d1, d2, d3;
double x, y;
string commandName;
stringstream ss(command);
if ((ss >> commandName >> d1 >> x >> d2 >> y >> d3) && d1 == '('
&& d2 == ',' && d3 == ')') { // verify that separators are correct
if (commandName == "pan")
_view_ptr->pan(Point(x, y));
}
}
void Controller::zoom(string command) {
double y;
string commandName;
stringstream ss(command);
if (ss >> commandName >> y) // verify that separators are correct
if (commandName == "zoom")
_view_ptr->zoom(y);
}
void Controller::size(string command) {
double y;
string commandName;
stringstream ss(command);
if (ss >> commandName >> y) // verify that separators are correct
if (commandName == "size")
_view_ptr->size(y);
}
void Controller::defaultSettings() {
_view_ptr->defaultSettings();
}