Skip to content

Commit

Permalink
Add support for decoding Team IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gigabyte5671 committed Jan 15, 2023
1 parent 5a7ce4b commit 7290e9d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion data/Enumfields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const generalEnumfields = {
'042E': {length: 4, name: undefined},
'042F': {length: 4, name: undefined},
'0448': {length: 4, type: 'Region', name: 'Region'},
'0452': {length: 4, name: 'Team ID'},
'0452': {length: 4, type: 'TeamID', name: 'Team ID'},
'0457': {length: 4, name: undefined},
'0458': {length: 4, name: undefined},
'0472': {length: 4, name: undefined},
Expand Down
7 changes: 7 additions & 0 deletions data/Teams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Team } from '../interfaces/index.js';

export const Teams = [
'Spectator',
'Diamond Sword',
'Blood Eagle'
] as Team[];
1 change: 1 addition & 0 deletions data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export { Items } from './Items.js';
export { Maps } from './Maps.js';
export { Regions } from './Regions.js';
export { loginServers } from './Servers.js';
export { Teams } from './Teams.js';
export { WatchNowSections } from './WatchNow.js';
13 changes: 11 additions & 2 deletions functions/Decoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generalEnumfields, IngameMessageTypes, Items, Maps, Regions, WatchNowSections } from '../data/index.js';
import type { EnumTree, Map } from '../interfaces/index.js';
import { generalEnumfields, IngameMessageTypes, Items, Maps, Regions, Teams, WatchNowSections } from '../data/index.js';
import type { EnumTree, Map, Team } from '../interfaces/index.js';
import { hexToString } from './Utils.js';

export interface DecoderOptions {
Expand Down Expand Up @@ -205,6 +205,15 @@ export class Decoder {
return output ?? id;
}

// Team IDs are represented by 4 byte integers.
// A map of team IDs to team data is available in the `data` module.
if (type === 'TeamID') {
if (value.length !== 4) {
if (this.#options.debug) console.warn('[Decoder] Error decoding Map ID (', value, '). Enumfield may be incorrectly typed.');
return value;
}
const id = parseInt(hexToString(new Uint8Array(value.reverse())), 16); // Decode ID as integer.
return Teams[id] ?? id;
}

// Region IDs are represented by 4 byte integers.
Expand Down
1 change: 1 addition & 0 deletions interfaces/Teams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Team = 'Spectator' | 'Diamond Sword' | 'Blood Eagle';
1 change: 1 addition & 0 deletions interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export type { Item } from './Items.js';
export type { Map } from './Maps.js';
export type { Region } from './Regions.js';
export type { LoginServer, GameServer } from './Servers.js';
export type { Team } from './Teams.js';
export type { WatchNowSection } from './WatchNow.js';

0 comments on commit 7290e9d

Please sign in to comment.