110 lines
3.7 KiB
JavaScript
110 lines
3.7 KiB
JavaScript
const express = require('express')
|
|
const app = express()
|
|
const host = '0.0.0.0'
|
|
const port = 3000
|
|
app.use(require('compression')())
|
|
app.use(express.static('public'))
|
|
const server = app.listen(port, host, () => console.info(`包剪锤 http://${host}:${port}`))
|
|
const io = require('socket.io')(server)
|
|
|
|
const status = {
|
|
player1: {
|
|
id: '',
|
|
name: '玩家甲',
|
|
score: 0,
|
|
shape: '',
|
|
result: '',
|
|
current: '',
|
|
},
|
|
player2: {
|
|
id: '',
|
|
name: '玩家乙',
|
|
score: 0,
|
|
shape: '',
|
|
result: '',
|
|
current: '',
|
|
},
|
|
}
|
|
|
|
io.on('connection', (client) => {
|
|
if (!status.player1.id) {
|
|
status.player1.id = client.id
|
|
status.player1.name = '玩家甲'
|
|
status.player1.score = 0
|
|
status.player1.shape = ''
|
|
status.player1.result = ''
|
|
status.player1.current = ''
|
|
} else if (!status.player2.id) {
|
|
status.player2.id = client.id
|
|
status.player2.name = '玩家乙'
|
|
status.player2.score = 0
|
|
status.player2.shape = ''
|
|
status.player2.result = ''
|
|
status.player2.current = ''
|
|
}
|
|
io.emit('status', status)
|
|
|
|
client.on('rename', (name) => {
|
|
name = name.trim()
|
|
if (name == '' || name == status.player1.name || name == status.player2.name) return false
|
|
if (status.player1.id == client.id) status.player1.name = name
|
|
else if (status.player2.id == client.id) status.player2.name = name
|
|
io.emit('status', status)
|
|
})
|
|
|
|
client.on('disconnect', () => {
|
|
if (status.player1.id == client.id) {
|
|
status.player1.id = ''
|
|
status.player1.name = '玩家甲'
|
|
} else if (status.player2.id == client.id) {
|
|
status.player2.id = ''
|
|
status.player2.name = '玩家乙'
|
|
}
|
|
status.player1.score = 0
|
|
status.player1.shape = ''
|
|
status.player1.result = ''
|
|
status.player1.current = ''
|
|
status.player2.score = 0
|
|
status.player2.shape = ''
|
|
status.player2.result = ''
|
|
status.player2.current = ''
|
|
io.emit('status', status)
|
|
})
|
|
|
|
client.on('shape', (shape) => {
|
|
// 接收双方出拳,各一次机会
|
|
if (status.player1.id == client.id && !status.player1.current) status.player1.current = shape
|
|
else if (status.player2.id == client.id && !status.player2.current) status.player2.current = shape
|
|
// 比较双方出拳
|
|
if (status.player1.current && status.player2.current) {
|
|
status.player1.shape = status.player1.current
|
|
status.player2.shape = status.player2.current
|
|
status.player1.current = ''
|
|
status.player2.current = ''
|
|
if (status.player1.shape == status.player2.shape) {
|
|
// 平局各加0.5分
|
|
status.player1.score += 0.5
|
|
status.player2.score += 0.5
|
|
status.player1.result = 'draw'
|
|
status.player2.result = 'draw'
|
|
} else if (
|
|
// 玩家1胜利
|
|
(status.player1.shape == 'rock' && status.player2.shape == 'scissors') ||
|
|
(status.player1.shape == 'scissors' && status.player2.shape == 'paper') ||
|
|
(status.player1.shape == 'paper' && status.player2.shape == 'rock')
|
|
) {
|
|
status.player1.score++
|
|
status.player1.result = 'win'
|
|
status.player2.result = 'defeated'
|
|
} else {
|
|
// 玩家2胜利
|
|
status.player2.score++
|
|
status.player1.result = 'defeated'
|
|
status.player2.result = 'win'
|
|
}
|
|
io.emit('status', status)
|
|
console.table(status)
|
|
}
|
|
})
|
|
})
|