Skip to content

Commit

Permalink
deliver #180 zero pad vi template rename
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnymac committed Jan 16, 2017
1 parent ed72401 commit e64a65d
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions app/screens/Detail/components/Nickname.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ function getNumberInCircle(num) {
return `${num}`
}

// 07 instead of 7, but 10 is still 10
function zeroPad(str, len = 3) {
return `${Array(len - str.length).join('0')}${str}`
}

function templateRename(pokemon, name) {
const totalEnergy = parseInt(Math.floor(100 / pokemon.move_2.energy_cost), 10)
const energy = getNumberInCircle(totalEnergy)
const vi = zeroPad((100 - pokemon.iv).toFixed(0))
const attack = getNumberInCircle(pokemon.attack)
const defense = getNumberInCircle(pokemon.defense)
const stamina = getNumberInCircle(pokemon.stamina)

return name
.replace('[IV]', pokemon.iv.toFixed(0))
.replace('[VI]', vi)
.replace('[ATT]', attack)
.replace('[DEF]', defense)
.replace('[STA]', stamina)
.replace('[FAST]', pokemon.move_1.power.toFixed(0))
.replace('[CHARGE]', pokemon.move_2.power.toFixed(0))
.replace('[ENERGY]', energy)
.replace('[HP]', pokemon.stamina_max)
}

class Nickname extends React.Component {
static propTypes = {
pokemon: PropTypes.object.isRequired,
Expand Down Expand Up @@ -81,25 +106,7 @@ class Nickname extends React.Component {
} = this.props

if (e.key === 'Enter') {
let newName = e.target.value

const totalEnergy = parseInt(Math.floor(100 / pokemon.move_2.energy_cost), 10)
const energy = getNumberInCircle(totalEnergy)
const vi = (100 - pokemon.iv).toFixed(0)
const attack = getNumberInCircle(pokemon.attack)
const defense = getNumberInCircle(pokemon.defense)
const stamina = getNumberInCircle(pokemon.stamina)

newName = newName
.replace('[IV]', pokemon.iv.toFixed(0))
.replace('[VI]', vi)
.replace('[ATT]', attack)
.replace('[DEF]', defense)
.replace('[STA]', stamina)
.replace('[FAST]', pokemon.move_1.power.toFixed(0))
.replace('[CHARGE]', pokemon.move_2.power.toFixed(0))
.replace('[ENERGY]', energy)
.replace('[HP]', pokemon.stamina_max)
const newName = templateRename(pokemon, e.target.value)

if (newName.length > 12) {
ipcRenderer.send('error-message', 'The name must contain 12 characters or less.')
Expand Down

0 comments on commit e64a65d

Please sign in to comment.