-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.js
83 lines (56 loc) · 1.65 KB
/
Player.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const Sandbox = require("./Sandbox");
// time on 1 task execution
const TASK_TIME = 10 * 60 * 1000;
class Player {
constructor(id, name) {
this.id = id;
this.pos = 1;
this.total = 0;
this.score = 0;
this.outstanding = 0;
this.name = name;
this.socket = null;
this.color = "#FFFFFF";
this.textureDataUrl = null;
this.sandbox = null;
this.completeTask = true;
this.canLeave = false;
this._leaveTimer = null;
}
createSandbox(sessionId) {
if (this.socket != null) {
this.sandbox = new Sandbox(this.id, sessionId, this.socket);
this.sandbox.onTestResults = (status, results) => {
console.log(`onTestResult for user ${this.name}`);
if (status == "passed") {
this.prepareToNextTask();
}
}
}
}
updateSocket(newSocket) {
this.socket = newSocket;
if (this.sandbox) {
this.sandbox.updateClientSocket(this.socket);
}
}
startTaskTimer() {
this._leaveTimer = setTimeout(() => {
this.canLeave = true;
console.log(`User ${this.name} now can leave`);
this.socket.emit("leave");
}, TASK_TIME);
}
prepareToNextTask() {
this.sandbox.stopActivity();
this.completeTask = true;
this.canLeave = false;
clearTimeout(this._leaveTimer);
this._leaveTimer = null;
}
leaveTask() {
this.outstanding++;
this.prepareToNextTask();
}
}
module.exports = Player;