-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathapp.js
More file actions
253 lines (207 loc) · 6.39 KB
/
Copy pathapp.js
File metadata and controls
253 lines (207 loc) · 6.39 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var WebSocketServer = require('ws').Server;
var http = require('http');
var server = http.createServer();
var crypto = require('crypto');
var MersenneTwister = require('./MersenneTwister');
var Cataso = require('./cataso/Cataso');
var BattleRaiso = require('./battleraiso/BattleRaiso');
var Goipai = require('./goipai/Goipai');
var roomList = [
new Cataso()
, new Cataso()
, new Cataso()
, new BattleRaiso()
, new Goipai()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new Cataso()
, new BattleRaiso()
, new BattleRaiso()
, new BattleRaiso()
, new Goipai()
, new Goipai()
];
var User = function (ws, uid, trip) {
this.ws = ws;
this.uid = uid;
this.trip = trip;
}
var splitSyntaxType1 = function (source) {
return source.substring(1);
}
var sendUserList = function (index, ws) {
var buff = '';
var i;
var len1 = roomList[index].userList.length;
for (i = 0; i < len1; i++) {
if (i > 0) { buff += ' '; }
if (roomList[index].userList[i].trip !== '') {
buff += roomList[index].userList[i].uid + '%' + roomList[index].userList[i].trip;
} else {
buff += roomList[index].userList[i].uid;
}
}
try {
ws.send('A' + buff);
} catch (e) { }
}
var createTrip = function (source) {
while (source.length < 8) { source += 'H'; }
var cipher = crypto.createCipher('des-ecb', source.substr(0, 3));
var crypted = cipher.update(source.substr(0, 8), 'utf-8', 'hex');
crypted += cipher.final('hex');
return crypted.substr(0, 10);
}
var login = function (index, ws, message) {
var isSuccessful = false;
var token = splitSyntaxType1( message).split('#', 2);
var uid = token[0];
var src;
if (token.length > 1) { src = token[1]; }
if (
uid.length > 0
&& uid.match(/^[0-9A-Za-z]{1,12}$/)
) {
isSuccessful = true;
var i;
var len1 = roomList[index].userList.length;
for (i = 0; i < len1; i++) {
if (uid === roomList[index].userList[i].uid) {
isSuccessful = false;
break;
}
}
}
if (isSuccessful) {
try {
var trip = '';
if (src) { trip = createTrip(src); }
if (trip !== '') {
ws.send('B' + uid + '%' + trip);
} else {
ws.send('B' + uid);
}
sendUserList(index, ws);
var user = new User(ws, uid, trip);
roomList[index].userList.push(user);
if (user.trip !== '') {
roomList[index]._broadcast('D' + user.uid + '%' + user.trip);
} else {
roomList[index]._broadcast('D' + user.uid);
}
if (roomList[index].owner !== null) {
ws.send('F' + roomList[index].owner.uid);
}
} catch (e) {
}
} else {
try {
ws.send('C');
} catch (e) { }
}
}
var wss = new WebSocketServer({server:server});
wss.on('connection', function (ws) {
ws.on('close', function () {
var user = null;
var i;
var len1 = roomList.length;
for (i = 0; i < len1; i++) {
var j;
var len2 = roomList[i].userList.length;
for (j = 0; j < len2; j++) {
if (ws === roomList[i].userList[j].ws) {
user = roomList[i].userList[j];
roomList[i].removeUser(user);
break;
}
}
if (user !== null) { break; }
}
});
ws.on('message', function (message) {
var index = message.charCodeAt(0);
message = message.substring(1);
var i;
var len1;
if (index === 100) {
var buff = String.fromCharCode(100);
len1 = roomList.length;
for (i = 0; i < len1; i++) {
buff += roomList[i].userList.length + ' ';
buff += roomList[i].symbol + ' ';
if (roomList[i].isPlaying) {
buff += 'p';
} else {
buff += 'r';
}
if (i < len1 - 1) { buff += ' '; }
}
try {
ws.send(buff);
} catch (e) {
}
return;
} else if (index >= roomList.length || message.length === 0) { return; }
var user = null;
len1 = roomList[index].userList.length;
for (i = 0; i < len1; i++) {
if (ws === roomList[index].userList[i].ws) {
user = roomList[index].userList[i];
break;
}
}
var param;
if (user === null) {
switch (message[0]) {
case 'a':
sendUserList(index, ws);
break;
case 'b':
login(index, ws, message);
break;
}
} else {
switch (message[0]) {
case 'c':
param = splitSyntaxType1(message);
roomList[index].onChat(user, param);
if (param.length > 1 && param[0] === '/') {
roomList[index].onCommand(user, param.split(' '));
}
break;
case 'd':
param = splitSyntaxType1(message);
roomList[index].onMessage(user.uid, param);
break;
case 'e':
roomList[index].chat('?', 'deeppink', user.uid + 'がベルを鳴らしました。');
roomList[index]._broadcast('J');
break;
case 'f':
roomList[index].chat(
'?'
, 'deeppink'
, user.uid + 'のダイス=>[' + MersenneTwister.Share.nextInt(1, 100) + ']'
);
break;
}
}
});
});
server.listen(process.env.PORT || 5000);
process.on('uncaughtException', function (e) {
console.log('uncaughtException => ' + e);
});