import * as React from 'react' ; import { MAX_COUNTER_HEALTH } from './Constants' ; import { TranslationsType } from './reducers/StateTypes' ; export function icon ( name , theme ? , key ? ) { const globalSrc = `/images/icons/${ name }.svg` ; const themeSrc = ( theme ) ? `/themes/${ theme }/images/icon/${ name }.svg` : null ; return < img key = { ( key === null ) ? name : key } className = { 'inline_icon svg ' + name } src = { themeSrc || globalSrc } />; } export function camelCase ( str ) { return str . replace ( /(?:^\w|[A-Z]|\b\w)/g , ( letter , index ) => { return index === 0 ? letter . toLowerCase ( ) : letter . toUpperCase ( ) ; } ) . replace ( /\s+/g , '' ) . replace ( /'/ , '' ) ; } export function romanize ( num ) { if ( + num === 0 ) { return '0' ; } if ( ! + num ) { return '' ; } const digits = String ( + num ) . split ( '' ) ; const key = [ '' , 'C' , 'CC' , 'CCC' , 'CD' , 'D' , 'DC' , 'DCC' , 'DCCC' , 'CM' , '' , 'X' , 'XX' , 'XXX' , 'XL' , 'L' , 'LX' , 'LXX' , 'LXXX' , 'XC' , '' , 'I' , 'II' , 'III' , 'IV' , 'V' , 'VI' , 'VII' , 'VIII' , 'IX' ] ; let roman = '' ; let i = 3 ; while ( i -- ) { roman = ( key [ + ( digits . pop ( ) || 0 ) + ( i * 10 ) ] || '' ) + roman ; } return ( ( num < 0 ) ? '-' : '' ) + Array ( + digits . join ( '' ) + 1 ) . join ( 'M' ) + roman ; } export function horizontalCounter ( count ) { let numbers = [ ] ; const output = [ ] ; let outputted = 0 ; if ( typeof count === 'string' ) { numbers = count . split ( ',' ) ; count = numbers . length ; } else { numbers = [ ... Array ( count + 1 ) . keys ( ) ] ; } while ( count >= 0 ) { output . push ( < span key = { outputted } > { numbers [ outputted ] } < /span>); count -- ; outputted ++ ; } return < span > { output } < /span>; } export function translate ( english , translations ) { if ( translations ) { const translation = translations [ english . toLowerCase ( ) ] ; if ( translation ) { return translation . toString ( ) ; } else { console . error ( 'Failed to locate translation for string: ' + english ) ; } } return english ; } export function translateTier ( tier , english , translations ) { const tierRoman = romanize ( tier ) ; if ( translations ) { const translated = translate ( english , translations ) ; const translatedTier = translate ( 'Tier' , translations ) ; if ( translations . tierwordorder && translations . tierwordorder === 't#w' ) { return translatedTier + ' ' + tierRoman + ' ' + translated ; } else if ( translations . AdjectiveAfterNoun ) { return translated + ' ' + translatedTier + ' ' + tierRoman ; } else { return translatedTier + ' ' + translated + ' ' + tierRoman ; } } return 'Tier ' + tierRoman + ' ' + english ; } export function healthCounter ( health , back = false ) { const digitWidth = [ 0 , 17 , 24 ] ; let maxWidth = 268 ; let outputtedWidth = 0 ; if ( back ) { health = MAX_COUNTER_HEALTH + 1 ; } let output = '<ul class="hp-tracker hp-tracker-vertical-right">' ; let temp = '' ; let outputted = ( back ) ? - 1 : 0 ; while ( health > 0 ) { health -- ; if ( outputted < 9 || ( outputted === 9 && health === 0 ) ) { output += '<li>' + health + '</li>' ; } else if ( outputted === 9 ) { output += '</ul><table class="hp-tracker hp-tracker-horizontal"><tr>' ; temp = '<td>' + health + '</td>' ; outputtedWidth += digitWidth [ health . toString ( ) . length ] ; } else if ( outputtedWidth + digitWidth [ health . toString ( ) . length ] < maxWidth ) { temp = '<td>' + health + '</td>' + temp ; outputtedWidth += digitWidth [ health . toString ( ) . length ] ; } else if ( maxWidth > 0 ) { output += temp + '</tr></table><ul class="hp-tracker hp-tracker-vertical-left">' ; temp = '<li>' + health + '</li>' ; maxWidth = 0 ; } else { temp = '<li>' + health + '</li>' + temp ; } outputted ++ ; } output += temp + '</ul>' ; return < span dangerouslySetInnerHTML = { { __html : output } } > < /span>; } export function lootCounter ( count ) { const digitWidth = [ 0 , 16 , 23 ] ; let maxWidth = 269 ; let outputtedWidth = 0 ; let output = '<ul class="hp-tracker hp-tracker-vertical-right">' ; let temp = '' ; let outputted = 0 ; while ( count > 0 ) { if ( outputted < 15 || ( outputted === 15 && count === 0 ) ) { output += '<li>' + count + '</li>' ; } else if ( outputted === 15 ) { output += '</ul><table class="hp-tracker hp-tracker-horizontal"><tr>' ; temp = '<td>' + count + '</td>' ; outputtedWidth += digitWidth [ count . toString ( ) . length ] ; } else if ( outputtedWidth + digitWidth [ count . toString ( ) . length ] < maxWidth ) { temp = '<td>' + count + '</td>' + temp ; outputtedWidth += digitWidth [ count . toString ( ) . length ] ; } else if ( maxWidth > 0 ) { output += temp + '</tr></table><ul class="hp-tracker hp-tracker-vertical-left">' ; temp = '<li>' + count + '</li>' ; maxWidth = 0 ; } else { temp = '<li>' + count + '</li>' + temp ; } outputted ++ ; count -- ; } output += temp + '</ul>' ; return < span dangerouslySetInnerHTML = { { __html : output } } > < /span>; }