-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
57 lines (49 loc) · 1.29 KB
/
user.cpp
File metadata and controls
57 lines (49 loc) · 1.29 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
#include "user.h"
User::User(int userID, int bandwidth, Server* server, int city){
cityID = city;
this->server = server;
this->userID = userID;
this->bandwidth = bandwidth;
}
User::~User(){
server = NULL;
cityID = 0;
while(!wantedHosts.empty()){
wantedHosts.pop_back();
}
}
// Both pingALL and ping should update addwantedHosts if needed to.
void User::pingAll(std::vector<User> users){
for(unsigned int i=0; i<users.size(); i++){
ping(users[i]);
}
}
double User::ping(User target){
if(target.getID() == userID){
return 0;
}
int distance = server->getDistance(getCity(),target.getCity());
if(distance*1.0/bandwidth < DESIRED_PING){
addWantedHosts(target);
return (distance*1.0/bandwidth);
}
}
void User::removeUser(User targetUser){
for (unsigned int i = 0; i < wantedHosts.size(); i++){
if (wantedHosts[i] == targetUser.getID()){
wantedHosts.erase(wantedHosts.begin() + i);
break;
}
}
}
void User::addWantedHosts(User wantedHost){
wantedHosts.push_back(wantedHost.getID());
send();
}
void User::setWantedHosts(std::vector<int> userWanted){
wantedHosts = userWanted;
}
// Use whenever wantedHosts is changed, so when hosts are added and when hosts are removed
void User::send(){
server->updateUserWanted(userID, wantedHosts);
}