基本实现两个玩家依次行棋的逻辑
This commit is contained in:
parent
7b33d4db97
commit
1ca2c8f4a3
2088
package-lock.json
generated
Normal file
2088
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
25
package.json
Normal file
25
package.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "luzhanqi",
|
||||
"version": "0.1.0",
|
||||
"description": "陆战棋",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"dev": "nodemon server",
|
||||
"start": "node server",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "ssh://git@nas:2222/zhaoxin/luzhanqi.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Zhao Xin <7176466@qq.com>",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.19"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.18.1",
|
||||
"socket.io": "^4.5.1"
|
||||
}
|
||||
}
|
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 50 KiB |
51
public/index.html
Normal file
51
public/index.html
Normal file
@ -0,0 +1,51 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="shortcut icon" href="data:" type="image/x-icon">
|
||||
<title>陆战棋游戏</title>
|
||||
<style>
|
||||
button {
|
||||
padding: 0.5rem;
|
||||
color: lightgray;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.player1 {
|
||||
color: black;
|
||||
}
|
||||
|
||||
.player2 {
|
||||
color: red;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>陆战棋游戏</h1>
|
||||
<h2 id="role"></h2>
|
||||
<button id="gameButton">玩家1</button>
|
||||
<script src="/socket.io/socket.io.min.js"></script>
|
||||
<script>
|
||||
const client = io()
|
||||
|
||||
client.on('role', (name) => {
|
||||
role.innerText = name
|
||||
})
|
||||
|
||||
client.on('player', (player) => {
|
||||
gameButton.classList = player
|
||||
gameButton.innerText = player == 'player1' ? '玩家1' : '玩家2'
|
||||
})
|
||||
|
||||
gameButton.onclick = () => {
|
||||
client.emit('click')
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
136
server.js
Normal file
136
server.js
Normal file
@ -0,0 +1,136 @@
|
||||
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')
|
||||
// })
|
||||
})
|
Loading…
Reference in New Issue
Block a user