import { Component , OnInit , Input , Output , EventEmitter , ViewChild } from '@angular/core' ; import { NPCState } from '../npc-states-widget.component' ; import * as settingsJson from '../../../../../assets/npc-settings.json' ; import { EventEditorComponent } from '../../event-widget/event-editor/editor/event-editor.component' ; import { destructureEventArray , createEventArray , EventArray , EventArrayType } from '../../../../models/events' ; import { EventType } from '../../event-widget/event-registry/abstract-event' ; @ Component ( { selector : 'app-npc-states' , templateUrl : './npc-states.component.html' , styleUrls : [ './npc-states.component.scss' , '../../widget.scss' ] , } ) export class NpcStatesComponent implements OnInit { @ ViewChild ( 'eventEditor' , { static : false } ) eventEditor ? : EventEditorComponent ; @ Input ( ) states : NPCState [ ] = [ ] ; @ Output ( ) exit : EventEmitter < NPCState [ ] > = new EventEmitter < NPCState [ ] > ( ) ; currentState ? : NPCState ; index = 0 ; props = settingsJson . default ; eventTypes = Object . values ( EventArrayType ) ; warnings : string [ ] = [ ] ; private missingTradeEvent = false ; eventType : EventArrayType = EventArrayType . Simple ; trader ? : string ; clipboard = '' ; constructor ( ) { } ngOnInit ( ) { this . states . forEach ( state => { if ( ! state . position ) { state . position = { x : 0 , y : 0 , lvl : 0 , active : false } ; } else if ( state . position . active !== false ) { state . position . active = true ; } } ) ; this . selectTab ( this . index ) ; } selectTab ( index ) { if ( this . currentState ) { if ( ! this . eventEditor ) { throw new Error ( 'event editor is not defined' ) ; } this . currentState . event = createEventArray ( this . eventEditor . export ( ) , this . eventType , this . trader ) ; this . eventEditor . show ( ) ; } this . currentState = this . states [ index ] ; this . index = index ; if ( this . currentState ) { ( { type : this . eventType , trader : this . trader } = destructureEventArray ( this . currentState . event ) ) ; } } newPage ( ) { this . index ++ ; this . states . splice ( this . index , 0 , { reactType : '' , position : { x : 0 , y : 0 , lvl : 0 , active : false } , face : '' , action : [ ] , hidden : false , condition : '' , config : 'normal' , event : [ ] } ) ; } copyPage ( ) { if ( ! this . currentState ) { return ; } if ( ! this . eventEditor ) { throw new Error ( 'event editor is not defined' ) ; } this . currentState . event = createEventArray ( this . eventEditor . export ( ) , this . eventType , this . trader ) ; this . clipboard = JSON . stringify ( this . currentState ) ; console . log ( JSON . parse ( this . clipboard ) ) ; } pastePage ( ) { this . index ++ ; this . states . splice ( this . index , 0 , JSON . parse ( this . clipboard ) ) ; } removePage ( ) { this . states . splice ( this . index , 1 ) ; this . index = Math . max ( 0 , this . index - 1 ) ; } export ( ) { if ( ! this . eventEditor ) { throw new Error ( 'event editor is not defined' ) ; } if ( this . currentState ) { this . currentState . event = createEventArray ( this . eventEditor . export ( ) , this . eventType , this . trader ) ; } const out = this . states ; out . forEach ( state => { if ( state . position && state . position . active ) { state . position . active = undefined ; } else { state . position = undefined ; } } ) ; return out ; } save ( ) { this . exit . emit ( this . export ( ) ) ; } cancel ( ) { this . exit . error ( 'cancel' ) ; } updateDisplayedWarnings ( ) { this . warnings . length = 0 ; if ( this . isTradeEvent ) { if ( ! this . trader ) { this . warnings . push ( 'Missing trader name' ) ; } if ( this . missingTradeEvent ) { this . warnings . push ( 'START_NPC_TRADE_MENU event is missing, trade popup will not appear' ) ; } } } updateTradeEventWarning ( updatedEvents ) { function hasEventOfTypeRecursive ( object , type ) { if ( typeof object !== 'object' || object === null ) { return false ; } for ( const [ key , value ] of Object . entries ( object ) ) { if ( ( key === 'type' && value === type ) || hasEventOfTypeRecursive ( value , type ) ) { return true ; } } return false ; } this . missingTradeEvent = updatedEvents . length > 0 && ! hasEventOfTypeRecursive ( updatedEvents , 'START_NPC_TRADE_MENU' ) ; this . updateDisplayedWarnings ( ) ; } get isTradeEvent ( ) { return this . eventType === EventArrayType . Trade ; } }