新建
This commit is contained in:
parent
0066cba512
commit
0e47630eeb
2437
package-lock.json
generated
Normal file
2437
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": "xiaomaodiaoyu",
|
||||
"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/xiaomaodiaoyu.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Zhao Xin <7176466@qq.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"express": "^4.18.1",
|
||||
"socketio": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.19"
|
||||
}
|
||||
}
|
0
public/game.js
Normal file
0
public/game.js
Normal file
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 name="viewport" ,content="width=device-width, initial-scale=1.0">
|
||||
<link rel="shortcut icon" href="data:" type="image/x-icon">
|
||||
<!-- <link rel="stylesheet" href="style.css"> -->
|
||||
<title>小猫钓鱼</title>
|
||||
<style>
|
||||
.ready {
|
||||
color: red;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>小猫钓鱼</h1>
|
||||
<h2>在线人数:<var id="onlineNumber">0</var></h2>
|
||||
<h2>玩家人数:<var id="playersNumber">0</var></h2>
|
||||
<button id="buttonReady">我准备好了</button>
|
||||
<div id="players"></div>
|
||||
<div id="cards"></div>
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
const player = io()
|
||||
|
||||
player.on('onlineNumber', (number) => {
|
||||
onlineNumber.innerText = number
|
||||
})
|
||||
|
||||
player.on('playersNumber', (number) => {
|
||||
playersNumber.innerText = number
|
||||
buttonReady.classList.remove('ready')
|
||||
})
|
||||
|
||||
player.on('ready', (ready) => {
|
||||
if(ready) buttonReady.classList.add('ready')
|
||||
else buttonReady.classList.remove('ready')
|
||||
})
|
||||
|
||||
player.on('status', (status)=>{
|
||||
console.table(status)
|
||||
})
|
||||
buttonReady.addEventListener('click', (e) => player.emit('ready'))
|
||||
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
71
server.js
Normal file
71
server.js
Normal file
@ -0,0 +1,71 @@
|
||||
const express = require('express')
|
||||
const game = express()
|
||||
game.use(express.static('public'))
|
||||
const host = process.env.HOST || '0.0.0.0'
|
||||
const port = process.env.PORT || 3000
|
||||
const server = game.listen(port, host, () => console.log(`小猫钓鱼游戏服务运行在 http://${host}:${port}/`))
|
||||
const io = require('socket.io')(server)
|
||||
|
||||
const clients = []
|
||||
const players = []
|
||||
const cards = []
|
||||
let current = 0
|
||||
let started = false
|
||||
|
||||
function isGameStarted() {
|
||||
if (players.length < 2) return false
|
||||
for (let player of players) {
|
||||
if (!player.ready) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
io.on('connection', (client) => {
|
||||
client.ready = false
|
||||
client.cards = []
|
||||
clients.push(client)
|
||||
io.emit('onlineNumber', clients.length)
|
||||
io.emit('playersNumber', players.length)
|
||||
|
||||
client.on('ready', () => {
|
||||
if (!isGameStarted()) {
|
||||
client.ready = !client.ready
|
||||
if (client.ready) {
|
||||
players.push(client)
|
||||
} else {
|
||||
const index = players.indexOf(client)
|
||||
players.splice(index, 1)
|
||||
}
|
||||
io.emit('playersNumber', players.length)
|
||||
client.emit('ready', client.ready)
|
||||
if (isGameStarted()) gameStart()
|
||||
}
|
||||
})
|
||||
|
||||
client.on('disconnect', () => {
|
||||
const index = clients.indexOf(client)
|
||||
clients.splice(index, 1)
|
||||
io.emit('onlineNumber', clients.length)
|
||||
})
|
||||
})
|
||||
|
||||
function gameStart() {
|
||||
console.log('游戏开始')
|
||||
// cards = []
|
||||
cards.length = 0
|
||||
current = 0
|
||||
// 洗牌 -> players.cards 每家18张
|
||||
// players[0].cards = [1, 2, 3]
|
||||
// players[1].cards = [1, 2, 3, 4]
|
||||
// players[2].cards = [1, 2, 3, 4, 5]
|
||||
// ...
|
||||
emitStatus()
|
||||
}
|
||||
|
||||
function emitStatus() {
|
||||
const cardsNumbers = []
|
||||
for (let p of players) {
|
||||
cardsNumbers.push(p.cards.length)
|
||||
}
|
||||
io.emit('status', { players: cardsNumbers, cards, current })
|
||||
}
|
Loading…
Reference in New Issue
Block a user