-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
326 lines (305 loc) · 9.23 KB
/
server.cpp
File metadata and controls
326 lines (305 loc) · 9.23 KB
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
#include "server.h"
Server::Server(){
}
Server::Server(std::vector<City> cities){
//constructor for a server object
for(unsigned int i=0;i<cities.size();i++){
std::vector<int> tempVector(cities.size(),0);
adjMatrix.push_back(tempVector);
}
}
Server::~Server(){
}
void Server::group(){
}
void Server::init_cities(std::vector<City> cities){
for(unsigned int i=0;i<cities.size();i++){
std::vector<int> tempVector(cities.size(),0);
cityList.push_back(cities[i]);
adjMatrix.push_back(tempVector);
}
}
void Server::printCity(){
for(unsigned int i=0; i<cityList.size();i++){
printf("cityList[%d] has cityno of %d", i, cityList[i].getCityNo());
cityList[i].printAdjMatrix();
printf("\n");
}
}
void Server::printUsers(){
for(unsigned int i=0;i<userList.size();i++){
printf("User %d is in city %d\n", userList[i].getID(),userList[i].getCity());
}
}
void Server::init_city_adjMatrix(){
//creating a adjMatrix
for(unsigned int i=0; i<cityList.size();i++){
cityList[i].setAdjMatrix(adjMatrix);
}
}
void Server::removeUser(User targetUser){
for (unsigned int i = 0; i < userList.size(); i++){
if (targetUser.getID() == userList[i].getID()){
userList.erase(userList.begin() + i);
cityList.at(targetUser.getCity()).removeUser(targetUser);
}
else{
userList[i].removeUser(targetUser);
}
}
}
int Server::getDistance(int pingingUserCity, int targetUserCity){
return adjMatrix[pingingUserCity][targetUserCity];
}
std::vector<int> Server::addUser(User targetUser, int mode){
userList.push_back(targetUser);
cityList[targetUser.getCity()].addUser(targetUser);
std::vector<int> zero;
int loc = userList.size()- 1;
for(unsigned int i=0; i< userList.size();i++){
commandUserPing(userList[i],userList[loc]);
}
for(unsigned int i=0; i< userList.size();i++){
commandUserPing(userList[loc],userList[i]);
}
if(userList.size() > 5){
switch(mode){
case 0:
return geolocation();
case 1:
return matchmake();
case 2:
if (userList.size() > 10){
return matchmakeRandom();
}
else{
return zero;
}
}
}
else{
return zero;
}
}
std::vector<int> Server::matchmakeRandom(){
std::srand((unsigned)time(0));
std::vector<int> final_group;
for(int i=0; i<5;i++){
int randomUser = rand()%userList.size();
final_group.push_back(userList[randomUser].getID());
removeUser(userList[randomUser]);
}
return final_group;
}
std::vector<int> Server::geolocation(){
std::vector< std::pair<int, std::vector<int> > >count;
std::vector<int> final_group;
std::vector<int> target;
int found = 0;
for(unsigned int i=0; i<userList.size(); i++){
for(unsigned int j=0; j<userList[i].getWantedHosts().size(); j++){
found = 0;
for(unsigned int k=0; k<count.size(); k++){
if(count.at(k).first == userList[i].getWantedHosts().at(j)){
found = 1;
count.at(k).second.push_back(userList[i].getID());
if(count.at(k).second.size() >= 4){
target.push_back(k);
break;
}
break;
}
}
if(found == 0){
std::vector<int> tempVector;
tempVector.push_back(userList[i].getID());
count.push_back(std::pair<int, std::vector<int>>(userList[i].getWantedHosts().at(j),tempVector));
}
}
}
if(target.size()!=0){
std::vector< std::vector<int> > temp(target.size());
int desiredHost = -1;
int targetCity;
for (unsigned int i = 0 ; i < target.size();i++){
for(unsigned int m =0; m<userList.size();m++){
if(userList[m].getID() == count.at(target[i]).first){
targetCity = userList[m].getCity();
}
}
for (unsigned int j = 0; j < cityList[targetCity].getUsers().size();j++){
for (unsigned int k = 0; k < count.at(target[i]).second.size();k++){
if (count.at(target[i]).second.at(k) == cityList[targetCity].getUsers().at(j).getID()){
temp[i].push_back(k);
}
if (temp[i].size() > 3){
desiredHost = i;
break;
}
}
if (temp[i].size() > 3){
break;
}
}
if (temp[i].size() > 3){
break;
}
}
if (desiredHost == -1){
int foundHost = 0;
int percentage = 0;
for (unsigned int i=0 ; i < target.size();i++){
foundHost = 0;
for(unsigned int m=0; m<userList.size();m++){
if(userList[m].getID() == count.at(target[i]).first){
targetCity = userList[m].getCity();
}
}
for(unsigned int n=0; n<cityList.size();n++){
if(targetCity == cityList[n].getCityNo()){
percentage = cityList[n].getPercent() / 10;
}
}
for(unsigned int j=0; j<waitingHosts.size();j++){
if(waitingHosts[j].first == count.at(target[i]).first){
foundHost = 1;
waitingHosts[j].second = waitingHosts[j].second + 1;
if(waitingHosts[j].second > percentage){
waitingHosts.erase(waitingHosts.begin()+j);
return geolocationHelper(count.at(target[i]),temp[i]);
}
}
}
if(foundHost == 0){
waitingHosts.push_back(std::pair<int,int>(count.at(target[i]).first,0));
}
}
}
else{
final_group.push_back(count.at(target.at(desiredHost)).first);
for(unsigned int i=0; i<temp[desiredHost].size();i++){
final_group.push_back(count.at(target.at(desiredHost)).second.at(temp[desiredHost].at(i)));
for(unsigned int j=0; j<userList.size();j++){
if(userList[j].getID() == count.at(target.at(desiredHost)).second.at(temp[desiredHost].at(i))){
removeUser(userList[j]);
break;
}
}
}
for(unsigned int j=0; j<userList.size();j++){
if(userList[j].getID() == count.at(target.at(desiredHost)).first){
removeUser(userList[j]);
break;
}
}
}
}
return (final_group);
}
std::vector<int> Server::geolocationHelper(std::pair<int,std::vector<int>> count, std::vector<int> temp){
std::vector<int> final_group;
final_group.push_back(count.first);
int counter = temp.size();
for(unsigned int j=0; j<temp.size(); j++){
final_group.push_back(count.second.at(temp[j]));
for(unsigned int k=0;k<userList.size();k++){
if(userList[k].getID() == count.second.at(temp[j])){
removeUser(userList[k]);
break;
}
}
}
int iterator = 0;
for(unsigned int j=0; j<temp.size();j++){
count.second.erase(count.second.begin()+temp[j]-iterator);
iterator++;
}
std::srand((unsigned)time(0));
for(unsigned int i=counter; i<5;i++){
int random = std::rand()%count.second.size();
final_group.push_back(count.second.at(random));
for(unsigned int j=0; j<userList.size();j++){
if(userList[j].getID() == count.second.at(random)){
removeUser(userList[j]);
break;
}
}
}
for(unsigned int i=0; i<userList.size();i++){
if(userList[i].getID() == count.first){
removeUser(userList[i]);
break;
}
}
return final_group;
}
std::vector<int> Server::matchmake(){
std::vector< std::pair<int, std::vector<int> > >count;
std::vector<int> final_group;
int target =0;
int found = 0;
for(unsigned int i=0; i<userList.size(); i++){
for(unsigned int j=0; j<userList[i].getWantedHosts().size(); j++){
found = 0;
for(unsigned int k=0; k<count.size(); k++){
if(count.at(k).first == userList[i].getWantedHosts().at(j)){
found = 1;
count.at(k).second.push_back(userList[i].getID());
if(count.at(k).second.size() >= 4){
target = k;
break;
}
break;
}
}
if(found == 0){
std::vector<int> tempVector;
tempVector.push_back(userList[i].getID());
count.push_back(std::pair<int, std::vector<int>>(userList[i].getWantedHosts().at(j),tempVector));
}
if(target!= 0){
break;
}
}
if(target!=0){
break;
}
}
if(target!=0){
final_group.push_back(count.at(target).first);
for(unsigned int i=0; i<count.at(target).second.size();i++){
final_group.push_back(count.at(target).second.at(i));
for(unsigned int j=0;j<userList.size();j++){
if(userList[j].getID() == count.at(target).second.at(i)){
removeUser(userList[j]);
break;
}
}
}
for(unsigned int j=0;j<userList.size();j++){
if(userList[j].getID() == count.at(target).first){
removeUser(userList[j]);
}
}
}
return (final_group);
}
void Server::updateMatrix(int distance, City cityOne, City cityTwo){
int cityOneId = cityOne.getCityNo();
int cityTwoId = cityTwo.getCityNo();
adjMatrix[cityOneId][cityTwoId] = distance;
adjMatrix[cityTwoId][cityOneId] = distance;
}
void Server::updateUserWanted(int userID, std::vector<int> usersWanted){
for(unsigned int i=0; i<userList.size();i++){
if(userList[i].getID() == userID){
userList[i].setWantedHosts(usersWanted);
}
}
}
void Server::commandUserPing(User pingingUser){
pingingUser.pingAll(userList);
}
void Server::commandUserPing(User pingingUser, User pingedUser){
pingingUser.ping(pingedUser);
}