Skip to content

Commit

Permalink
refactor some code lines, increase version
Browse files Browse the repository at this point in the history
* Base32: use object assign and map
* DeckEncoder: use reduce instead of push
  • Loading branch information
chrysomallos committed May 22, 2022
1 parent eb8fbdc commit 141824d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "runeterra",
"version": "4.0.0",
"version": "4.0.1",
"description": "Legends of Runeterra deck code encoder/decoder",
"main": "src/index.js",
"types": "index.d.ts",
Expand Down
9 changes: 2 additions & 7 deletions src/Base32.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ class Base32 {
}

static decode (encoded) {
encoded = encoded.trim().replace(Base32.SEPARATOR, '')
encoded = encoded.replace(/[=]*$/, '')
encoded = encoded.toUpperCase()
encoded = encoded.trim().replace(Base32.SEPARATOR, '').replace(/[=]*$/, '').toUpperCase()

if (encoded.length === 0) return [0]
const encodedLength = encoded.length
Expand Down Expand Up @@ -91,10 +89,7 @@ class Base32 {
Base32.DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.split('')
Base32.MASK = Base32.DIGITS.length - 1
Base32.SHIFT = Base32.numberOfTrailingZeros(Base32.DIGITS.length)
Base32.CHAR_MAP = Base32.DIGITS.reduce((m, d, i) => {
m[d.toString()] = i
return m
}, {})
Base32.CHAR_MAP = Object.assign(...Base32.DIGITS.map((d, i) => ({ [d]: i })))
Base32.SEPARATOR = '-'

module.exports = Base32
55 changes: 24 additions & 31 deletions src/DeckEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class DeckEncoder {
throw new TypeError('The provided code requires a higher version of this library; please update.')
}

for (let i = 3; i > 0; i--) {
// first encoded cards are grouped by 3, 2, 1 pieces
DeckEncoder.GROUPS.forEach(i => {
const numGroupOfs = VarInt.pop(bytes)

for (let j = 0; j < numGroupOfs; j++) {
Expand All @@ -43,7 +44,7 @@ class DeckEncoder {
result.push(Card.from(setString, factionString, cardString, i))
}
}
}
})

while (bytes.length > 0) {
const fourPlusCount = VarInt.pop(bytes)
Expand All @@ -66,46 +67,37 @@ class DeckEncoder {
throw new TypeError('The deck provided contains invalid card codes')
}

const grouped3 = this.groupByFactionAndSetSorted(cards.filter(c => c.count === 3))
const grouped2 = this.groupByFactionAndSetSorted(cards.filter(c => c.count === 2))
const grouped1 = this.groupByFactionAndSetSorted(cards.filter(c => c.count === 1))
const nOfs = cards.filter(c => c.count > 3)

const grouped = DeckEncoder.GROUPS.map(i => this.groupByFactionAndSetSorted(cards.filter(c => c.count === i)))
return Base32.encode([
DeckEncoder.FORMAT << 4 | cards.reduce((p, c) => Math.max(p, c.version), 0) & 0xF,
...this.encodeGroup(grouped3),
...this.encodeGroup(grouped2),
...this.encodeGroup(grouped1),
...this.encodeNofs(nOfs)
...grouped.map(group => this.encodeGroup(group)).reduce((prev, curr) => [...prev, ...curr], []),
...this.encodeNofs(cards.filter(c => c.count > DeckEncoder.GROUPS[0]))
])
}

static encodeNofs (nOfs) {
return nOfs
.sort((a, b) => a.code.localeCompare(b.code))
.reduce((result, card) => {
result.push(...VarInt.get(card.count))
result.push(...VarInt.get(card.set))
result.push(...VarInt.get(card.faction.id))
result.push(...VarInt.get(card.id))
return result
}, [])
.reduce((prev, card) => [
...prev,
...VarInt.get(card.count),
...VarInt.get(card.set),
...VarInt.get(card.faction.id),
...VarInt.get(card.id)
], [])
}

static encodeGroup (group) {
return group.reduce((result, list) => {
result.push(...VarInt.get(list.length))

const first = list[0]
result.push(...VarInt.get(first.set))
result.push(...VarInt.get(first.faction.id))

for (const card of list) {
result.push(...VarInt.get(card.id))
}

return result
}, VarInt.get(group.length))
return group.reduce(
(prev, list) => [
...prev,
...VarInt.get(list.length),
...VarInt.get(list[0].set),
...VarInt.get(list[0].faction.id),
...list.map(card => VarInt.get(card.id)).reduce((prev, curr) => [...prev, ...curr], [])
],
VarInt.get(group.length)
)
}

static isValidDeck (cards) {
Expand Down Expand Up @@ -145,5 +137,6 @@ class DeckEncoder {
DeckEncoder.MAX_KNOWN_VERSION = 5
DeckEncoder.FORMAT = 1
DeckEncoder.INITIAL_VERSION = 1
DeckEncoder.GROUPS = [3, 2, 1]

module.exports = DeckEncoder

0 comments on commit 141824d

Please sign in to comment.