Skip to content

Commit 70d006d

Browse files
committed
Add support for Bandle City and deck minimum version
1 parent d122c49 commit 70d006d

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "runeterra",
3-
"version": "3.0.1",
3+
"version": "4.0.0",
44
"description": "Legends of Runeterra deck code encoder/decoder",
55
"main": "src/index.js",
66
"types": "index.d.ts",

src/Card.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ module.exports = class Card {
2626
get id () {
2727
return parseInt(this.code.substring(4, 7))
2828
}
29+
30+
get version () {
31+
return Faction.getVersion(this.code.substring(2, 4))
32+
}
2933
}

src/DeckEncoder.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ class DeckEncoder {
1515
}
1616

1717
const firstByte = bytes.shift()
18+
const format = firstByte >> 4
1819
const version = firstByte & 0xF
1920

21+
if (format !== DeckEncoder.FORMAT) {
22+
throw new TypeError('The provided code does not match the required format.')
23+
}
2024
if (version > DeckEncoder.MAX_KNOWN_VERSION) {
2125
throw new TypeError('The provided code requires a higher version of this library; please update.')
2226
}
@@ -65,7 +69,7 @@ class DeckEncoder {
6569

6670
const grouped = DeckEncoder.GROUPS.map(i => this.groupByFactionAndSetSorted(cards.filter(c => c.count === i)))
6771
return Base32.encode([
68-
0x11,
72+
DeckEncoder.FORMAT << 4 | cards.reduce((p, c) => Math.max(p, c.version), 0) & 0xF,
6973
...grouped.map(group => this.encodeGroup(group)).reduce((prev, curr) => [...prev, ...curr], []),
7074
...this.encodeNofs(cards.filter(c => c.count > DeckEncoder.GROUPS[0]))
7175
])
@@ -130,7 +134,9 @@ class DeckEncoder {
130134
}
131135
}
132136

133-
DeckEncoder.MAX_KNOWN_VERSION = 3
137+
DeckEncoder.MAX_KNOWN_VERSION = 4
138+
DeckEncoder.FORMAT = 1
139+
DeckEncoder.INITIAL_VERSION = 1
134140
DeckEncoder.GROUPS = [3, 2, 1]
135141

136142
module.exports = DeckEncoder

src/Faction.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Faction {
55
}
66

77
static fromCode (code) {
8-
const factionId = Faction.FACTIONS[code]
8+
const [factionId] = Faction.FACTIONS[code] || []
99

1010
if (factionId === undefined) {
1111
throw new TypeError('Invalid faction code. It is possible you need to upgrade the runeterra package.')
@@ -15,26 +15,37 @@ class Faction {
1515
}
1616

1717
static fromID (id) {
18-
const [shortCode, factionId] = Object.entries(Faction.FACTIONS).find(([, factionId]) => factionId === id) || []
18+
const [shortCode, factionId] = Object.entries(Faction.FACTIONS).find(([, [factionId]]) => factionId === id) || []
1919

2020
if (factionId === undefined) {
2121
throw new TypeError('Invalid faction id. It is possible you need to upgrade the runeterra package.')
2222
}
2323

2424
return new this(shortCode, factionId)
2525
}
26+
27+
static getVersion (code) {
28+
const [, version] = Faction.FACTIONS[code] || []
29+
30+
if (version === undefined) {
31+
throw new TypeError('Invalid faction code. It is possible you need to upgrade the runeterra package.')
32+
}
33+
34+
return version
35+
}
2636
}
2737

2838
Faction.FACTIONS = {
29-
DE: 0,
30-
FR: 1,
31-
IO: 2,
32-
NX: 3,
33-
PZ: 4,
34-
SI: 5,
35-
BW: 6,
36-
MT: 9,
37-
SH: 7
39+
DE: [0, 1],
40+
FR: [1, 1],
41+
IO: [2, 1],
42+
NX: [3, 1],
43+
PZ: [4, 1],
44+
SI: [5, 1],
45+
BW: [6, 2],
46+
MT: [9, 2],
47+
SH: [7, 3],
48+
BC: [10, 4]
3849
}
3950

4051
module.exports = Faction

test/deckCodesTestData.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,19 @@
428428
]
429429
},
430430
{
431-
"code": "CEAQCBAHBIAQCBAHAMAAIAQAAICQGBQE",
431+
"code": "CMAQCBAHBIAQCBAHAMAAIAQAAICQGBQE",
432432
"cards": [
433433
"3:04SH010",
434434
"2:04SH003",
435435
"4:02DE002",
436436
"5:03BW004"
437437
]
438+
},
439+
{
440+
"code": "CQAQEAIKBIKAAAA",
441+
"cards": [
442+
"3:01BC010",
443+
"3:01BC020"
444+
]
438445
}
439446
]

test/enconding.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ describe('Encoding/Decoding', () => {
7676
assert.deepStrictEqual(deck, decoded)
7777
})
7878

79+
it('should decode decks with bandle city cards', () => {
80+
const deck = [
81+
Card.fromCardString('3:05BC009'),
82+
Card.fromCardString('3:04SH003'),
83+
Card.fromCardString('3:02DE002'),
84+
Card.fromCardString('3:03BW004')
85+
]
86+
const code = DeckEncoder.encode(deck)
87+
const decoded = DeckEncoder.decode(code)
88+
assert.deepStrictEqual(deck, decoded)
89+
})
90+
7991
it('should decode large decks', () => {
8092
const deck = [
8193
Card.fromCardString('3:01DE002'),

0 commit comments

Comments
 (0)