import { loadNode } from 'app/actions/Quest' ; import { changeSettings } from 'app/actions/Settings' ; import { defaultContext } from 'app/components/views/quest/cardtemplates/Template' ; import { ParserNode , TemplateContext } from 'app/components/views/quest/cardtemplates/TemplateTypes' ; import Redux from 'redux' ; import { renderXML } from 'shared/render/QDLParser' ; import { Quest } from 'shared/schema/Quests' ; import { PanelType , PlaytestSettings , QuestType } from '../reducers/StateTypes' ; import { store } from '../Store' ; import { SetDirtyAction , SetDirtyTimeoutAction , SetFatalAction , SetLineAction , SetWordCountAction } from './ActionTypes' ; import { pushError } from './Dialogs' ; import { saveQuest } from './Quest' ; declare var window ; export function setFatal ( error ) { return { type : 'SET_FATAL' , error } ; } export function setDirty ( isDirty ) { return { type : 'SET_DIRTY' , isDirty } ; } export function setDirtyTimeout ( timer ) { return { type : 'SET_DIRTY_TIMEOUT' , timer } ; } export function setLine ( line ) { return { type : 'SET_LINE' , line } ; } export function setOpInit ( mathjs ) { return { type : 'SET_OP_INIT' , mathjs } ; } export function setWordCount ( count ) { return { type : 'SET_WORD_COUNT' , count } ; } export function panelToggle ( panel ) { return { type : 'PANEL_TOGGLE' , panel } ; } export function lineNumbersToggle ( ) { return { type : 'LINE_NUMBERS_TOGGLE' } ; } export function updateDirtyState ( ) { return ( dispatch ) : any => { const editor = store . getState ( ) ; if ( ! editor . dirty ) { dispatch ( setDirty ( true ) ) ; } if ( editor . dirtyTimeout ) { clearTimeout ( editor . dirtyTimeout ) ; } const timer = setTimeout ( ( ) => { const state = store . getState ( ) ; if ( state . editor . dirtyTimeout !== timer ) { return ; } if ( state . editor . dirty ) { dispatch ( saveQuest ( state . quest ) ) ; } dispatch ( setDirtyTimeout ( null ) ) ; } , 2000 ) ; dispatch ( setDirtyTimeout ( timer ) ) ; } ; } export function getPlayNode ( node ) { let tag = node . get ( 0 ) . tagName ; if ( tag === 'quest' ) { node = node . children ( ) . first ( ) ; tag = node . get ( 0 ) . tagName ; } if ( tag !== 'roleplay' && tag !== 'combat' && tag !== 'decision' ) { return null ; } return node ; } export function startPlaytestWorker ( oldWorker , elem , settings ) { return ( dispatch ) : any => { if ( oldWorker ) { oldWorker . terminate ( ) ; } if ( ! window . Worker ) { console . log ( 'Web worker not available, skipping playtest.' ) ; return ; } const worker = new Worker ( 'playtest.js' ) ; worker . onerror = ( ev ) => { dispatch ( { type : 'PLAYTEST_ERROR' , msg : ev . error } ) ; worker . terminate ( ) ; } ; worker . onmessage = ( e ) => { if ( e . data . status === 'COMPLETE' ) { dispatch ( { type : 'PLAYTEST_COMPLETE' } ) ; } else { dispatch ( { type : 'PLAYTEST_MESSAGE' , msgs : e . data } ) ; } } ; worker . postMessage ( { type : 'RUN' , xml : elem . toString ( ) , settings } ) ; dispatch ( { type : 'PLAYTEST_INIT' , worker } ) ; } ; } export function renderAndPlay ( quest , qdl , line , oldWorker , ctx = defaultContext ( ) ) { return ( dispatch ) : any => { setTimeout ( ( ) => { const xmlResult = renderXML ( qdl ) ; dispatch ( { type : 'QUEST_RENDER' , qdl : xmlResult , msgs : xmlResult . getFinalizedLogs ( ) } ) ; const questNode = xmlResult . getResult ( ) ; const playNode = getPlayNode ( xmlResult . getResultAt ( line ) ) ; if ( ! playNode ) { const err = new Error ( 'Invalid cursor position; to play from the cursor, cursor must be on a roleplaying, combat, or decision card.' ) ; err . name = 'RenderError' ; return dispatch ( pushError ( err ) ) ; } const newNode = new ParserNode ( playNode , ctx ) ; dispatch ( { type : 'REBOOT_APP' } ) ; dispatch ( changeSettings ( { audioEnabled : false , autoRoll : false , contentSets : { horror : quest . expansionhorror , future : quest . expansionfuture , wyrmsgiants : quest . expansionwyrmsgiants , scarredlands : quest . expansionscarredlands , } , difficulty : 'NORMAL' , fontSize : 'SMALL' , multitouch : false , numLocalPlayers : quest . minplayers , showHelp : true , simulator : true , timerSeconds : 10 , vibration : false , } ) ) ; dispatch ( loadNode ( newNode , new Quest ( { author : quest . author || '' , id : quest . id || '' , maxplayers : quest . maxplayers || 6 , minplayers : quest . minplayers || 1 , publishedurl : '' , summary : quest . summary || '' , theme : quest . theme || 'base' , title : quest . title || '' , partition : 'expedition-private' , expansionhorror : quest . expansionhorror , expansionfuture : quest . expansionfuture , expansionwyrmsgiants : quest . expansionwyrmsgiants , expansionscarredlands : quest . expansionscarredlands , } ) ) ) ; dispatch ( startPlaytestWorker ( oldWorker , questNode , { expansionhorror : Boolean ( quest . expansionhorror ) , expansionfuture : Boolean ( quest . expansionfuture ) , expansionwyrmsgiants : Boolean ( quest . expansionwyrmsgiants ) , expansionscarredlands : Boolean ( quest . expansionscarredlands ) , } ) ) ; } ) ; } ; }