From 393f9b2cfe7df9562a4c888b42eacc92c5ef4170 Mon Sep 17 00:00:00 2001 From: anXieTyPB Date: Thu, 13 Dec 2018 19:55:02 +0100 Subject: [PATCH] fix: chat scope is parsed and formatted correctly * fix: chat audience is being parsed properly now * test: add test for chat formatter * improvement: differ 0x10 and 0x20 chat message types * improvement: revert changes used for testing in example.j --- package.json | 2 +- parsers/formatters.js | 5 +++++ parsers/gamedata.js | 11 +++++++++-- test/formatters.test.js | 19 +++++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test/formatters.test.js diff --git a/package.json b/package.json index 8463f6c..5bdde43 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "jest": "^23.5.0" }, "scripts": { - "test": "jest test/test.js", + "test": "jest", "coverage": "jest test/test.js --collectCoverage", "lint": "eslint --ext .jsx,.js lib/ test/", "lint:fix": "eslint --ext .jsx,.js lib/ --fix" diff --git a/parsers/formatters.js b/parsers/formatters.js index d9ee68a..3eedee9 100644 --- a/parsers/formatters.js +++ b/parsers/formatters.js @@ -37,6 +37,11 @@ const chatModeFormatter = (flag) => { case 0x02: return 'OBS' } + + if (flag >= 3 && flag <= 27) { + return `PRIVATE${flag}` + } + return flag } diff --git a/parsers/gamedata.js b/parsers/gamedata.js index 28bd9b8..20126d8 100644 --- a/parsers/gamedata.js +++ b/parsers/gamedata.js @@ -7,7 +7,7 @@ const { Parser } = require('binary-parser') const { CommandDataBlock } = require('./actions') -const {chatModeFormatter} = require('./formatters') +const { chatModeFormatter } = require('./formatters') // 0x17 const LeaveGameBlock = new Parser() @@ -45,7 +45,14 @@ const PlayerChatMessageBlock = new Parser() .int8('playerId') .int16le('byteCount') .int8('flags') - .int32('chatMode', {formatter: chatModeFormatter}) + .choice( + {tag: 'flags', + choices: { + 0x10: new Parser(), + 0x20: new Parser().int8('chatMode', {length: 4, formatter: chatModeFormatter, encoding: 'hex'}).skip(3) + } + } + ) .string('message', {zeroTerminated: true, encoding: 'utf8'}) // 0x22 diff --git a/test/formatters.test.js b/test/formatters.test.js new file mode 100644 index 0000000..dfaa63d --- /dev/null +++ b/test/formatters.test.js @@ -0,0 +1,19 @@ +const formatters = require('../parsers/formatters') + +describe('chatModeFormatter', () => { + const {chatModeFormatter} = formatters + it('correctly handles 0 as ALL', () => { + expect(chatModeFormatter(0)).toBe('ALL') + }) + it('correctly handles 1 as ALLY', () => { + expect(chatModeFormatter(1)).toBe('ALLY') + }) + it('correctly handles 2 as OBS', () => { + expect(chatModeFormatter(2)).toBe('OBS') + }) + for (let i = 0; i < 24; i++) { + it(`correctly handles private message to slot ${i}`, () => { + expect(chatModeFormatter(3 + i)).toBe(`PRIVATE${i + 3}`) + }) + } +})