添加gzip

This commit is contained in:
赵鑫 2022-08-21 11:01:20 +08:00
parent 5022e45588
commit 127045ef4f
3 changed files with 2517 additions and 2412 deletions

4913
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,7 @@
"author": "Zhao Xin <7176466@qq.com>",
"license": "MIT",
"dependencies": {
"compression": "^1.7.4",
"express": "^4.18.1",
"socketio": "^1.0.0"
},

View File

@ -2,6 +2,7 @@ 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)
@ -12,6 +13,7 @@ const status = {
name: '玩家甲',
score: 0,
shape: '',
result: '',
current: '',
},
player2: {
@ -19,6 +21,7 @@ const status = {
name: '玩家乙',
score: 0,
shape: '',
result: '',
current: '',
},
}
@ -29,12 +32,14 @@ io.on('connection', (client) => {
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)
@ -57,9 +62,11 @@ io.on('connection', (client) => {
}
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)
})
@ -78,14 +85,22 @@ io.on('connection', (client) => {
// 平局各加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)