-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathRoom.js
More file actions
122 lines (99 loc) · 3.24 KB
/
Copy pathRoom.js
File metadata and controls
122 lines (99 loc) · 3.24 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
var Room = function () { }
Room.prototype.symbol = null;
Room.prototype.userList = null;
Room.prototype.owner = null;
Room.prototype.initialize = function (symbol) {
this.symbol = symbol;
this.userList = [];
this.owner = null;
this.isPlaying = false;
}
Room.prototype.removeUser = function (user) {
var i;
var len1 = this.userList.length;
for (i = 0; i < len1; i++) {
if (this.userList[i].ws === user.ws) {
this.userList.splice(i, 1);
if (user.trip !== '') {
this._broadcast('E' + user.uid + '%' + user.trip);
} else {
this._broadcast('E' + user.uid);
}
break;
}
}
if (this.owner === user) {
this._broadcast('G');
this.owner = null;
}
}
Room.prototype._unicast = function (user, message) {
try {
user.ws.send(message);
} catch (e) {
this.removeUser(user);
}
}
Room.prototype.unicast = function (uid, message) {
var user = null;
var i;
var len1 = this.userList.length;
for (i = 0; i < len1; i++) {
if (this.userList[i].uid === uid) {
user = this.userList[i];
break;
}
}
if (user !== null) { this._unicast(user, 'I' + message); }
}
Room.prototype._broadcast = function (message) {
var i;
var len1 = this.userList.length;
for (i = 0; i < len1; i++) { this._unicast(this.userList[i], message); }
}
Room.prototype.broadcast = function (message) {
this._broadcast('I' + message);
}
Room.prototype.chat = function (uid, color, message) {
this._broadcast('H' + uid + ' ' + color + ' ' + message);
}
Room.prototype.reset = function () { }
Room.prototype.onLoad = function () { }
Room.prototype.onMessage = function (uid, message) { }
Room.prototype.onCommand = function (user, message) {
this.basicCommand(user, message);
}
Room.prototype.onChat = function (user, message) {
this.chat(user.uid, 'white', (message.split('<').join('<')).split('>').join('>'));
}
Room.prototype.basicCommand = function (user, message) {
switch (message[0]) {
case '/grant':
if (this.owner === null) {
this.owner = user;
this._broadcast('F' + user.uid);
this.chat('?', 'deeppink', user.uid + 'が管理者を取得しました。');
} else {
this.chat('?', 'deeppink', '既に管理者が居ます。');
}
break;
case '/revoke':
if (this.owner !== null && this.owner === user) {
this.owner = null;
this._broadcast('G');
this.chat('?', 'deeppink', user.uid + 'が管理者を辞退しました。');
} else {
this.chat('?', 'deeppink', '元から管理者ではありません。');
}
break;
case '/reset':
if (this.owner !== null && this.owner.uid === user.uid) {
this.reset();
this.chat('?', 'deeppink', 'コンテンツをリセットしました。');
} else {
this.chat('?', 'deeppink', '管理者でないためリセットできません。');
}
break;
}
}
module.exports = Room;