-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameControl.cpp
223 lines (188 loc) · 6.07 KB
/
GameControl.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
//
// Benjy Berkowicz - 336182589
// Advanced Programming 2016-2017 Bar Ilan
//
#include <cstring>
#include "GameControl.h"
#include "LuxuryTaxi.h"
#include "StandardTaxi.h"
#include "Socket.h"
#include "Udp.h"
#include "Serializer.h"
#include "Tcp.h"
using namespace std;
GameControl::GameControl() {
gridmap = 0;
dispatcher = 0;
}
void GameControl::addTaxi(std::string input) {
int id, type;
CarMaker manufacturer;
Color color;
// Parse and tokenize the input
vector<string> tokens = tokenizeByChar(input, ',');
Taxi * t;
// We begin generating the input required to build our taxi
id = atoi(tokens[0].c_str());
type = atoi(tokens[1].c_str());
manufacturer = (CarMaker)enumFromString(tokens[2], 'T');
color = (Color)enumFromString(tokens[3], 'C');
// The taxi is generated and added to the dispatcher
if (type == 1) {
t = new StandardTaxi(id, manufacturer, color);
} else {
t = new LuxuryTaxi(id, manufacturer, color);
}
dispatcher->addTaxi(t);
}
void GameControl::getGeneralInput() {
Point dimensions;
int numOfObstacles;
string input;
char dummy;
// We get the first line required for general input
getline(cin, input);
// The line is tokenized
vector<string> tokens = tokenizeByChar(input, ' ');
dimensions.x = atoi(tokens[0].c_str());
dimensions.y = atoi(tokens[1].c_str());
// The number of obstacles is tokenized
getline(cin, input);
tokens = tokenizeByChar(input, ' ');
numOfObstacles = atoi(input.c_str());
// We loop through, gathering the obstacles and adding them to a vector
for (int i = 0; i < numOfObstacles; i++) {
getline(cin, input);
tokens = tokenizeByChar(input, ',');
Point temp;
temp.x = atoi(tokens[0].c_str());
temp.y = atoi(tokens[1].c_str());
obstacles.push_back(temp);
}
// The obstacles are put in the gridmap
gridmap = new GridMap(dimensions.x, dimensions.y, obstacles);
// The gridmap is used by the dispatcher
dispatcher = new TaxiDispatch(gridmap, clock);
}
void GameControl::addRide(string input) {
int id, num_passengers, tariff, startTime;
Point start, end;
vector<string> tokens = tokenizeByChar(input, ',');
// The input is tokenized and parsed into Points
start = Point(atoi(tokens[1].c_str()), atoi(tokens[2].c_str()));
end = Point(atoi(tokens[3].c_str()), atoi(tokens[4].c_str()));
id = atoi(tokens[0].c_str());
num_passengers = atoi(tokens[5].c_str());
tariff = atoi(tokens[6].c_str());
startTime = atoi(tokens[7].c_str());
// The trip is generated based on input
TripInfo* t = new TripInfo(id, start, end, num_passengers, tariff, startTime);
dispatcher->addTrip(t);
}
void GameControl::printTaxiLocation(string input) {
// Parses the input and prints the taxi location from the dispatcher
vector<string> tokens = tokenizeByChar(input, ',');
int id = atoi(tokens[0].c_str());
Point * location = dispatcher->getDriverLocation(id);
cout << location->toString() << endl;
}
void GameControl::addDriver(string input, char* argv[]) {
char buffer[2048];
Serializer serializer;
//Create the socket to receive the driver
Tcp * tcp = new Tcp(1, atoi(argv[1]), "127.0.0.1");
tcp->initialize();
dispatcher->assignSocket(tcp);
// Loop through all clients, receiving their info
for(int i = 0; i < atoi(input.c_str()); i++){
tcp->acceptSock();
//gets the driver from the client
cout << "Receive status: " << tcp->reciveData(buffer, sizeof(buffer), tcp->upto - 1) << endl;
string receive(buffer);
cout << "recived data from client number " << i << ", " << receive << endl;
//deserialize the driver from client
Driver * d = serializer.deserializeDriver(receive);
dispatcher->addDriver(d, tcp->upto - 1);
cout << "sending taxi to client: " << i << endl;
dispatcher->sendTaxi(d->getID());
}
}
int GameControl::enumFromString(string raw, char type) {
int returnVal;
// Given the input type we could be parsing a MarriageStatus, Color, or TypeofCar
switch (type) {
case 'M':
if (!raw.compare("S")) {
returnVal = SINGLE;
}
else if (!raw.compare("M")) {
returnVal = MARRIED;
}
else if (!raw.compare("D")) {
returnVal = DIVORCED;
}
else if (!raw.compare("W")) {
returnVal = WIDOWED;
}
break;
case 'C':
if (!raw.compare("R")) {
returnVal = RED;
}
else if (!raw.compare("B")) {
returnVal = BLUE;
}
else if (!raw.compare("G")) {
returnVal = GREEN;
}
else if (!raw.compare("P")) {
returnVal = PINK;
}
else if (!raw.compare("W")) {
returnVal = WHITE;
}
break;
case 'T':
if (!raw.compare("H")) {
returnVal = HONDA;
}
else if (!raw.compare("S")) {
returnVal = SUBARU;
}
else if (!raw.compare("T")) {
returnVal = TESLA;
}
else if (!raw.compare("F")) {
returnVal = FIAT;
}
break;
default:
returnVal = 0;
break;
}
return returnVal;
}
vector<string> GameControl::tokenizeByChar(string input, char delim) {
vector<string> tokens;
istringstream iss(input);
string oneToken;
// We use a stringstream to tokenize our output
while(getline(iss, oneToken, delim)) {
tokens.push_back(oneToken);
}
return tokens;
}
GameControl::~GameControl() {
if (gridmap != 0) {
delete gridmap;
}
if (dispatcher != 0) {
delete dispatcher;
}
}
void GameControl::moveOneStep() {
dispatcher->moveOneStep();
}
void GameControl::closingOperations() {
dispatcher->closingOperations();
}