import * as $ from "jquery" ; import * as _ from "lodash" ; import { GetRenderPictures } from "../app" ; export interface IParse { [ index ] : string | ( ( ) => string ) ; } export namespace Text { let buffer = "" ; let toolbars = $ ( "<div></div>" ) ; export function InsertImage ( imgSrc , align = "left" ) { if ( ! GetRenderPictures ( ) ) { return "" ; } return `<img src='${ imgSrc }' align='${ align }' alt='MISSING IMAGE: ${ imgSrc }' style='margin: 1px 8px;'/>` ; } export function Say ( imgSrc , text = "" , align = "left" ) { const textbox = document . getElementById ( "mainTextArea" ) ; if ( GetRenderPictures ( ) ) { textbox . innerHTML += `<img src='${ imgSrc }' align='${ align }' alt='MISSING IMAGE: ${ imgSrc }' style='margin: 1px 8px;'>${ text }</img>` ; } else { textbox . innerHTML += text ; } } export function SetTooltip ( text , parseStrings ? ) { const textbox = document . getElementById ( "tooltipTextArea" ) ; textbox . innerHTML = Text . Parse ( text , parseStrings ) ; } export function Parse ( text , parseStrings ? ) { try { if ( parseStrings ) { let start = text . indexOf ( "[" ) ; let stop = text . indexOf ( "]" , start ) ; while ( start !== - 1 && stop !== - 1 ) { const code = text . slice ( start + 1 , stop ) ; let replaceStr ; if ( parseStrings [ code ] !== undefined ) { replaceStr = parseStrings [ code ] ; if ( _ . isFunction ( replaceStr ) ) { replaceStr = replaceStr ( ) ; } if ( _ . isUndefined ( replaceStr ) ) { replaceStr = ApplyStyle ( `['${ code }' is undefined]` , "error" ) ; } } else { replaceStr = ApplyStyle ( `['${ code }' couldn't be parsed]` , "error" ) ; } text = text . slice ( 0 , start ) + replaceStr + text . slice ( stop + 1 ) ; start = text . indexOf ( "[" , start + replaceStr . length ) ; stop = text . indexOf ( "]" , start ) ; } } return text ; } catch ( e ) { alert ( e . message + "........." + e . stack ) ; return ApplyStyle ( `PARSE ERROR: { ${ text } }` , "error" ) ; } } export function Clear ( ) { const textbox = document . getElementById ( "mainTextArea" ) ; textbox . innerHTML = "" ; textbox . scrollTop = 0 ; buffer = "" ; } export function ApplyStyle ( text , cssClasses ? , tag = "span" ) { const classes = cssClasses ? ` class ="${ cssClasses }">` : ">" ; return `<${ tag }${ classes }${ text }</${ tag }>` ; } export function Add ( text , parse ? , cssClasses ? , tag ? ) { const parsed = Text . Parse ( text , parse ) ; if ( cssClasses ) { buffer += ApplyStyle ( parsed , cssClasses , tag ) ; } else { buffer += parsed ; } } export function Out ( text , cssClasses ? , tag ? ) { text = text . replace ( /“/g , "<i>“" ) . replace ( /”/g , "”</i>" ) ; text = text . replace ( /\n/g , "<br>" ) ; if ( cssClasses ) { buffer += ApplyStyle ( text , cssClasses , tag ) ; } else { buffer += text ; } } export function AddSpan ( text , parse ? , cssClasses ? ) { Text . Add ( text , parse , cssClasses , "span" ) ; } export function AddDiv ( text , parse ? , cssClasses ? ) { Text . Add ( text , parse , cssClasses , "div" ) ; } export function AddToolbar ( list , toolbarLabel ? , cssClasses ? ) { const toolbar = $ ( "<div>" ) ; if ( toolbarLabel ) { const label = $ ( "<span>" , { class : "tbarLbl" , text : toolbarLabel , } ) ; toolbar . append ( label ) ; } for ( const input of list ) { toolbar . append ( createInput ( input , cssClasses ) ) ; } toolbars . append ( toolbar ) ; } export function ResetToolbars ( ) { toolbars = $ ( "<div></div>" ) ; } export function NL ( ) { buffer += "<br/><br/>" ; } export function Flush ( textCssClasses ? , toolbarCssClasses ? ) { const textBox = $ ( "#mainTextArea" ) ; const textClasses = ( textCssClasses ) ? textCssClasses : "" ; const toolbarClasses = ( toolbarCssClasses ) ? toolbarCssClasses : "" ; if ( toolbars ) { textBox . append ( toolbars ) ; } textBox . append ( ApplyStyle ( buffer , textClasses ) ) ; buffer = "" ; toolbars = $ ( "<div></div>" ) ; } function DigitToText ( num ) { num = Math . floor ( num ) ; switch ( num ) { case 0 : return "zero" ; case 1 : return "one" ; case 2 : return "two" ; case 3 : return "three" ; case 4 : return "four" ; case 5 : return "five" ; case 6 : return "six" ; case 7 : return "seven" ; case 8 : return "eight" ; case 9 : return "nine" ; case 10 : return "ten" ; case 11 : return "eleven" ; case 12 : return "twelve" ; case 13 : return "thirteen" ; case 14 : return "fourteen" ; case 15 : return "fifteen" ; case 16 : return "sixteen" ; case 17 : return "seventeen" ; case 18 : return "eighteen" ; case 19 : return "nineteen" ; default : return num . toString ( ) ; } } export function NumToText ( num ) { num = Math . floor ( num ) ; if ( num < 0 ) { return num . toString ( ) ; } else if ( num < 20 ) { return DigitToText ( num ) ; } else if ( num < 1000 ) { const ones = num % 10 ; const tens = Math . floor ( num / 10 ) % 10 ; const hundreds = Math . floor ( num / 100 ) % 10 ; let str = "" ; if ( hundreds !== 0 ) { str += DigitToText ( hundreds ) + " hundred" ; } if ( tens !== 0 ) { if ( hundreds !== 0 ) { str += " " ; } if ( num % 100 < 20 ) { str += DigitToText ( num % 100 ) ; return str ; } else { switch ( tens ) { case 2 : str += "twenty" ; break ; case 3 : str += "thirty" ; break ; case 4 : str += "fourty" ; break ; case 5 : str += "fifty" ; break ; case 6 : str += "sixty" ; break ; case 7 : str += "seventy" ; break ; case 8 : str += "eighty" ; break ; case 9 : str += "ninety" ; break ; } } } if ( ones !== 0 ) { if ( hundreds !== 0 && tens === 0 ) { str += " " ; } else if ( tens !== 0 ) { str += "-" ; } str += DigitToText ( ones ) ; } return str ; } return num . toString ( ) ; } export function Quantify ( num ) { num = Math . floor ( num ) ; if ( num < 0 ) { return num . toString ( ) ; } switch ( num ) { case 0 : return "lack" ; case 1 : return _ . sample ( [ "lone" , "solitary" , "individual" , "single" ] ) ; case 2 : return _ . sample ( [ "duo" , "pair" ] ) ; case 3 : return _ . sample ( [ "trio" , "triad" ] ) ; case 4 : return _ . sample ( [ "quad" , "quartette" ] ) ; case 5 : return "quintet" ; case 6 : return "sextet" ; case 7 : return "septet" ; case 8 : return "octet" ; case 9 : return "nonet" ; default : return "brace" ; } } export function Ordinal ( num , capital ? ) { num = Math . floor ( num ) ; switch ( num ) { case 1 : return capital ? "First" : "first" ; case 2 : return capital ? "Second" : "second" ; case 3 : return capital ? "Third" : "third" ; case 4 : return capital ? "Fourth" : "fourth" ; case 5 : return capital ? "Fifth" : "fifth" ; case 6 : return capital ? "Sixth" : "sixth" ; case 7 : return capital ? "Seventh" : "seventh" ; case 8 : return capital ? "Eight" : "eight" ; case 9 : return capital ? "Ninth" : "ninth" ; default : return num + "th" ; } } export function ParserPlural ( parse = { } , condition ? , prefix = "" , postfix = "" ) { parse [ `${ prefix }a${ postfix }` ] = condition ? "" : " a" ; parse [ `${ prefix }s${ postfix }` ] = condition ? "s" : "" ; parse [ `${ prefix }notS${ postfix }` ] = condition ? "" : "s" ; parse [ `${ prefix }es${ postfix }` ] = condition ? "es" : "" ; parse [ `${ prefix }notEs${ postfix }` ] = condition ? "" : "es" ; parse [ `${ prefix }yIes${ postfix }` ] = condition ? "ies" : "y" ; parse [ `${ prefix }isAre${ postfix }` ] = condition ? "are" : "is" ; parse [ `${ prefix }hasHave${ postfix }` ] = condition ? "have" : "has" ; parse [ `${ prefix }wasWere${ postfix }` ] = condition ? "were" : "was" ; parse [ `${ prefix }oneof${ postfix }` ] = condition ? " one of" : "" ; parse [ `${ prefix }someof${ postfix }` ] = condition ? " some of" : "" ; parse [ `${ prefix }eachof${ postfix }` ] = condition ? " each of" : "" ; parse [ `${ prefix }allof${ postfix }` ] = condition ? " all of" : "" ; parse [ `${ prefix }ItThey${ postfix }` ] = condition ? "They" : "It" ; parse [ `${ prefix }ItsTheyre${ postfix }` ] = condition ? "They’re" : "It’s" ; parse [ `${ prefix }itThey${ postfix }` ] = condition ? "they" : "it" ; parse [ `${ prefix }itThem${ postfix }` ] = condition ? "them" : "it" ; parse [ `${ prefix }itsTheir${ postfix }` ] = condition ? "their" : "its" ; parse [ `${ prefix }itsTheyre${ postfix }` ] = condition ? "they’re" : "it’s" ; parse [ `${ prefix }itsTheyve${ postfix }` ] = condition ? "they’ve" : "it’s" ; parse [ `${ prefix }thisThese${ postfix }` ] = condition ? "these" : "this" ; parse [ `${ prefix }thatThose${ postfix }` ] = condition ? "those" : "that" ; return parse ; } export function Enumerate ( list , conjunction ) { let output = "" ; list . reverse ( ) ; list . forEach ( ( elem , idx ) => { if ( idx === 0 ) { output = elem + output ; } else if ( idx === 1 ) { output = elem + " " + conjunction + " " + output ; } else { output = elem + ", " + output ; } } ) ; return output ; } function createInput ( inputOptions , cssClasses ? ) { let input ; const type = inputOptions . type || "button" ; const classesStr = ( cssClasses || "" ) + " " + ( inputOptions . classes || "" ) ; if ( type . toLowerCase ( ) === "button" ) { const btnName = inputOptions . nameStr ; const onclick = inputOptions . func ; const clickParam = inputOptions . param ; input = $ ( "<input />" , { type : "button" , class : "tbarInput " + classesStr , value : btnName , on : { click ( ) { const data = $ ( this ) . data ( ) ; const func = data . func ; func ( data . param ) ; } , } , } ) ; $ ( input ) . data ( "param" , inputOptions . obj ) ; $ ( input ) . data ( "func" , onclick ) ; } else if ( type . toLowerCase ( ) === "select" ) { } else if ( type . toLowerCase ( ) === "checkbox" ) { } else if ( type . toLowerCase ( ) === "rado" ) { } return input ; } export function Bold ( text ) { return ApplyStyle ( text , "bold" ) ; } export function Damage ( text ) { return ApplyStyle ( text , "life bold" ) ; } export function Heal ( text ) { return ApplyStyle ( text , "heal bold" ) ; } export function Lust ( text ) { return ApplyStyle ( text , "pink bold" ) ; } export function Soothe ( text ) { return ApplyStyle ( text , "soothe bold" ) ; } export function Mana ( text ) { return ApplyStyle ( text , "mana bold" ) ; } }