Skip to content

Commit

Permalink
fix: replace classic player names with reforged metadata names (#49)
Browse files Browse the repository at this point in the history
* fix: replace classic player names with reforged metadata names

* improvement: extract playerid from extra player list, overwrite name
  • Loading branch information
PBug90 authored Mar 3, 2020
1 parent b862519 commit 36a6d0c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
Binary file added replays/reforged_truncated_playernames.w3g
Binary file not shown.
18 changes: 12 additions & 6 deletions src/W3GReplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
CommandDataBlock,
ParserOutput,
PlayerChatMessageBlock,
Platform
Platform,
ExtraPlayerListEntry
} from './types'
import { sortPlayers } from './sort'

Expand Down Expand Up @@ -91,18 +92,23 @@ class W3GReplay extends ReplayParser {
return this.finalize()
}

handleMetaData (metaData: GameMetaDataDecoded) {
handleMetaData (metaData: GameMetaDataDecoded): void {
this.slots = metaData.playerSlotRecords
this.playerList = [metaData.player, ...metaData.playerList]
this.meta = metaData
const tempPlayers: {[key: string]: GameMetaDataDecoded['player'] } = {}
this.teams = []
this.players = {}

this.playerList.forEach((player: GameMetaDataDecoded['player']): void => {
tempPlayers[player.playerId] = player
})

if (metaData.extraPlayerList) {
metaData.extraPlayerList.forEach((extraPlayer: ExtraPlayerListEntry) => {
tempPlayers[extraPlayer.playerId].playerName = extraPlayer.name
})
}

this.slots.forEach((slot: SlotRecord) => {
if (slot.slotStatus > 1) {
this.teams[slot.teamId] = this.teams[slot.teamId] || []
Expand All @@ -115,7 +121,7 @@ class W3GReplay extends ReplayParser {
})
}

processGameDataBlock (block: GameDataBlock) {
processGameDataBlock (block: GameDataBlock): void {
switch (block.type) {
case 31:
case 30:
Expand Down Expand Up @@ -143,7 +149,7 @@ class W3GReplay extends ReplayParser {
})
}

processCommandDataBlock (block: CommandDataBlock) {
processCommandDataBlock (block: CommandDataBlock): void {
const currentPlayer = this.players[block.playerId]
currentPlayer.currentTimePlayed = this.totalTimeTracker
currentPlayer._lastActionWasDeselect = false
Expand All @@ -156,7 +162,7 @@ class W3GReplay extends ReplayParser {
}
}

handleActionBlock (action: ActionBlock, currentPlayer: Player) {
handleActionBlock (action: ActionBlock, currentPlayer: Player): void {
this.playerActionTracker[currentPlayer.id] = this.playerActionTracker[currentPlayer.id] || []
this.playerActionTracker[currentPlayer.id].push(action)

Expand Down
5 changes: 4 additions & 1 deletion src/parsers/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ const GameMetaDataReforged = (buildNo: number) => new Parser()
.array('extraPlayerList', {
type: new Parser()
.int8('preVars1')
.buffer('pre', { length: 4 })
.int8('pre1')
.int8('pre2')
.int8('playerId')
.int8('pre4')
.int8('nameLength')
.string('name', { length: 'nameLength' })
.skip(1)
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export interface SlotRecord {
handicapFlag: number;
}

export interface ExtraPlayerListEntry{
nameLength: number;
name: string;
playerId: number;
}

export interface GameMetaDataDecoded {
player: {
hasRecord?: number;
Expand Down Expand Up @@ -65,6 +71,7 @@ export interface GameMetaDataDecoded {
mapChecksum: string;
mapName: string;
creator: string;
extraPlayerList?: ExtraPlayerListEntry[];
}

export interface ActionBlock{
Expand Down
9 changes: 9 additions & 0 deletions test/replays.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,13 @@ describe('Replay parsing tests', () => {
expect(test.players[0].name).toBe('HurricaneBo')
expect(test.players[1].name).toBe('SimplyHunteR')
})

it('handles truncated player names in reforged replays', () => {
const test = Parser.parse('./replays/reforged_truncated_playernames.w3g')
expect(test.version).toBe('1.32')
expect(test.buildNumber).toBe(6105)
expect(test.players.length).toBe(2)
expect(test.players[0].name).toBe('WaN#1734')
expect(test.players[1].name).toBe('РозовыйПони#228941')
})
})

0 comments on commit 36a6d0c

Please sign in to comment.