Skip to content

Commit

Permalink
fix: chat scope is parsed and formatted correctly
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
PBug90 authored Dec 13, 2018
1 parent 1c7bfed commit 393f9b2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions parsers/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const chatModeFormatter = (flag) => {
case 0x02:
return 'OBS'
}

if (flag >= 3 && flag <= 27) {
return `PRIVATE${flag}`
}

return flag
}

Expand Down
11 changes: 9 additions & 2 deletions parsers/gamedata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/formatters.test.js
Original file line number Diff line number Diff line change
@@ -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}`)
})
}
})

0 comments on commit 393f9b2

Please sign in to comment.