-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
145 lines (123 loc) · 4.03 KB
/
server.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var socketClients = {};
var fiveDesk = [];
io.on('connection', function (socket) {
var _socketId = socket.id;
function setError(result) {
socket.emit('errorResult', result);
}
function delDesk(index) {
fiveDesk.splice(index, 1);
}
function getClientByName(name) {
for (var key in socketClients) {
var item = socketClients[key];
if (item.name === name) {
return item;
}
}
}
function existsDesk(socketId) {
for (var item of fiveDesk) {
if (item.White === socketId || item.Black === socketId) {
return true;
}
}
return false;
}
socket.on('disconnect', () => {
for (let key in socketClients) {
if (key === _socketId) {
delete socketClients[key];
break;
}
}
for (let i = fiveDesk.length - 1; i >= 0; i--) {
var desk = fiveDesk[i];
if (desk.White === _socketId || desk.Black === _socketId) {
socketClients[desk.White === _socketId ? desk.Black : desk.White].socket.emit('break');
delDesk(i);
break;
}
}
});
socket.on('setName', function (name) {
if (getClientByName(name)) {
setError({ Message: '对不起,已存在该名称!' });
return;
}
socketClients[socket.id] = {
id: socket.id,
socket: socket,
name: name
}
});
socket.on('updateClients', function () {
for (let key in socketClients) {
var client = socketClients[key];
var result = [];
if (!existsDesk(key)) {
for (let key2 in socketClients) {
if (key2 !== key && !existsDesk(key2)) {
result.push({
id: key2,
name: socketClients[key2].name
});
}
}
client.socket.emit('updateClients', result);
}
}
});
socket.on('applyConnect', function (socketId) {
var thisClient = socketClients[socket.id];
if (!socketClients.hasOwnProperty(socketId)) {
thisClient.socket.emit('errorResult', { IsSuccess: false, Message: '对方不在线' });
return;
}
if (existsDesk(socketId)) {
thisClient.socket.emit('errorResult', { IsSuccess: false, Message: '对方已加入其它棋局' });
return;
}
var client = socketClients[socketId];
if (!thisClient || !client)
return;
fiveDesk.push({
White: thisClient.id,
Black: client.id,
Result: false
});
client.socket.emit('applyConnect', {
IsSuccess: true,
Message: thisClient.name,
DeskId: fiveDesk.length - 1
});
});
socket.on('agreeConnect', function (deskId) {
if (fiveDesk.length > deskId) {
var desk = fiveDesk[deskId];
desk.Result = true;
socketClients[desk.White].socket.emit('deskBegin', { role: 1, deskId: deskId });
socketClients[desk.Black].socket.emit('deskBegin', { role: -1, deskId: deskId });
}
});
socket.on('clickPiece', function (data) {
var deskId = data.deskId;
if (fiveDesk.length > deskId) {
var desk = fiveDesk[deskId];
var socket;
if (data.role === 1) {
socket = socketClients[desk.Black].socket;
} else {
socket = socketClients[desk.White].socket;
}
socket.emit('serverClickPiece', { x: data.x, y: data.y })
}
});
});
var server = http.listen(8080, function () {
console.log('Server is running!');
});