添加gzip
This commit is contained in:
parent
5022e45588
commit
127045ef4f
4913
package-lock.json
generated
4913
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -15,6 +15,7 @@
|
|||||||
"author": "Zhao Xin <7176466@qq.com>",
|
"author": "Zhao Xin <7176466@qq.com>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"compression": "^1.7.4",
|
||||||
"express": "^4.18.1",
|
"express": "^4.18.1",
|
||||||
"socketio": "^1.0.0"
|
"socketio": "^1.0.0"
|
||||||
},
|
},
|
||||||
|
15
server.js
15
server.js
@ -2,6 +2,7 @@ const express = require('express')
|
|||||||
const app = express()
|
const app = express()
|
||||||
const host = '0.0.0.0'
|
const host = '0.0.0.0'
|
||||||
const port = 3000
|
const port = 3000
|
||||||
|
app.use(require('compression')())
|
||||||
app.use(express.static('public'))
|
app.use(express.static('public'))
|
||||||
const server = app.listen(port, host, () => console.info(`包剪锤 http://${host}:${port}`))
|
const server = app.listen(port, host, () => console.info(`包剪锤 http://${host}:${port}`))
|
||||||
const io = require('socket.io')(server)
|
const io = require('socket.io')(server)
|
||||||
@ -12,6 +13,7 @@ const status = {
|
|||||||
name: '玩家甲',
|
name: '玩家甲',
|
||||||
score: 0,
|
score: 0,
|
||||||
shape: '',
|
shape: '',
|
||||||
|
result: '',
|
||||||
current: '',
|
current: '',
|
||||||
},
|
},
|
||||||
player2: {
|
player2: {
|
||||||
@ -19,6 +21,7 @@ const status = {
|
|||||||
name: '玩家乙',
|
name: '玩家乙',
|
||||||
score: 0,
|
score: 0,
|
||||||
shape: '',
|
shape: '',
|
||||||
|
result: '',
|
||||||
current: '',
|
current: '',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -29,12 +32,14 @@ io.on('connection', (client) => {
|
|||||||
status.player1.name = '玩家甲'
|
status.player1.name = '玩家甲'
|
||||||
status.player1.score = 0
|
status.player1.score = 0
|
||||||
status.player1.shape = ''
|
status.player1.shape = ''
|
||||||
|
status.player1.result = ''
|
||||||
status.player1.current = ''
|
status.player1.current = ''
|
||||||
} else if (!status.player2.id) {
|
} else if (!status.player2.id) {
|
||||||
status.player2.id = client.id
|
status.player2.id = client.id
|
||||||
status.player2.name = '玩家乙'
|
status.player2.name = '玩家乙'
|
||||||
status.player2.score = 0
|
status.player2.score = 0
|
||||||
status.player2.shape = ''
|
status.player2.shape = ''
|
||||||
|
status.player2.result = ''
|
||||||
status.player2.current = ''
|
status.player2.current = ''
|
||||||
}
|
}
|
||||||
io.emit('status', status)
|
io.emit('status', status)
|
||||||
@ -57,9 +62,11 @@ io.on('connection', (client) => {
|
|||||||
}
|
}
|
||||||
status.player1.score = 0
|
status.player1.score = 0
|
||||||
status.player1.shape = ''
|
status.player1.shape = ''
|
||||||
|
status.player1.result = ''
|
||||||
status.player1.current = ''
|
status.player1.current = ''
|
||||||
status.player2.score = 0
|
status.player2.score = 0
|
||||||
status.player2.shape = ''
|
status.player2.shape = ''
|
||||||
|
status.player2.result = ''
|
||||||
status.player2.current = ''
|
status.player2.current = ''
|
||||||
io.emit('status', status)
|
io.emit('status', status)
|
||||||
})
|
})
|
||||||
@ -78,14 +85,22 @@ io.on('connection', (client) => {
|
|||||||
// 平局各加0.5分
|
// 平局各加0.5分
|
||||||
status.player1.score += 0.5
|
status.player1.score += 0.5
|
||||||
status.player2.score += 0.5
|
status.player2.score += 0.5
|
||||||
|
status.player1.result = 'draw'
|
||||||
|
status.player2.result = 'draw'
|
||||||
} else if (
|
} else if (
|
||||||
|
// 玩家1胜利
|
||||||
(status.player1.shape == 'rock' && status.player2.shape == 'scissors') ||
|
(status.player1.shape == 'rock' && status.player2.shape == 'scissors') ||
|
||||||
(status.player1.shape == 'scissors' && status.player2.shape == 'paper') ||
|
(status.player1.shape == 'scissors' && status.player2.shape == 'paper') ||
|
||||||
(status.player1.shape == 'paper' && status.player2.shape == 'rock')
|
(status.player1.shape == 'paper' && status.player2.shape == 'rock')
|
||||||
) {
|
) {
|
||||||
status.player1.score++
|
status.player1.score++
|
||||||
|
status.player1.result = 'win'
|
||||||
|
status.player2.result = 'defeated'
|
||||||
} else {
|
} else {
|
||||||
|
// 玩家2胜利
|
||||||
status.player2.score++
|
status.player2.score++
|
||||||
|
status.player1.result = 'defeated'
|
||||||
|
status.player2.result = 'win'
|
||||||
}
|
}
|
||||||
io.emit('status', status)
|
io.emit('status', status)
|
||||||
console.table(status)
|
console.table(status)
|
||||||
|
Loading…
Reference in New Issue
Block a user