Created
April 23, 2020 03:38
-
-
Save georgestephanis/76b27ac145f6bee4d82f2a8459c3243e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MtgCard { | |
/* raw; | |
quantity; | |
name = ''; | |
set = ''; | |
setNumber = 0; | |
*/ | |
constructor( input = '' ) { | |
this.raw = ''; | |
this.quantity = 0; | |
this.name = ''; | |
this.set = ''; | |
this.setNumber = ''; | |
this.parseImport( input ); | |
} | |
parseImport( input ) { | |
if ( ! input ) { | |
return; | |
} | |
this.raw = input; | |
const matches = input.match( /^(?<quantity>\d+) (?<name>.+) \((?<set>[\da-zA-Z]{3})\) (?<setNumber>\d+)$/ ); | |
if ( matches ) { | |
this.quantity = matches.groups.quantity; | |
this.name = matches.groups.name; | |
this.set = matches.groups.set; | |
this.setNumber = matches.groups.setNumber; | |
} | |
} | |
get arena() { | |
return this.quantity + ' ' + this.name + ' (' + this.set + ') ' + this.setNumber; | |
} | |
} | |
class MtgDeck { | |
/* Companion; | |
Commander; | |
Deck = []; | |
Sideboard = []; | |
*/ | |
constructor( input = false ) { | |
this.Deck = []; | |
this.Sideboard = []; | |
if ( input ) { | |
this.parseImport( input ); | |
} | |
} | |
parseImport( input ) { | |
if ( ! Array.isArray( input ) ) { | |
input = input.split('\n'); | |
} | |
let type = 'Deck'; | |
input.forEach( function( line ) { | |
if ( line ) { | |
// If it starts with a digit, it's a card. Else, it's a type. | |
if ( line.match( /^\d/ ) ) { | |
// Decks and Sideboards can be multiple cards, so add it to the array. | |
if ( 'Deck' === type || 'Sideboard' === type ) { | |
this[ type ].push( new MtgCard( line ) ); | |
} else { | |
this[ type ] = new MtgCard( line ); | |
} | |
} else { | |
type = line; | |
} | |
} | |
}, this ); | |
} | |
get arena() { | |
let deckExport = ''; | |
if ( this.Commander ) { | |
deckExport += 'Commander\n' + this.Commander.arena + '\n\n'; | |
} | |
if ( this.Companion ) { | |
deckExport += 'Companion\n' + this.Companion.arena + '\n\n'; | |
} | |
if ( this.Sideboard.length ) { | |
deckExport += 'Deck\n'; | |
this.Deck.forEach( function( card ) { | |
deckExport += card.arena + '\n'; | |
} ) | |
deckExport += '\n'; | |
} | |
if ( this.Sideboard.length ) { | |
deckExport += 'Sideboard\n'; | |
this.Sideboard.forEach( function( card ) { | |
deckExport += card.arena + '\n'; | |
} ) | |
deckExport += '\n'; | |
} | |
return deckExport; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const deckString = ` | |
Commander | |
1 Daxos, Blessed by the Sun (THB) 9 | |
Companion | |
1 Lurrus of the Dream Den (IKO) 226 | |
Deck | |
1 Stonecoil Serpent (ELD) 235 | |
1 Healer's Hawk (GRN) 14 | |
22 Plains (IKO) 262 | |
1 Ajani's Pridemate (WAR) 4 | |
1 Soulmender (M20) 37 | |
1 Hunted Witness (GRN) 15 | |
1 Charmed Stray (WAR) 8 | |
1 Beloved Princess (ELD) 7 | |
1 Alseid of Life's Bounty (THB) 1 | |
1 Charming Prince (ELD) 8 | |
1 Daybreak Chaplain (M20) 12 | |
1 Gingerbrute (ELD) 219 | |
1 Impassioned Orator (RNA) 12 | |
1 Hushbringer (ELD) 18 | |
1 Grateful Apparition (WAR) 17 | |
1 Drannith Healer (IKO) 10 | |
1 Haazda Marshal (GRN) 13 | |
1 Sworn Companions (GRN) 27 | |
1 Shadowspear (THB) 236 | |
1 The Birth of Meletis (THB) 5 | |
1 Sentinel's Mark (RNA) 20 | |
1 Battlefield Promotion (WAR) 5 | |
1 Dawn of Hope (GRN) 8 | |
1 Desperate Lunge (WAR) 266 | |
1 Moment of Heroism (M20) 30 | |
1 Rally for the Throne (ELD) 25 | |
1 Triumphant Surge (THB) 41 | |
1 Take Heart (GRN) 28 | |
1 Light of Hope (IKO) 20 | |
1 Golden Egg (ELD) 220 | |
1 Shatter the Sky (THB) 37 | |
1 Citywide Bust (GRN) 4 | |
1 Unbreakable Formation (RNA) 29 | |
1 Ravnica at War (WAR) 28 | |
1 Inspired Charge (M20) 24 | |
1 Faerie Guidemother (ELD) 11 | |
1 Solid Footing (IKO) 31 | |
1 Sentinel's Eyes (THB) 36 | |
Sideboard | |
1 Lurrus of the Dream Den (IKO) 226 | |
`.trim(); | |
const deck = new MtgDeck( deckString ); | |
console.log( deck.arena ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment