import { ActionCreator , Action , Dispatch } from 'redux' ; import { ThunkAction } from 'redux-thunk' ; import * as RunActions from './RunActions' ; import ChatMode from '../../model/enums/ChatMode' ; import State from '../State' ; import DataContext from '../../model/DataContext' ; import localization from '../../model/resources/localization' ; import actionCreators from '../actionCreators' ; import ChatMessage from '../../model/ChatMessage' ; import Account from '../../model/Account' ; import Persons from '../../model/Persons' ; import PersonInfo from '../../model/PersonInfo' ; import PlayerInfo from '../../model/PlayerInfo' ; import Role from '../../model/enums/Role' ; import PlayerStates from '../../model/enums/PlayerStates' ; import tableActionCreators from '../table/tableActionCreators' ; import StakeTypes from '../../model/enums/StakeTypes' ; import MainView from '../../model/enums/MainView' ; import Constants from '../../model/enums/Constants' ; import getErrorMessage from '../../utils/ErrorHelpers' ; let timerRef = null ; const runChatModeChanged = ( chatMode ) => ( { type : RunActions . RunActionTypes . RunChatModeChanged , chatMode } ) ; const runChatMessageChanged = ( message ) => ( { type : RunActions . RunActionTypes . RunChatMessageChanged , message } ) ; const runChatMessageSend = ( ) => ( dispatch , getState , dataContext ) => { const state = getState ( ) ; const text = state . run . chat . message ; if ( text . length > 0 ) { dataContext . gameClient . sayAsync ( text ) ; } dispatch ( runChatMessageChanged ( '' ) ) ; dispatch ( chatMessageAdded ( { sender : state . user . login , text } ) ) ; if ( ! state . run . chat . isVisible ) { dispatch ( activateChat ( ) ) ; } } ; const markQuestion = ( ) => ( dispatch , getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'MARK' ) ; } ; const pause = ( ) => ( dispatch , getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'PAUSE' , getState ( ) . run . stage . isGamePaused ? '-' : '+' ) ; } ; const runShowPersons = ( ) => ( { type : RunActions . RunActionTypes . RunShowPersons } ) ; const runHidePersons = ( ) => ( { type : RunActions . RunActionTypes . RunHidePersons } ) ; const runShowTables = ( ) => ( { type : RunActions . RunActionTypes . RunShowTables } ) ; const runHideTables = ( ) => ( { type : RunActions . RunActionTypes . RunHideTables } ) ; const runChatVisibilityChanged = ( isOpen ) => ( { type : RunActions . RunActionTypes . RunChatVisibilityChanged , isOpen } ) ; const playerSelected = ( playerIndex ) => async ( dispatch , getState , dataContext ) => { dataContext . gameClient . msgAsync ( getState ( ) . run . selection . message , playerIndex ) ; dispatch ( clearDecisions ( ) ) ; } ; const exitGame = ( ) => async ( dispatch , getState , dataContext ) => { try { await dataContext . gameClient . leaveGameAsync ( ) ; } catch ( e ) { alert ( localization . exitError ) ; } if ( timerRef ) { window . clearTimeout ( timerRef ) ; timerRef = null ; } if ( getState ( ) . ui . previousMainView === MainView . Lobby ) { actionCreators . navigateToLobby ( - 1 ) ( dispatch , getState , dataContext ) ; } else { dispatch ( actionCreators . navigateToWelcome ( ) as any ) ; } } ; const chatMessageAdded = ( chatMessage ) => ( { type : RunActions . RunActionTypes . ChatMessageAdded , chatMessage } ) ; const lastReplicChanged = ( chatMessage ) => ( { type : RunActions . RunActionTypes . LastReplicChanged , chatMessage } ) ; const activateChat = ( ) => ( { type : RunActions . RunActionTypes . ActivateChat } ) ; const showmanReplicChanged = ( replic ) => ( { type : RunActions . RunActionTypes . ShowmanReplicChanged , replic } ) ; const playerReplicChanged = ( playerIndex , replic ) => ( { type : RunActions . RunActionTypes . PlayerReplicChanged , playerIndex , replic } ) ; const infoChanged = ( all , showman , players ) => ( { type : RunActions . RunActionTypes . InfoChanged , all , showman , players } ) ; const chatPersonSelected = ( personName ) => ( { type : RunActions . RunActionTypes . ChatPersonSelected , personName } ) ; const tableSelected = ( tableIndex ) => ( { type : RunActions . RunActionTypes . TableSelected , tableIndex } ) ; const operationError = ( error ) => ( { type : RunActions . RunActionTypes . OperationError , error } ) ; const addTable = ( ) => async ( dispatch , getState , dataContext ) => { if ( getState ( ) . run . persons . players . length >= Constants . MAX_PLAYERS_COUNT ) { return ; } try { dataContext . gameClient . msgAsync ( 'CONFIG' , 'ADDTABLE' ) ; } catch ( e ) { dispatch ( operationError ( getErrorMessage ( e ) ) ) ; } } ; const deleteTable = ( ) => async ( dispatch , getState , dataContext ) => { const tableIndex = getState ( ) . run . selectedTableIndex - 1 ; if ( tableIndex <= 0 || tableIndex >= getState ( ) . run . persons . players . length ) { return ; } try { dataContext . gameClient . msgAsync ( 'CONFIG' , 'DELETETABLE' , tableIndex ) ; } catch ( e ) { dispatch ( operationError ( getErrorMessage ( e ) ) ) ; } } ; const kickPerson = ( ) => async ( dispatch , getState , dataContext ) => { const personName = getState ( ) . run . chat . selectedPersonName ; if ( ! personName ) { return ; } try { dataContext . gameClient . msgAsync ( 'KICK' , personName ) ; } catch ( e ) { dispatch ( operationError ( getErrorMessage ( e ) ) ) ; } } ; const banPerson = ( ) => async ( dispatch , getState , dataContext ) => { const personName = getState ( ) . run . chat . selectedPersonName ; if ( ! personName ) { return ; } try { dataContext . gameClient . msgAsync ( 'BAN' , personName ) ; } catch ( e ) { dispatch ( operationError ( getErrorMessage ( e ) ) ) ; } } ; const personAvatarChanged = ( personName , avatarUri ) => ( { type : RunActions . RunActionTypes . PersonAvatarChanged , personName , avatarUri } ) ; const gameStarted = ( ) => ( { type : RunActions . RunActionTypes . GameStarted } ) ; const stageChanged = ( stageName ) => ( { type : RunActions . RunActionTypes . StageChanged , stageName } ) ; const playersStateCleared = ( ) => ( { type : RunActions . RunActionTypes . PlayersStateCleared } ) ; const gameStateCleared = ( ) => ( { type : RunActions . RunActionTypes . GameStateCleared } ) ; const sumsChanged = ( sums ) => ( { type : RunActions . RunActionTypes . SumsChanged , sums } ) ; const afterQuestionStateChanged = ( isAfterQuestion ) => ( { type : RunActions . RunActionTypes . AfterQuestionStateChanged , isAfterQuestion } ) ; const currentPriceChanged = ( currentPrice ) => ( { type : RunActions . RunActionTypes . CurrentPriceChanged , currentPrice } ) ; const personAdded = ( person ) => ( { type : RunActions . RunActionTypes . PersonAdded , person } ) ; const personRemoved = ( name ) => ( { type : RunActions . RunActionTypes . PersonRemoved , name } ) ; const showmanChanged = ( name ) => ( { type : RunActions . RunActionTypes . ShowmanChanged , name } ) ; const playerAdded = ( ) => ( { type : RunActions . RunActionTypes . PlayerAdded } ) ; const playerChanged = ( index , name ) => ( { type : RunActions . RunActionTypes . PlayerChanged , index , name } ) ; const playerDeleted = ( index ) => ( { type : RunActions . RunActionTypes . PlayerDeleted , index } ) ; const playersSwap = ( index1 , index2 ) => ( { type : RunActions . RunActionTypes . PlayersSwap , index1 , index2 } ) ; const roleChanged = ( role ) => ( { type : RunActions . RunActionTypes . RoleChanged , role } ) ; const playerStateChanged = ( index , state ) => ( { type : RunActions . RunActionTypes . PlayerStateChanged , index , state } ) ; const playerLostStateDropped = ( index ) => ( { type : RunActions . RunActionTypes . PlayerLostStateDropped , index } ) ; const isPausedChanged = ( isPaused ) => ( { type : RunActions . RunActionTypes . IsPausedChanged , isPaused } ) ; const playerStakeChanged = ( index , stake ) => ( { type : RunActions . RunActionTypes . PlayerStakeChanged , index , stake } ) ; const decisionNeededChanged = ( decisionNeeded ) => ( { type : RunActions . RunActionTypes . DecisionNeededChanged , decisionNeeded } ) ; const clearDecisions = ( ) => ( { type : RunActions . RunActionTypes . ClearDecisions } ) ; const selectQuestion = ( themeIndex , questionIndex ) => async ( dispatch , getState , dataContext ) => { if ( ! getState ( ) . run . table . isSelectable ) { return ; } const theme = getState ( ) . run . table . roundInfo [ themeIndex ] ; if ( theme ) { const question = theme . questions [ questionIndex ] ; if ( question > - 1 ) { dataContext . gameClient . msgAsync ( 'CHOICE' , themeIndex , questionIndex ) ; dispatch ( tableActionCreators . isSelectableChanged ( false ) ) ; dispatch ( decisionNeededChanged ( false ) ) ; } } } ; const selectTheme = ( themeIndex ) => async ( dispatch , getState , dataContext ) => { if ( ! getState ( ) . run . table . isSelectable ) { return ; } const theme = getState ( ) . run . table . roundInfo [ themeIndex ] ; if ( theme ) { dataContext . gameClient . msgAsync ( 'DELETE' , themeIndex ) ; dispatch ( tableActionCreators . isSelectableChanged ( false ) ) ; dispatch ( decisionNeededChanged ( false ) ) ; } } ; const isGameButtonEnabledChanged = ( isGameButtonEnabled ) => ( { type : RunActions . RunActionTypes . IsGameButtonEnabledChanged , isGameButtonEnabled } ) ; const pressGameButton = ( ) => async ( dispatch , _getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'I' ) ; dispatch ( isGameButtonEnabledChanged ( false ) ) ; setTimeout ( ( ) => { dispatch ( isGameButtonEnabledChanged ( true ) ) ; } , 3000 ) ; } ; const apellate = ( ) => async ( _dispatch , _getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'APELLATE' , '+' ) ; } ; const isAnswering = ( ) => ( { type : RunActions . RunActionTypes . IsAnswering } ) ; const onAnswerChanged = ( answer ) => ( { type : RunActions . RunActionTypes . AnswerChanged , answer } ) ; const sendAnswer = ( ) => async ( dispatch , getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'ANSWER' , getState ( ) . run . answer ) ; dispatch ( clearDecisions ( ) ) ; } ; const validate = ( name , answer , rightAnswers , wrongAnswers , header , message ) => ( { type : RunActions . RunActionTypes . Validate , name , answer , rightAnswers , wrongAnswers , header , message } ) ; const approveAnswer = ( ) => async ( dispatch , getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'ISRIGHT' , '+' ) ; dispatch ( clearDecisions ( ) ) ; } ; const rejectAnswer = ( ) => async ( dispatch , getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'ISRIGHT' , '-' ) ; dispatch ( clearDecisions ( ) ) ; } ; const setStakes = ( allowedStakeTypes , minimum , maximum , stake , step , message , areSimple ) => ( { type : RunActions . RunActionTypes . SetStakes , allowedStakeTypes , minimum , maximum , stake , step , message , areSimple } ) ; const stakeChanged = ( stake ) => ( { type : RunActions . RunActionTypes . StakeChanged , stake } ) ; const sendNominal = ( ) => async ( dispatch , getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'STAKE' , 0 ) ; dispatch ( clearDecisions ( ) ) ; } ; const sendStake = ( ) => async ( dispatch , getState , dataContext ) => { const state = getState ( ) ; if ( state . run . stakes . message === 'STAKE' ) { dataContext . gameClient . msgAsync ( 'STAKE' , 1 , state . run . stakes . stake ) ; } else { dataContext . gameClient . msgAsync ( state . run . stakes . message , state . run . stakes . stake ) ; } dispatch ( clearDecisions ( ) ) ; } ; const sendPass = ( ) => async ( dispatch , _getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'STAKE' , 2 ) ; dispatch ( clearDecisions ( ) ) ; } ; const sendAllIn = ( ) => async ( dispatch , _getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'STAKE' , 3 ) ; dispatch ( clearDecisions ( ) ) ; } ; const selectionEnabled = ( allowedIndices , message ) => ( { type : RunActions . RunActionTypes . SelectionEnabled , allowedIndices , message } ) ; const showLeftSeconds = ( leftSeconds , dispatch ) : void => { let leftSecondsString = ( leftSeconds % 60 ) . toString ( ) ; if ( leftSecondsString . length < 2 ) { leftSecondsString = `0${ leftSeconds }` ; } dispatch ( showmanReplicChanged ( `${ localization . theGameWillStartIn } 00:00:${ leftSecondsString } ${ localization . orByFilling }` ) ) ; if ( leftSeconds > 0 ) { timerRef = window . setTimeout ( ( ) => { showLeftSeconds ( leftSeconds - 1 , dispatch ) ; } , 1000 ) ; } } ; const onMediaEnded = ( ) => async ( _dispatch , _getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'ATOM' ) ; } ; const areSumsEditableChanged = ( areSumsEditable ) => ( { type : RunActions . RunActionTypes . AreSumsEditableChanged , areSumsEditable } ) ; const changePlayerSum = ( playerIndex , sum ) => async ( dispatch , getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'CHANGE' , playerIndex + 1 , sum ) ; } ; const readingSpeedChanged = ( readingSpeed ) => ( { type : RunActions . RunActionTypes . ReadingSpeedChanged , readingSpeed } ) ; const runTimer = ( timerIndex , maximumTime , runByUser ) => ( { type : RunActions . RunActionTypes . RunTimer , timerIndex , maximumTime , runByUser } ) ; const pauseTimer = ( timerIndex , currentTime , pausedByUser ) => ( { type : RunActions . RunActionTypes . PauseTimer , timerIndex , currentTime , pausedByUser } ) ; const resumeTimer = ( timerIndex , runByUser ) => ( { type : RunActions . RunActionTypes . ResumeTimer , timerIndex , runByUser } ) ; const stopTimer = ( timerIndex ) => ( { type : RunActions . RunActionTypes . StopTimer , timerIndex } ) ; const timerMaximumChanged = ( timerIndex , maximumTime ) => ( { type : RunActions . RunActionTypes . TimerMaximumChanged , timerIndex , maximumTime } ) ; const activateShowmanDecision = ( ) => ( { type : RunActions . RunActionTypes . ActivateShowmanDecision } ) ; const activatePlayerDecision = ( playerIndex ) => ( { type : RunActions . RunActionTypes . ActivatePlayerDecision , playerIndex } ) ; const showMainTimer = ( ) => ( { type : RunActions . RunActionTypes . ShowMainTimer } ) ; const clearDecisionsAndMainTimer = ( ) => ( { type : RunActions . RunActionTypes . ClearDecisionsAndMainTimer } ) ; const hintChanged = ( hint ) => ( { type : RunActions . RunActionTypes . HintChanged , hint } ) ; const startGame = ( ) => ( _dispatch , _getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'START' ) ; } ; const ready = ( isReady ) => ( _dispatch , _getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'READY' , isReady ? '+' : '-' ) ; } ; const hostNameChanged = ( hostName ) => ( { type : RunActions . RunActionTypes . HostNameChanged , hostName } ) ; const themeNameChanged = ( themeName ) => ( { type : RunActions . RunActionTypes . ThemeNameChanged , themeName } ) ; const updateCaption = ( questionCaption ) => ( dispatch , getState ) => { const { themeName } = getState ( ) . run . stage ; dispatch ( tableActionCreators . captionChanged ( `${ themeName }, ${ questionCaption }` ) ) ; } ; const moveNext = ( ) => async ( _dispatch , _getState , dataContext ) => { dataContext . gameClient . msgAsync ( 'MOVE' , '1' ) ; } ; const isReadyChanged = ( personIndex , isReady ) => ( { type : RunActions . RunActionTypes . IsReadyChanged , personIndex , isReady } ) ; const runActionCreators = { runChatModeChanged , runChatMessageChanged , runChatMessageSend , markQuestion , pause , runShowPersons , runHidePersons , runShowTables , runHideTables , runChatVisibilityChanged , playerSelected , exitGame , chatMessageAdded , lastReplicChanged , activateChat , showmanReplicChanged , playerReplicChanged , infoChanged , chatPersonSelected , tableSelected , addTable , deleteTable , kickPerson , banPerson , personAvatarChanged , gameStarted , stageChanged , playersStateCleared , gameStateCleared , sumsChanged , afterQuestionStateChanged , currentPriceChanged , personAdded , personRemoved , showmanChanged , playerAdded , playerChanged , playerDeleted , playersSwap , roleChanged , playerStateChanged , playerLostStateDropped , isPausedChanged , playerStakeChanged , decisionNeededChanged , clearDecisions , selectQuestion , selectTheme , pressGameButton , apellate , isAnswering , onAnswerChanged , sendAnswer , validate , approveAnswer , rejectAnswer , setStakes , stakeChanged , sendNominal , sendStake , sendPass , sendAllIn , selectionEnabled , showLeftSeconds , onMediaEnded , areSumsEditableChanged , changePlayerSum , readingSpeedChanged , runTimer , pauseTimer , resumeTimer , stopTimer , timerMaximumChanged , activateShowmanDecision , activatePlayerDecision , showMainTimer , clearDecisionsAndMainTimer , hintChanged , startGame , operationError , hostNameChanged , themeNameChanged , updateCaption , moveNext , isReadyChanged , ready } ; export default runActionCreators ;