Skip to content

Commit

Permalink
feat: use mapping to differ units / buildings / upgrades / items
Browse files Browse the repository at this point in the history
  • Loading branch information
PBug90 committed Nov 13, 2018
1 parent 8cc7f53 commit 6ed265b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6ed265b

Please sign in to comment.