import { getContentSets , numAdventurers , numAliveAdventurers , numPlayers } from 'app/actions/Settings' ; import { CombatPhase } from 'app/Constants' ; import { logEvent } from 'app/Logging' ; import { AppStateWithHistory , SettingsType } from 'app/reducers/StateTypes' ; import { connect } from 'react-redux' ; import Redux from 'redux' ; import { ParserNode } from '../TemplateTypes' ; import { adventurerDelta , handleCombatEnd , setupCombatDecision , tierSumDelta , toCombatPhase , } from './Actions' ; import PlayerTier , { DispatchProps , StateProps } from './PlayerTier' ; import { mapStateToProps as mapStateToPropsBase } from './Types' ; const mapStateToProps = ( state , ownProps ) : StateProps => { let maxTier = 0 ; let histIdx = state . _history . length - 1 ; while ( Boolean ( state . _history [ histIdx ] ) && state . _history [ histIdx ] . quest && state . _history [ histIdx ] . quest . node && state . _history [ histIdx ] . quest . node . ctx . templates . combat && histIdx > 0 ) { const combatContext = state . _history [ histIdx ] . quest . node . ctx . templates . combat ; if ( ! combatContext ) { break ; } const tier = combatContext . tier ; histIdx -- ; const phase = state . _history [ histIdx ] . quest . node . ctx . templates . combat . phase ; if ( tier && phase === CombatPhase . prepare ) { maxTier = Math . max ( maxTier , tier ) ; } } const node = ownProps . node ; if ( ! node ) { throw Error ( 'Incomplete props given' ) ; } const stateCombat = state . quest . node . ctx . templates . combat ; return { ... mapStateToPropsBase ( state , ownProps ) , node : state . quest . node , adventurers : numAdventurers ( state . settings , state . multiplayer ) , combat : node . ctx . templates . combat , maxTier , numAliveAdventurers : numAliveAdventurers ( state . settings , state . quest . node , state . multiplayer ) , localAliveAdventurers : stateCombat . numAliveAdventurers , tier : stateCombat . tier , contentSets : getContentSets ( state . settings , state . multiplayer ) , } ; } ; const mapDispatchToProps = ( dispatch ) : DispatchProps => { return { onAdventurerDelta : ( node , settings , current , delta ) => { dispatch ( adventurerDelta ( { node , settings , current , delta } ) ) ; } , onDecisionSetup : ( node , seed ) => { dispatch ( setupCombatDecision ( { node , seed } ) ) ; } , onDefeat : ( node , settings , maxTier , seed ) => { logEvent ( 'combat' , 'combat_defeat' , { difficulty : settings . difficulty , label : numPlayers ( settings ) , maxTier , players : numPlayers ( settings ) , value : maxTier , } ) ; dispatch ( handleCombatEnd ( { node , settings , victory : false , maxTier , seed } ) ) ; } , onNext : ( node , phase ) => { dispatch ( toCombatPhase ( { node , phase } ) ) ; } , onTierSumDelta : ( node , current , delta ) => { dispatch ( tierSumDelta ( { node , current , delta } ) ) ; } , onVictory : ( node , settings , maxTier , seed ) => { logEvent ( 'combat' , 'combat_victory' , { difficulty : settings . difficulty , label : numPlayers ( settings ) , maxTier , players : numPlayers ( settings ) , value : maxTier , } ) ; dispatch ( handleCombatEnd ( { node , settings , victory : true , maxTier , seed } ) ) ; } , } ; } ; export default connect ( mapStateToProps , mapDispatchToProps ) ( PlayerTier ) ;