forked from haydn/c-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
121 lines (86 loc) · 2.74 KB
/
bot.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
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
import uuid from 'uuid';
import chalk from 'chalk';
import WebSocket from 'faye-websocket';
import ConnectBehavior from './behaviors/connect';
import AddPlayerBehavior from './behaviors/add-player';
import JoinBehavior from './behaviors/join';
import ReadyBehavior from './behaviors/ready';
import WaitBehavior from './behaviors/wait';
import CountdownBehavior from './behaviors/countdown';
import PlayBehavior from './behaviors/play';
export default class Bot {
constructor(url, name) {
this.url = url;
this.name = name;
this.privateId = uuid.v4();
this.publicId = null;
this.targetId = null;
this._webSocket = null;
this._behaviors = {
"connect": new ConnectBehavior(this),
"add-player": new AddPlayerBehavior(this),
"join": new JoinBehavior(this),
"ready": new ReadyBehavior(this),
"wait": new WaitBehavior(this),
"countdown": new CountdownBehavior(this),
"play": new PlayBehavior(this)
};
this._behavior = null;
this.init();
}
init() {
this._webSocket = new WebSocket.Client(this.url);
this._webSocket.on('open', this.onOpen.bind(this));
this._webSocket.on('message', this.onMessage.bind(this));
this._webSocket.on('error', this.onError.bind(this));
this._webSocket.on('close', this.onClose.bind(this));
this.setBehavior(null);
}
destroy() {
this.setBehavior(null);
this._webSocket = null;
}
setBehavior(name) {
if (name === null || this._behaviors[name]) {
console.log(chalk.cyan(this.name, 'behavior'), name);
} else {
console.log(chalk.red(this.name, 'behavior'), name);
}
if (this._behavior && this._behaviors[this._behavior]) {
this._behaviors[this._behavior].deactivate();
}
this._behavior = name;
if (this._behavior && this._behaviors[this._behavior]) {
this._behaviors[this._behavior].activate();
}
}
onOpen(event) {
console.log(chalk.cyan(this.name, 'open'));
this.setBehavior("connect");
}
onMessage(event) {
var behavior = this._behaviors[this._behavior];
if (behavior) {
JSON.parse(event.data).forEach((data) => {
var handler = behavior.getHander(data);
if (handler) {
console.log(chalk.green(this.name, "<-"), data);
handler.call(behavior, data);
} else {
console.log(chalk.red(this.name, "<-"), data);
}
});
}
}
onError(event) {
console.log(chalk.red(this.name, 'error'), event);
}
onClose(event) {
console.log(chalk.cyan(this.name, 'close'), event.code, event.reason);
this.destroy();
}
send(...data) {
console.log(chalk.yellow(this.name, '->'), data);
this._webSocket.send(JSON.stringify([data]));
}
}