-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
69 lines (59 loc) · 1.79 KB
/
client.js
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
import {
Entity
} from "./entity.js"
class Client extends Entity {
constructor(id, radius, x, y, messageManager, dataRequestFactory, avgFramesBetweenMessages, dataRequestRouter) {
super(radius);
this.id = id;
this.radius = radius;
this.x = x;
this.y = y;
this.messageManager = messageManager;
this.dataRequestFactory = dataRequestFactory;
this.avgFramesBetweenMessages = avgFramesBetweenMessages;
this.dataRequestRouter = dataRequestRouter;
}
init() {
this.group = d3.select("svg").append("g");
this.circle = this.group.append("circle");
this.circle.attr("cx", this.x);
this.circle.attr("cy", this.y);
this.circle.attr("r", this.radius);
this.circle.attr("class", "client");
this.circle.attr("id", this.id);
this.circle.attr("display", "none");
this.appear();
}
cleanup() {
this.disappear();
window.setTimeout(function() {
this.group.remove();
}.bind(this), 1520);
}
handleFrame() {
if (Math.random() < (1 / this.avgFramesBetweenMessages)) {
this.sendDataRequest();
}
}
appear() {
$("#" + this.id).fadeIn(1500);
}
disappear() {
$("#" + this.id).fadeOut(1500);
}
sendDataRequest() {
var leader = this.dataRequestRouter.getLeader();
if (leader == null) {
return;
}
var dataTypes = ["SPADE", "CLUB", "HEART", "DIAMOND"];
var dataType = dataTypes[Math.round(Math.random() * (dataTypes.length - 1))];
var msg = this.dataRequestFactory.get(dataType, this.id, leader);
msg.init();
this.messageManager.schedule(msg);
}
handleMessage(msg) {}
}
export {
Client
}