Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Created April 6, 2023 05:56
Show Gist options
  • Save KevinGutowski/42817b4d178808f963b22d2c427e0218 to your computer and use it in GitHub Desktop.
Save KevinGutowski/42817b4d178808f963b22d2c427e0218 to your computer and use it in GitHub Desktop.
Parse Pokémon Trading Card Online Deckstring
function importDeckString(deckString) {
var lines = deckString.trim().split('\n');
lines = lines.filter(item => item !== "");
lines.pop()
const cards = [];
var numOfPokemon = 0
var numOfTrainers = 0
var numOfEnergy = 0
lines.forEach((line,index)=>{
if (line.startsWith('Pokémon:')) {
numOfPokemon = parseInt(line.split(' ')[1])
} else if (line.startsWith('Trainer:')) {
numOfTrainers = parseInt(line.split(' ')[1])
} else if (line.startsWith('Energy:')) {
numOfEnergy = parseInt(line.split(' ')[1])
} else {
let splitLine = line.split(' ')
let cardCount = parseInt(splitLine[0])
var cardNumber;
var setCode;
var name;
var type;
var variant = ''
if (parseInt(splitLine[splitLine.length - 1])) {
cardNumber = parseInt(splitLine[splitLine.length - 1])
setCode = splitLine[splitLine.length - 2]
name = splitLine.slice(1,splitLine.length - 2).join(' ')
} else {
variant = splitLine[splitLine.length - 1]
cardNumber = parseInt(splitLine[splitLine.length - 2])
setCode = splitLine[splitLine.length - 3]
name = splitLine.slice(1,splitLine.length - 3).join(' ')
}
name = replaceEnergyBrackets(name)
if (index <= numOfPokemon) {
type='Pokémon'
} else if ((index > numOfPokemon) && (index <= numOfPokemon + numOfTrainers + 1)) {
type='Trainer'
} else {
type='Energy'
if (name.includes('Basic ')) {
name = name.replace('Basic ','')
}
}
cards.push({
'count':cardCount,
'name': name,
'type': type,
'set_code': setCode,
'number': cardNumber,
'variant': variant
})
}
})
updateTable(cards)
}
function replaceEnergyBrackets(string) {
string = string
.replace('{G}','Grass')
.replace('{R}','Fire')
.replace('{F}','Fighting')
.replace('{W}','Water')
.replace('{L}','Lightning')
.replace('{P}','Psychic')
.replace('{F}','Fighting')
.replace('{D}','Darkness')
.replace('{M}','Metal')
.replace('{Y}','Fairy')
.replace('{C}','Colorless')
.replace('{D}','Dragon')
return string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment