Skip to content

Commit

Permalink
Merge pull request #19 from chrysomallos/faction-runeterra
Browse files Browse the repository at this point in the history
add new factions and add version support
  • Loading branch information
Doges authored May 16, 2022
2 parents 60a8af8 + 962d35d commit eb8fbdc
Show file tree
Hide file tree
Showing 8 changed files with 572 additions and 441 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": "3.0.0",
"version": "4.0.0",
"description": "Legends of Runeterra deck code encoder/decoder",
"main": "src/index.js",
"types": "index.d.ts",
Expand Down
4 changes: 4 additions & 0 deletions src/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ module.exports = class Card {
get id () {
return parseInt(this.code.substring(4, 7))
}

get version () {
return Faction.getVersion(this.code.substring(2, 4))
}
}
10 changes: 8 additions & 2 deletions src/DeckEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ class DeckEncoder {
}

const firstByte = bytes.shift()
const format = firstByte >> 4
const version = firstByte & 0xF

if (format !== DeckEncoder.FORMAT) {
throw new TypeError('The provided code does not match the required format.')
}
if (version > DeckEncoder.MAX_KNOWN_VERSION) {
throw new TypeError('The provided code requires a higher version of this library; please update.')
}
Expand Down Expand Up @@ -68,7 +72,7 @@ class DeckEncoder {
const nOfs = cards.filter(c => c.count > 3)

return Base32.encode([
0x11,
DeckEncoder.FORMAT << 4 | cards.reduce((p, c) => Math.max(p, c.version), 0) & 0xF,
...this.encodeGroup(grouped3),
...this.encodeGroup(grouped2),
...this.encodeGroup(grouped1),
Expand Down Expand Up @@ -138,6 +142,8 @@ class DeckEncoder {
}
}

DeckEncoder.MAX_KNOWN_VERSION = 3
DeckEncoder.MAX_KNOWN_VERSION = 5
DeckEncoder.FORMAT = 1
DeckEncoder.INITIAL_VERSION = 1

module.exports = DeckEncoder
34 changes: 23 additions & 11 deletions src/Faction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Faction {
}

static fromCode (code) {
const factionId = Faction.FACTIONS[code]
const [factionId] = Faction.FACTIONS[code] || []

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

static fromID (id) {
const [shortCode, factionId] = Object.entries(Faction.FACTIONS).find(([shortCode, factionId]) => factionId === id) || []
const [shortCode, [factionId]] = Object.entries(Faction.FACTIONS).find(([, [factionId]]) => factionId === id) || [undefined, []]

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

return new this(shortCode, factionId)
}

static getVersion (code) {
const [, version] = Faction.FACTIONS[code] || []

if (version === undefined) {
throw new TypeError('Invalid faction code. It is possible you need to upgrade the runeterra package.')
}

return version
}
}

Faction.FACTIONS = {
DE: 0,
FR: 1,
IO: 2,
NX: 3,
PZ: 4,
SI: 5,
BW: 6,
MT: 9,
SH: 7
DE: [0, 1],
FR: [1, 1],
IO: [2, 1],
NX: [3, 1],
PZ: [4, 1],
SI: [5, 1],
BW: [6, 2],
MT: [9, 2],
SH: [7, 3],
BC: [10, 4],
RU: [12, 5]
}

module.exports = Faction
Loading

0 comments on commit eb8fbdc

Please sign in to comment.