Skip to content

Commit eb8fbdc

Browse files
authored
Merge pull request #19 from chrysomallos/faction-runeterra
add new factions and add version support
2 parents 60a8af8 + 962d35d commit eb8fbdc

File tree

8 files changed

+572
-441
lines changed

8 files changed

+572
-441
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.0",
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
}
@@ -68,7 +72,7 @@ class DeckEncoder {
6872
const nOfs = cards.filter(c => c.count > 3)
6973

7074
return Base32.encode([
71-
0x11,
75+
DeckEncoder.FORMAT << 4 | cards.reduce((p, c) => Math.max(p, c.version), 0) & 0xF,
7276
...this.encodeGroup(grouped3),
7377
...this.encodeGroup(grouped2),
7478
...this.encodeGroup(grouped1),
@@ -138,6 +142,8 @@ class DeckEncoder {
138142
}
139143
}
140144

141-
DeckEncoder.MAX_KNOWN_VERSION = 3
145+
DeckEncoder.MAX_KNOWN_VERSION = 5
146+
DeckEncoder.FORMAT = 1
147+
DeckEncoder.INITIAL_VERSION = 1
142148

143149
module.exports = DeckEncoder

src/Faction.js

Lines changed: 23 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,38 @@ class Faction {
1515
}
1616

1717
static fromID (id) {
18-
const [shortCode, factionId] = Object.entries(Faction.FACTIONS).find(([shortCode, factionId]) => factionId === id) || []
18+
const [shortCode, [factionId]] = Object.entries(Faction.FACTIONS).find(([, [factionId]]) => factionId === id) || [undefined, []]
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],
49+
RU: [12, 5]
3850
}
3951

4052
module.exports = Faction

0 commit comments

Comments
 (0)