137 lines
4.7 KiB
JavaScript
137 lines
4.7 KiB
JavaScript
|
const express = require('express')
|
|||
|
const app = express()
|
|||
|
app.use(express.static('public'))
|
|||
|
const host = process.env.HOST || '0.0.0.0'
|
|||
|
const port = process.env.PORT || 8000
|
|||
|
const server = app.listen(port, host, () => console.log(`陆战棋游戏运行在:http://${host}:${port}/`))
|
|||
|
const io = require('socket.io')(server)
|
|||
|
|
|||
|
const clients = []
|
|||
|
let currentPlayer = null
|
|||
|
|
|||
|
io.on('connection', (client) => {
|
|||
|
// 来人了
|
|||
|
clients.push(client)
|
|||
|
|
|||
|
const i = clients.indexOf(client)
|
|||
|
if (i == 0) client.emit('role', '玩家1')
|
|||
|
else if (i == 1) client.emit('role', '玩家2')
|
|||
|
else client.emit('role', '旁观者')
|
|||
|
|
|||
|
if (clients.length == 2) {
|
|||
|
currentPlayer = clients[0]
|
|||
|
}
|
|||
|
|
|||
|
io.emit('player', clients.length < 2 || currentPlayer != clients[1] ? 'player1' : 'player2')
|
|||
|
|
|||
|
// 人走了
|
|||
|
client.on('disconnect', () => {
|
|||
|
const idx = clients.indexOf(client)
|
|||
|
clients.splice(idx, 1)
|
|||
|
|
|||
|
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')
|
|||
|
}
|
|||
|
}
|
|||
|
})
|
|||
|
|
|||
|
// console.log(`${client.handshake.address} 来了`)
|
|||
|
// client.emit('client', '旁观者')
|
|||
|
|
|||
|
// if (players.length < 2) {
|
|||
|
// players.push(client)
|
|||
|
// if (players.length == 2) {
|
|||
|
// // 游戏开始
|
|||
|
// console.log('玩家就位,游戏开始')
|
|||
|
// currentPlayer = players[0]
|
|||
|
// io.emit('currentPlayer', currentPlayer === players[0] ? 'player1' : 'player2')
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// if (players.length > 0 && client.id == players[0].id) client.emit('client', '玩家1')
|
|||
|
// else if (players.length == 2 && client.id == players[1].id) client.emit('client', '玩家2')
|
|||
|
// else client.emit('client', '旁观者')
|
|||
|
|
|||
|
// client.on('click', () => {
|
|||
|
// if (client == currentPlayer) {
|
|||
|
// // 切换玩家
|
|||
|
// if (currentPlayer === players[0]) {
|
|||
|
// currentPlayer = players[1]
|
|||
|
// io.emit('currentPlayer', 'player2')
|
|||
|
// } else {
|
|||
|
// currentPlayer = players[0]
|
|||
|
// io.emit('currentPlayer', 'player1')
|
|||
|
// }
|
|||
|
// }
|
|||
|
// })
|
|||
|
|
|||
|
// client.on('disconnect', () => {
|
|||
|
// io.emit('client', '旁观者')
|
|||
|
// if (players.includes(client)) {
|
|||
|
// const playerNumber = players.indexOf(client)
|
|||
|
// players.splice(playerNumber, 1)
|
|||
|
// }
|
|||
|
// if (players.length == 2) {
|
|||
|
// // 游戏开始
|
|||
|
// console.log('玩家就位,游戏开始')
|
|||
|
// currentPlayer = players[0]
|
|||
|
// io.emit('currentPlayer', currentPlayer === players[0] ? 'player1' : 'player2')
|
|||
|
// }
|
|||
|
// if (players.length > 0 && client.id == players[0].id) client.emit('client', '玩家1')
|
|||
|
// else if (players.length == 2 && client.id == players[1].id) client.emit('client', '玩家2')
|
|||
|
// else client.emit('client', '旁观者')
|
|||
|
// })
|
|||
|
|
|||
|
// if (players.length < 2) {
|
|||
|
// players.push(client)
|
|||
|
// console.log(`玩家${players.length}加入了[${client.handshake.address}]`)
|
|||
|
// if (players.length == 2) {
|
|||
|
// // 广播当前是player1(玩家1, 黑色)
|
|||
|
// currentPlayer = players[0]
|
|||
|
// io.emit('currentPlayer', 'player1')
|
|||
|
// // 向玩家1发出点击许可
|
|||
|
// currentPlayer.emit('yourTurn')
|
|||
|
// }
|
|||
|
// } else {
|
|||
|
// console.log(`旁观者${client.handshake.address}来了`)
|
|||
|
// if (currentPlayer === players[0]) {
|
|||
|
// io.emit('currentPlayer', 'player1')
|
|||
|
// } else {
|
|||
|
// io.emit('currentPlayer', 'player2')
|
|||
|
// }
|
|||
|
// }
|
|||
|
// client.on('click', () => {
|
|||
|
// console.log(client.handshake.address, '点了按钮')
|
|||
|
// if (currentPlayer === players[0]) {
|
|||
|
// io.emit('currentPlayer', 'player2')
|
|||
|
// currentPlayer = players[1]
|
|||
|
// } else {
|
|||
|
// io.emit('currentPlayer', 'player1')
|
|||
|
// currentPlayer = players[0]
|
|||
|
// }
|
|||
|
// currentPlayer.emit('yourTurn')
|
|||
|
// })
|
|||
|
})
|