添加一些变量,新建一些处理事件的函数
This commit is contained in:
parent
c95e04c34b
commit
cfae266bc6
83
server.js
83
server.js
@ -7,53 +7,56 @@ const server = app.listen(port, host, () => console.log(`陆战棋游戏服务
|
||||
const io = require('socket.io')(server)
|
||||
|
||||
const clients = []
|
||||
let player1 = null
|
||||
let player2 = null
|
||||
let startTime = null
|
||||
let isStarted = false
|
||||
let player1Ready = false
|
||||
let player2Ready = false
|
||||
let currentPlayer = null
|
||||
|
||||
io.on('connection', (client) => {
|
||||
// 来人了
|
||||
handleConnection(client)
|
||||
client.on('disconnect', () => handleDisconnection(client))
|
||||
client.on('join', () => handleJoin(client))
|
||||
})
|
||||
|
||||
// 处理客户端连接
|
||||
function handleConnection(client) {
|
||||
clients.push(client)
|
||||
client.emit('joined', { player: 'player1', ip: player1 != null ? player1.handshake.address : '' })
|
||||
client.emit('joined', { player: 'player2', ip: player2 != null ? player2.handshake.address : '' })
|
||||
}
|
||||
|
||||
const i = clients.indexOf(client)
|
||||
if (i == 0) client.emit('role', '玩家1')
|
||||
else if (i == 1) client.emit('role', '玩家2')
|
||||
else client.emit('role', '旁观者')
|
||||
// 处理客户端断线
|
||||
function handleDisconnection(client) {
|
||||
const index = clients.indexOf(client)
|
||||
clients.splice(index, 1)
|
||||
}
|
||||
|
||||
if (clients.length == 2) {
|
||||
currentPlayer = clients[0]
|
||||
// 处理参战请求
|
||||
function handleJoin(client) {
|
||||
if (player1 != null && player2 != null) {
|
||||
client.emit('alert', '玩家已满,无法加入游戏。')
|
||||
return
|
||||
}
|
||||
|
||||
io.emit('player', clients.length < 2 || currentPlayer != clients[1] ? 'player1' : 'player2')
|
||||
if (client == player1 || client == player2) {
|
||||
client.emit('alert', '你已加入,无法重新加入。')
|
||||
return
|
||||
}
|
||||
|
||||
// 人走了
|
||||
client.on('disconnect', () => {
|
||||
const idx = clients.indexOf(client)
|
||||
clients.splice(idx, 1)
|
||||
if (player1 == null) {
|
||||
player1 = client
|
||||
client.emit('alert', '加入游戏成功,你是黑方。')
|
||||
io.emit('joined', { player: 'player1', ip: client.handshake.address })
|
||||
return
|
||||
}
|
||||
|
||||
clients.forEach((c) => {
|
||||
const i = clients.indexOf(c)
|
||||
if (i == 0) c.emit('role', '玩家1')
|
||||
else if (i == 1) c.emit('role', '玩家2')
|
||||
else c.emit('role', '旁观者')
|
||||
})
|
||||
|
||||
if (clients.length >= 2) {
|
||||
currentPlayer = clients[0]
|
||||
} else {
|
||||
currentPlayer = null
|
||||
}
|
||||
io.emit('player', clients.length < 2 || currentPlayer != clients[1] ? 'player1' : 'player2')
|
||||
})
|
||||
|
||||
client.on('click', () => {
|
||||
if (clients.length >= 2 && client == currentPlayer) {
|
||||
// 切换玩家
|
||||
if (currentPlayer === clients[0]) {
|
||||
currentPlayer = clients[1]
|
||||
io.emit('player', 'player2')
|
||||
} else {
|
||||
currentPlayer = clients[0]
|
||||
io.emit('player', 'player1')
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
if (player2 == null) {
|
||||
player2 = client
|
||||
client.emit('alert', '加入游戏成功,你是红方。')
|
||||
io.emit('joined', { player: 'player2', ip: client.handshake.address })
|
||||
return
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user