-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
178 lines (153 loc) · 5.42 KB
/
app.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
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
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var serverConfig = require('./public/config').serverConfig;
var Game = require('./public/tdGame/Game');
var Rooms = require('./public/tdGame/Room');
var Room = Rooms.Room;
var utils = require('./public/utils');
io.on('connection', function (socket) {
var clientIp = socket.request.connection.remoteAddress;
console.log('New connection from ' + clientIp);
socket.on('newRoom', function(data) {
var roomName = data['name'];
var userInfo = data['userInfo'];
var msg = {code:0,msg:'failed'};
if(!Room.isRoomExisted(roomName)){
utils.clearSocketsByRoomName(io,roomName);
socket.roomName = roomName;
socket.role = 'master';
socket.join(roomName);
var game = new Game(io,roomName);
game.addPlayer(userInfo);
msg = Room.createRoom(roomName,game);
if(msg.code==1){
msg = {code:1,userInfos:game.userInfos};
}
}
socket.emit('roomInfo', msg);
});
socket.on('getRooms', function() {
// TODO 数据格式需要改动
var msg = {'ret': 1, 'data': Room.getRooms()};
socket.emit('getRooms', msg);
});
socket.on('joinRoom', function (data) {
var roomName = data.roomId;
var userInfo = data.userInfo;
var msg = {code:0,msg:'failed'};
if(!Room.isRoomExisted(roomName) || Room.isRoomFull(roomName)){
socket.emit('joinRoom', msg);
}else{
socket.roomName = roomName;
socket.role = 'challenger';
socket.join(roomName);
var game = Room.getRoom(roomName);
game.addPlayer(userInfo);
msg = {code:1,userInfos:game.userInfos};
game.broadcastMsg('roomInfo', msg);
}
});
socket.on('deleteRoom', function () {
var roomName = socket.roomName;
var msg = {code:0,msg:'failed'};
if(!Room.isRoomExisted(roomName)){
socket.emit('deleteRoom', msg);
}else{
var game = Room.getRoom(roomName);
game.broadcastMsg('deleteRoom',{code:1,msg:'success'});
Room.deleteRoom(roomName);
utils.clearSocketsByRoomName(io,roomName);
}
});
socket.on('startGame', function() {
var roomName = socket.roomName;
var game = Room.getRoom(roomName);
if(game){
game.startGame();
}
});
socket.on('playAgain', function(data) {
var roomName = data.roomId;
var userInfo = data.userInfo;
var msg = {code:0,msg:'failed'};
if(!Room.isRoomExisted(roomName)){
socket.role = 'master';
socket.roomName = roomName;
socket.join(roomName);
userInfo.isMaster = true;
var game = new Game(io,roomName);
game.addPlayer(userInfo);
msg = Room.createRoom(roomName,game);
if(msg.code === 1){
msg.userInfos = game.userInfos;
msg.msg = 'success';
}
}else if(!Room.isRoomFull(roomName)){
socket.role = 'challenger';
socket.roomName = roomName;
socket.join(roomName);
userInfo['isMaster'] = false;
var game = Room.getRoom(roomName);
game.addPlayer(userInfo);
msg ={code:1,msg:'success',userInfos:game.userInfos};
game.broadcastMsg('playAgain',msg);
return;
}
socket.emit('playAgain', msg);
});
socket.on('KeyUp', function (keyCode) {
var game = Room.getRoom(socket.roomName);
if(game){
if (socket.role === 'master') {
game.stopARoleByKeyCode(keyCode,game.roleArr[0]);
} else {
game.stopARoleByKeyCode(keyCode,game.roleArr[1]);
}
}
});
socket.on('KeyDown', function (keyCode) {
var game = Room.getRoom(socket.roomName);
if (game) {
if (socket.role === 'master') {
game.moveARoleByKeyCode(keyCode,game.roleArr[0]);
} else {
game.moveARoleByKeyCode(keyCode,game.roleArr[1]);
}
}
});
socket.on('MoveByAngle', function (angle) {
var game = Room.getRoom(socket.roomName);
if (game) {
if (socket.role === 'master') {
game.moveARoleByAngle(angle,game.roleArr[0]);
} else {
game.moveARoleByAngle(angle,game.roleArr[1]);
}
}
});
socket.on('TouchEnd', function () {
var game = Room.getRoom(socket.roomName);
if(game){
if (socket.role === 'master') {
game.stopAMobileRole(game.roleArr[0]);
} else {
game.stopAMobileRole(game.roleArr[1]);
}
}
});
socket.on('disconnect', function(){
var game = Room.getRoom(socket.roomName);
if(game){
game.broadcastMsg('deleteRoom',{code:1,msg:'success'});
game.stopGame(socket.role);
}
})
socket.on('error',function(){
console.log('server websocket error');
})
});
server.listen(4000, function(){
console.log('App listening at http://%s:%s', serverConfig.host, serverConfig.port);
});