-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
188 lines (158 loc) · 5.16 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
179
180
181
182
183
184
185
186
187
188
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(80);
var indexPage="Server is starting"
fs.readFile(__dirname + '/index.html', function (err, data) {
if (err) {
console.log("Error starting server")
}
indexPage=data
});
function handler (req, res) {
res.writeHead(200);
res.end(indexPage);
}
function getRoom(socket) {
var a = io.sockets.manager.roomClients[socket.id]
for(var i in a){
if (a.hasOwnProperty(i) && i!="") {
return i.substr(1, a[i].length)
}
}
}
gameRooms = {}
users = {}
io.sockets.on('connection', function (socket) {
rooms={}
for(i in io.sockets.manager.rooms) {
rooms[i.substr(1, i.length)]=io.sockets.manager.rooms[i].length
}
socket.emit('rooms', rooms);
socket.on('subscribe', function(data) {
socket.join(data.room);
if(gameRooms[data.room]==undefined){
gameRooms[data.room]={}
gameRooms[data.room].field=[]
gameRooms[data.room].turn=0
gameRooms[data.room].isStarted=false
}
if(gameRooms[data.room].isStarted){
socket.emit('setField', {
field: gameRooms[data.room].field,
isStarted: true,
player1: users[gameRooms[data.room].player1],
player2: users[gameRooms[data.room].player2]
})
socket.emit('chat', { msg: "Играют "+users[gameRooms[data.room].player1]+" и "+users[gameRooms[data.room].player2]})
}
if(gameRooms[data.room].player1==undefined) {
gameRooms[data.room].player1 = socket.id
io.sockets.in(getRoom(socket)).emit('chat', { msg: users[socket.id]+" - первый игрок"})
}else if(gameRooms[data.room].player2==undefined) {
gameRooms[data.room].player2 = socket.id
io.sockets.in(getRoom(socket)).emit('setField', {
field: [],
isStarted: true,
player1: users[gameRooms[data.room].player1],
player2: users[socket.id]
})
gameRooms[data.room].field=[]
gameRooms[data.room].turn=0
gameRooms[data.room].isStarted=true
io.sockets.in(getRoom(socket)).emit('chat', { msg: users[socket.id]+" - второй игрок"})
} else {
io.sockets.in(getRoom(socket)).emit('chat', { msg: users[socket.id]+" вошёл в комнату"})
}
})
socket.on('unsubscribe', function(data) {
if(gameRooms[data.room] && (gameRooms[data.room].player1 == socket.id || gameRooms[data.room].player2 == socket.id)) {
io.sockets.in(getRoom(socket)).emit('chat', { msg: users[socket.id]+" left room"})
io.sockets.in(getRoom(socket)).emit('setField', {
field: [],
isStarted: false,
player1: "",
player2: ""
})
gameRooms[data.room]={}
gameRooms[data.room].field=[]
gameRooms[data.room].turn=0
} else {
io.sockets.in(getRoom(socket)).emit('chat', { msg: users[socket.id]+" left room"})
}
socket.leave(data.room);
})
socket.on('setName', function(data) {
users[socket.id]=data.name
})
socket.on('changeName', function(data) {
var old = users[socket.id]
users[socket.id]=data.name
io.sockets.in(getRoom(socket)).emit('chat', { msg: old+" сменил имя на "+data.name})
})
socket.on('chat', function(data) {
if(data.msg.trim()!="") io.sockets.in(getRoom(socket)).emit('chat', { msg: users[socket.id]+": "+data.msg})
})
socket.on('roomList', function(data) {
rooms={}
for(i in io.sockets.manager.rooms) {
rooms[i.substr(1, i.length)]=io.sockets.manager.rooms[i].length
}
socket.emit('roomList', rooms);
})
socket.on('makeStep', function(data) {
var room=getRoom(socket)
if(gameRooms[room]!=undefined && !gameRooms[room].isStarted) { console.log("not started"); return;}
var pl=0
if(gameRooms[room].player1==socket.id && gameRooms[room].turn % 2 == 0) pl=1
if(gameRooms[room].player2==socket.id && gameRooms[room].turn % 2 == 1) pl=2
if(pl==0) { console.log("wrong player "+socket.id+" "+gameRooms[room].player1+" "+gameRooms[room].player2+" "+turn); return;}
var possible = false
var prev = gameRooms[room].prev
if(gameRooms[room].turn == 0) {
possible = true
console.log("$1")
} else {
var target = ((prev-1) % 9) + 1
var turn = Math.floor((data.id-1) / 9) + 1
console.log("$2"+target+"$"+turn)
if(target==turn) {
possible = true
console.log("$3")
} else {
console.log("$4")
var c=0
for(var i=9*(target-1)+1; i<=9*(target-1)+9; i++)
if(gameRooms[room].field[i]>0) c++
if(c==9) {
possible = true
console.log("$5")
}
}
}
console.log("$6")
if(gameRooms[room].field[data.id]>0) possible = false
if(!possible) return;
gameRooms[room].field[data.id]=pl
gameRooms[room].prev = data.id
io.sockets.in(room).emit('updateField', { a: data.id, b: pl})
gameRooms[room].turn++
})
socket.on('disconnect', function () {
var room=getRoom(socket)
if(gameRooms[room] && (gameRooms[room].player1 == socket.id || gameRooms[room].player2 == socket.id)) {
io.sockets.in(getRoom(socket)).emit('chat', { msg: users[socket.id]+" left room"})
io.sockets.in(getRoom(socket)).emit('setField', {
field: [],
isStarted: false,
player1: "",
player2: ""
})
gameRooms[room]={}
gameRooms[room].field=[]
gameRooms[room].turn=0
} else {
io.sockets.in(getRoom(socket)).emit('chat', { msg: users[socket.id]+" left room"})
}
});
});