Skip to content

chore/refactor: improvements for code base #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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