Skip to content

Commit

Permalink
feat: calculate group hotkeys for players and add to parser output(#63)
Browse files Browse the repository at this point in the history
* adds hotkeys to parser output

* renames hotkeys to groupHotkeys

* adds group hotkeys to schema
  • Loading branch information
DavidMente authored Nov 4, 2020
1 parent 39515ab commit 0fdb560
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import convert from "./convert";
import { items, units, buildings, upgrades, abilityToHero } from "./mappings";
import { Race, ItemID } from "./types";
import { TransferResourcesActionWithPlayer } from "./W3GReplay";
import { Action } from "./parsers/ActionParser";

const isRightclickAction = (input: number[]) =>
input[0] === 0x03 && input[1] === 0;
Expand Down Expand Up @@ -87,6 +88,9 @@ class Player {
selecthotkey: number;
esc: number;
};
groupHotkeys: {
[key: number]: { assigned: number; used: number };
};
resourceTransfers: TransferResourcesActionWithPlayerAndTimestamp[] = [];
_currentlyTrackedAPM: number;
_retrainingMetadata: { [key: string]: { start: number; end: number } };
Expand Down Expand Up @@ -130,6 +134,18 @@ class Player {
selecthotkey: 0,
esc: 0,
};
this.groupHotkeys = {
1: { assigned: 0, used: 0 },
2: { assigned: 0, used: 0 },
3: { assigned: 0, used: 0 },
4: { assigned: 0, used: 0 },
5: { assigned: 0, used: 0 },
6: { assigned: 0, used: 0 },
7: { assigned: 0, used: 0 },
8: { assigned: 0, used: 0 },
9: { assigned: 0, used: 0 },
0: { assigned: 0, used: 0 },
};
this._currentlyTrackedAPM = 0;
this._lastActionWasDeselect = false;
this._retrainingMetadata = {};
Expand Down Expand Up @@ -302,15 +318,17 @@ class Player {
});
}

handleOther(actionId: number): void {
switch (actionId) {
handleOther(action: Action): void {
switch (action.id) {
case 0x17:
this.actions.assigngroup++;
this._currentlyTrackedAPM++;
this.groupHotkeys[(action.groupNumber + 1) % 10].assigned++;
break;
case 0x18:
this.actions.selecthotkey++;
this._currentlyTrackedAPM++;
this.groupHotkeys[(action.groupNumber + 1) % 10].used++;
break;
case 0x1c:
case 0x1d:
Expand Down
2 changes: 1 addition & 1 deletion src/W3GReplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export default class W3GReplay extends EventEmitter {
case 0x65:
case 0x66:
case 0x67:
currentPlayer.handleOther(action.id);
currentPlayer.handleOther(action);
break;
case 0x51: {
const playerId = this.getPlayerBySlotId(action.slot);
Expand Down
8 changes: 8 additions & 0 deletions test/replays/132/replays.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,11 @@ it("parses 1.32.8 replay with observer on defeat setting", async () => {
);
expect(test.settings.observerMode).toBe("ON_DEFEAT");
});

it("should parse hotkeys correctly", async () => {
const test = await Parser.parse(path.resolve(__dirname, "reforged1.w3g"));
expect(test.players[0].groupHotkeys[1]).toEqual({ assigned: 1, used: 29 });
expect(test.players[0].groupHotkeys[2]).toEqual({ assigned: 1, used: 60 });
expect(test.players[1].groupHotkeys[1]).toEqual({ assigned: 21, used: 106 });
expect(test.players[1].groupHotkeys[2]).toEqual({ assigned: 4, used: 64 });
});
7 changes: 6 additions & 1 deletion test/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@
}
}
}
},
"groupHotkeys": {
"$id": "#/properties/players/items/properties/groupHotkeys",
"type": "object",
"title": "The group hotkeys schema"
}
}
}
Expand Down Expand Up @@ -712,4 +717,4 @@
}
}
}
}
}

0 comments on commit 0fdb560

Please sign in to comment.