From 6ed265b26c08fb8c54c363da4de887d6097c7145 Mon Sep 17 00:00:00 2001 From: anxietypb Date: Tue, 13 Nov 2018 23:24:53 +0100 Subject: [PATCH] feat: use mapping to differ units / buildings / upgrades / items --- lib/Player.js | 21 ++++++++++++++------- lib/mappings.js | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/Player.js b/lib/Player.js index adf81d5..7b922f7 100644 --- a/lib/Player.js +++ b/lib/Player.js @@ -3,7 +3,7 @@ const reverseString = input => input.split('').reverse().join('') const isObjectId = input => ['u', 'e', 'h', 'o'].indexOf(input[0]) >= 0 const isRightclickAction = input => input[0] === 0x03 && input[1] === 0 const isBasicAction = input => input[0] <= 0x19 && input[1] === 0 -const {itemIds} = require('./mappings') +const {items, units, buildings, upgrades} = require('./mappings') function Player (id, name, teamid, color, race) { this.id = id @@ -57,6 +57,18 @@ Player.prototype.detectRaceByActionId = function (actionId) { } } +Player.prototype.handleActionId = function (actionId) { + if (units[actionId]) { + this.units[actionId] = this.units[actionId] + 1 || 1 + } else if (items[actionId]) { + this.items[actionId] = this.items[actionId] + 1 || 1 + } else if (buildings[actionId]) { + this.buildings[actionId] = this.buildings[actionId] + 1 || 1 + } else if (upgrades[actionId]) { + this.upgrades[actionId] = this.upgrades[actionId] + 1 || 1 + } +} + Player.prototype.handle0x10 = function (actionId) { if (typeof actionId === 'string') { actionId = reverseString(actionId) @@ -74,14 +86,9 @@ Player.prototype.handle0x10 = function (actionId) { case 'h': case 'o': if (!this.detectedRace) this.detectRaceByActionId(actionId) - this.units[actionId] = this.units[actionId] + 1 || 1 - break - default: - if (itemIds.indexOf(actionId) > -1) { - this.items[actionId] = this.items[actionId] + 1 || 1 - } break } + this.handleActionId(actionId) actionId[0] !== '0' ? this.actions['buildtrain'] = this.actions['buildtrain'] + 1 || 1 diff --git a/lib/mappings.js b/lib/mappings.js index a7753f7..5bc711b 100644 --- a/lib/mappings.js +++ b/lib/mappings.js @@ -240,7 +240,22 @@ const items = { 'dthb': 'i_Thunderbloom Bulb' } +const units = { + +} + +const buildings = { + +} + +const upgrades = { + +} + module.exports = { items, + units, + buildings, + upgrades, itemIds: Object.keys(items) }