Skip to content

Commit

Permalink
fix: properly converts maps with backslash and/or forward slash
Browse files Browse the repository at this point in the history
  • Loading branch information
PBug90 committed Aug 28, 2019
1 parent 838fe15 commit a143318
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@
},
"globals": {
"ts-jest": {
"tsConfig": "tsconfig.json"
}
"tsConfig": "tsconfig.json",
"diagnostics": false
}
},
"testEnvironment": "node",
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
Expand Down
2 changes: 1 addition & 1 deletion src/W3GReplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class W3GReplay extends ReplayParser {
},
map: {
path: this.meta.mapName,
file: this.meta.mapName.split('\\').pop() || '',
file: convert.mapFilename(this.meta.mapName),
checksum: this.meta.mapChecksum
},
version: convert.gameVersion(this.header.version),
Expand Down
11 changes: 10 additions & 1 deletion src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ const gameVersion = (version: number): string => {
return `1.${version}`
}

const mapFilename = (mapPath: string): string => {
const fragment = mapPath.split('\\').pop()
if (fragment !== undefined){
return <string>fragment.split('//').pop()
}
return ''
}

export default {
playerColor,
gameVersion
gameVersion,
mapFilename
}
2 changes: 1 addition & 1 deletion src/parsers/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const SaveGameAction = new Parser()

// @ts-ignore
const SaveGameFinishedAction = new Parser()
.int16le()
.int16le("")

const UnitBuildingAbilityActionNoParams = new Parser()
.int16le('abilityFlags')
Expand Down
27 changes: 27 additions & 0 deletions test/convert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import convert from '../src/convert'

describe('mapFilename', () => {
it('returns mapfilename if path separator is \\ ', () => {
expect(convert.mapFilename(`Maps\\test\\somemap.w3x`)).toBe('somemap.w3x')
})

it('returns mapfilename if path separator is // ', () => {
expect(convert.mapFilename(`Maps//test//somemap.w3x`)).toBe('somemap.w3x')
})

it('returns mapfilename if path separator is // and then \\ ', () => {
expect(convert.mapFilename(`Maps//test\\somemap.w3x`)).toBe('somemap.w3x')
})

it('returns mapfilename if path separator is \\ and then // ', () => {
expect(convert.mapFilename(`Maps\\test//somemap.w3x`)).toBe('somemap.w3x')
})

it('returns mapfilename if path separator is \\ and then // repeated multiple times ', () => {
expect(convert.mapFilename(`Maps\\test\\test2//test3//somemap.w3x`)).toBe('somemap.w3x')
})

it('returns mapfilename if path separator is // and then \\ repeated multiple times ', () => {
expect(convert.mapFilename(`Maps//test//test2\\test3\\somemap.w3x`)).toBe('somemap.w3x')
})
})
25 changes: 25 additions & 0 deletions test/replays.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ describe('Replay parsing tests', () => {
expect(test.players.length).toBe(2)
expect(test.parseTime).toBeLessThan(300)
expect(test.parseTime).toBe(Math.round(test.parseTime))
expect(test.map).toEqual({
"checksum": "008ab7f1",
"file": "w3arena__twistedmeadows__v3.w3x",
"path": "Maps\\w3arena\\w3arena__twistedmeadows__v3.w3x"
})
})

it('parses a standard 1.26 replay properly', () => {
Expand All @@ -76,6 +81,11 @@ describe('Replay parsing tests', () => {
expect(test.matchup).toBe('HvU')
expect(test.type).toBe('1on1')
expect(test.players.length).toBe(2)
expect(test.map).toEqual({
"checksum": "51a1c63b",
"file": "w3arena__amazonia__v3.w3x",
"path": "Maps\\w3arena\\w3arena__amazonia__v3.w3x",
})
})

it('parses a netease 1.29 replay properly', () => {
Expand All @@ -88,6 +98,11 @@ describe('Replay parsing tests', () => {
expect(test.matchup).toBe('NvN')
expect(test.type).toBe('1on1')
expect(test.players.length).toBe(2)
expect(test.map).toEqual({
"checksum": "281f9d6a",
"file": "(4)TurtleRock.w3x",
"path": "Maps/1.29\\(4)TurtleRock.w3x",
})
})

it('parses a 2on2standard 1.29 replay properly', () => {
Expand All @@ -104,6 +119,11 @@ describe('Replay parsing tests', () => {
expect(test.matchup).toBe('HUvHU')
expect(test.type).toBe('2on2')
expect(test.players.length).toBe(4)
expect(test.map).toEqual({
"checksum": "b4230d1e",
"file": "w3arena__maelstrom__v2.w3x",
"path": "Maps\\w3arena\\w3arena__maelstrom__v2.w3x",
})
})

it('parses a standard 1.30 replay properly', () => {
Expand All @@ -121,6 +141,11 @@ describe('Replay parsing tests', () => {
expect(test.players[0].heroes[0]).toEqual(expect.objectContaining({ id: 'Udea', level: 6 }))
expect(test.players[0].heroes[1]).toEqual(expect.objectContaining({ id: 'Ulic', level: 6 }))
expect(test.players[0].heroes[2]).toEqual(expect.objectContaining({ id: 'Udre', level: 3 }))
expect(test.map).toEqual({
"file": "(4)TwistedMeadows.w3x",
"checksum": "c3cae01d",
"path": "Maps\\FrozenThrone\\(4)TwistedMeadows.w3x",
})
})

it('parsing result has the correct schema', () => {
Expand Down

0 comments on commit a143318

Please sign in to comment.