import { Chance } from 'chance' ; import { clamp } from 'lodash' ; import { Singleton , AutoWired } from 'typescript-ioc' ; @ Singleton @ AutoWired export class RNGService { private _chance = new Chance ( ) ; public get chance ( ) { return this . _chance ; } public id ( ) { return this . chance . guid ( ) ; } public numberInRange ( min , max ) { return this . chance . integer ( this . clampMinAtMax ( { min , max } ) ) ; } public clampMinAtMax ( { min , max } ) { if ( min < max ) return { min , max } ; return { min : max , max : max } ; } public pickone < T = any > ( items ) { if ( ! items || items . length === 0 ) return null ; return this . chance . pickone ( items ) ; } public picksome < T = any > ( items , qty ) { if ( items . length === 0 ) return null ; return this . chance . pickset ( items , qty ) ; } public likelihood ( percent = 50 ) { return this . chance . bool ( { likelihood : clamp ( percent , 0 , 100 ) } ) ; } public weighted ( items , weights ) { return this . chance . weighted ( items , weights ) ; } public weightedFromLootastic ( items ) { return this . weighted ( items . map ( x => x . result ) , items . map ( x => x . chance ) ) ; } }