From 090a94cd2411bd87137ab6401315a2862cae2844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=91=AB?= <7176466@qq.com> Date: Fri, 19 Aug 2022 01:59:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0gzip=20compression?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 89 +++++++++++++++++++++++++++++++ package.json | 1 + server.js | 133 +++++++++++++++++++++++----------------------- 3 files changed, 157 insertions(+), 66 deletions(-) diff --git a/package-lock.json b/package-lock.json index bca2a8c..c243b59 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "license": "MIT", "dependencies": { + "compression": "^1.7.4", "express": "^4.18.1", "socketio": "^1.0.0" }, @@ -229,6 +230,47 @@ "resolved": "https://registry.npmjs.org/component-inherit/-/component-inherit-0.0.3.tgz", "integrity": "sha512-w+LhYREhatpVqTESyGFg3NlP6Iu0kEKUHETY9GoZP/pQyW4mHFZuFWRUCIqVPZ36ueVLtoOEZaAqbCF2RDndaA==" }, + "node_modules/compressible": { + "version": "2.0.18", + "resolved": "https://registry.npmjs.org/compressible/-/compressible-2.0.18.tgz", + "integrity": "sha512-AF3r7P5dWxL8MxyITRMlORQNaOA2IkAFaTr4k7BUumjPtRpGDTZpl0Pb1XCO6JeDCBdp126Cgs9sMxqSjgYyRg==", + "dependencies": { + "mime-db": ">= 1.43.0 < 2" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/compression": { + "version": "1.7.4", + "resolved": "https://registry.npmjs.org/compression/-/compression-1.7.4.tgz", + "integrity": "sha512-jaSIDzP9pZVS4ZfQ+TzvtiWhdpFhE2RDHz8QJkpX9SIpLq88VueF5jJw6t+6CUQcAoA6t+x89MLrWAqpfDE8iQ==", + "dependencies": { + "accepts": "~1.3.5", + "bytes": "3.0.0", + "compressible": "~2.0.16", + "debug": "2.6.9", + "on-headers": "~1.0.2", + "safe-buffer": "5.1.2", + "vary": "~1.1.2" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/compression/node_modules/bytes": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", + "integrity": "sha512-pMhOfFDPiv9t5jjIXkHosWmkSyQbvsgEVNkz0ERHbuLh2T/7j4Mqqpz523Fe8MVY89KC6Sh/QfS2sM+SjgFDcw==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/compression/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -870,6 +912,14 @@ "node": ">= 0.8" } }, + "node_modules/on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/options": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/options/-/options-0.0.6.tgz", @@ -1526,6 +1576,40 @@ "resolved": "https://registry.npmjs.org/component-inherit/-/component-inherit-0.0.3.tgz", "integrity": "sha512-w+LhYREhatpVqTESyGFg3NlP6Iu0kEKUHETY9GoZP/pQyW4mHFZuFWRUCIqVPZ36ueVLtoOEZaAqbCF2RDndaA==" }, + "compressible": { + "version": "2.0.18", + "resolved": "https://registry.npmjs.org/compressible/-/compressible-2.0.18.tgz", + "integrity": "sha512-AF3r7P5dWxL8MxyITRMlORQNaOA2IkAFaTr4k7BUumjPtRpGDTZpl0Pb1XCO6JeDCBdp126Cgs9sMxqSjgYyRg==", + "requires": { + "mime-db": ">= 1.43.0 < 2" + } + }, + "compression": { + "version": "1.7.4", + "resolved": "https://registry.npmjs.org/compression/-/compression-1.7.4.tgz", + "integrity": "sha512-jaSIDzP9pZVS4ZfQ+TzvtiWhdpFhE2RDHz8QJkpX9SIpLq88VueF5jJw6t+6CUQcAoA6t+x89MLrWAqpfDE8iQ==", + "requires": { + "accepts": "~1.3.5", + "bytes": "3.0.0", + "compressible": "~2.0.16", + "debug": "2.6.9", + "on-headers": "~1.0.2", + "safe-buffer": "5.1.2", + "vary": "~1.1.2" + }, + "dependencies": { + "bytes": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", + "integrity": "sha512-pMhOfFDPiv9t5jjIXkHosWmkSyQbvsgEVNkz0ERHbuLh2T/7j4Mqqpz523Fe8MVY89KC6Sh/QfS2sM+SjgFDcw==" + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + } + } + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -2027,6 +2111,11 @@ "ee-first": "1.1.1" } }, + "on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==" + }, "options": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/options/-/options-0.0.6.tgz", diff --git a/package.json b/package.json index c20a14e..c1fd9e8 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "author": "Zhao Xin <7176466@qq.com>", "license": "MIT", "dependencies": { + "compression": "^1.7.4", "express": "^4.18.1", "socketio": "^1.0.0" }, diff --git a/server.js b/server.js index d3b90a4..bc10faa 100644 --- a/server.js +++ b/server.js @@ -1,9 +1,10 @@ const express = require('express') -const 游戏 = express() -游戏.use(express.static('public')) +const 应用 = express() +应用.use(require('compression')()) +应用.use(express.static('public')) const 主机 = process.env.HOST || '0.0.0.0' const 端口 = process.env.PORT || 3000 -const 服务器 = 游戏.listen(端口, 主机, () => console.log(`小猫钓鱼游戏服务运行在 http://${主机}:${端口}/`)) +const 服务器 = 应用.listen(端口, 主机, () => console.log(`小猫钓鱼游戏服务运行在 http://${主机}:${端口}/`)) const IO = require('socket.io')(服务器) const clients = [] // 所有客户 @@ -70,68 +71,68 @@ function gameStart() { cardsOnTable.length = 0 currentPlayerNumber = 0 // 洗牌,每玩家发18张 - const drawer = [ - { color: '♠️', value: '2' }, - { color: '♠️', value: '3' }, - { color: '♠️', value: '4' }, - { color: '♠️', value: '5' }, - { color: '♠️', value: '6' }, - { color: '♠️', value: '7' }, - { color: '♠️', value: '8' }, - { color: '♠️', value: '9' }, - { color: '♠️', value: '10' }, - { color: '♠️', value: 'J' }, - { color: '♠️', value: 'Q' }, - { color: '♠️', value: 'K' }, - { color: '♠️', value: 'A' }, - { color: '♥️', value: '2' }, - { color: '♥️', value: '3' }, - { color: '♥️', value: '4' }, - { color: '♥️', value: '5' }, - { color: '♥️', value: '6' }, - { color: '♥️', value: '7' }, - { color: '♥️', value: '8' }, - { color: '♥️', value: '9' }, - { color: '♥️', value: '10' }, - { color: '♥️', value: 'J' }, - { color: '♥️', value: 'Q' }, - { color: '♥️', value: 'K' }, - { color: '♥️', value: 'A' }, - { color: '♣️', value: '2' }, - { color: '♣️', value: '3' }, - { color: '♣️', value: '4' }, - { color: '♣️', value: '5' }, - { color: '♣️', value: '6' }, - { color: '♣️', value: '7' }, - { color: '♣️', value: '8' }, - { color: '♣️', value: '9' }, - { color: '♣️', value: '10' }, - { color: '♣️', value: 'J' }, - { color: '♣️', value: 'Q' }, - { color: '♣️', value: 'K' }, - { color: '♣️', value: 'A' }, - { color: '♦️', value: '2' }, - { color: '♦️', value: '3' }, - { color: '♦️', value: '4' }, - { color: '♦️', value: '5' }, - { color: '♦️', value: '6' }, - { color: '♦️', value: '7' }, - { color: '♦️', value: '8' }, - { color: '♦️', value: '9' }, - { color: '♦️', value: '10' }, - { color: '♦️', value: 'J' }, - { color: '♦️', value: 'Q' }, - { color: '♦️', value: 'K' }, - { color: '♦️', value: 'A' }, - { color: '小', value: 'Joker' }, - { color: '大', value: 'Joker' }, + const deck = [ + { suit: 's', rank: '2' }, + { suit: 's', rank: '3' }, + { suit: 's', rank: '4' }, + { suit: 's', rank: '5' }, + { suit: 's', rank: '6' }, + { suit: 's', rank: '7' }, + { suit: 's', rank: '8' }, + { suit: 's', rank: '9' }, + { suit: 's', rank: 'T' }, + { suit: 's', rank: 'J' }, + { suit: 's', rank: 'Q' }, + { suit: 's', rank: 'K' }, + { suit: 's', rank: 'A' }, + { suit: 'h', rank: '2' }, + { suit: 'h', rank: '3' }, + { suit: 'h', rank: '4' }, + { suit: 'h', rank: '5' }, + { suit: 'h', rank: '6' }, + { suit: 'h', rank: '7' }, + { suit: 'h', rank: '8' }, + { suit: 'h', rank: '9' }, + { suit: 'h', rank: 'T' }, + { suit: 'h', rank: 'J' }, + { suit: 'h', rank: 'Q' }, + { suit: 'h', rank: 'K' }, + { suit: 'h', rank: 'A' }, + { suit: 'c', rank: '2' }, + { suit: 'c', rank: '3' }, + { suit: 'c', rank: '4' }, + { suit: 'c', rank: '5' }, + { suit: 'c', rank: '6' }, + { suit: 'c', rank: '7' }, + { suit: 'c', rank: '8' }, + { suit: 'c', rank: '9' }, + { suit: 'c', rank: 'T' }, + { suit: 'c', rank: 'J' }, + { suit: 'c', rank: 'Q' }, + { suit: 'c', rank: 'K' }, + { suit: 'c', rank: 'A' }, + { suit: 'd', rank: '2' }, + { suit: 'd', rank: '3' }, + { suit: 'd', rank: '4' }, + { suit: 'd', rank: '5' }, + { suit: 'd', rank: '6' }, + { suit: 'd', rank: '7' }, + { suit: 'd', rank: '8' }, + { suit: 'd', rank: '9' }, + { suit: 'd', rank: 'T' }, + { suit: 'd', rank: 'J' }, + { suit: 'd', rank: 'Q' }, + { suit: 'd', rank: 'K' }, + { suit: 'd', rank: 'A' }, + { suit: '1', rank: '0' }, + { suit: '2', rank: '0' }, ] - while (drawer.length > 0) { - let index = Math.floor(Math.random() * drawer.length) - let card = drawer[index] + while (deck.length > 0) { + let index = Math.floor(Math.random() * deck.length) + let card = deck[index] players[currentPlayerNumber].cards.push(card) - drawer.splice(index, 1) + deck.splice(index, 1) currentPlayerNumber = (currentPlayerNumber + 1) % 3 } @@ -147,12 +148,12 @@ function gameStatus() { const cardsStrings = [] for (let card of cardsOnTable) { - cardsStrings.push(`[${card.color}${card.value}]`) + cardsStrings.push(`${card.rank}${card.suit}`) } IO.emit('status', { players: cardsNumbers, - cards: cardsStrings.join(''), + cards: cardsStrings, current: currentPlayerNumber, }) } @@ -161,13 +162,13 @@ function go(client) { if (currentPlayerNumber != -1 && players.indexOf(client) === currentPlayerNumber) { let card = players[currentPlayerNumber].cards[0] players[currentPlayerNumber].cards.splice(0, 1) - console.log(`玩家${currentPlayerNumber + 1} 出牌 [${card.color} ${card.value}]`) + console.log(`玩家${currentPlayerNumber + 1} 出牌 [${card.rank}${card.suit}]`) // 判断有没有钓到 let i = 0 let getit = false for (i = 0; i < cardsOnTable.length; i++) { - if (cardsOnTable[i].value == card.value) { + if (cardsOnTable[i].rank == card.rank) { getit = true break }