import { clone } from './functions/util' ; import { checkCondition } from './functions/condition' ; import { eventMsg } from './Utils/EventMessage' ; import { Message } from './Defines' ; interface IListAchievementData { id : number ; name : string ; opportunity : number ; description : string ; hide : number ; grade : number ; isAchieved : boolean ; } class Achievement { constructor ( ) { } Opportunity = { START : "START" , TRAJECTORY : "TRAJECTORY" , SUMMARY : "SUMMARY" , END : "END" } ; #achievements ; initial ( { achievements } ) { this . #achievements = achievements ; } count ( ) { return Object . keys ( this . #achievements ) . length ; } list ( property ) { return Object . values ( this . #achievements ) . map ( ( { id , name , opportunity , description , hide , grade , } ) => ( { id , name , opportunity , description , hide , grade , isAchieved : this . isAchieved ( id , property ) } ) ) ; } get ( achievementId ) { const achievement = this . #achievements [ achievementId ] ; if ( ! achievement ) throw new Error ( `[ERROR] No Achievement[${ achievementId }]` ) ; return clone ( achievement ) ; } check ( achievementId , property ) { const { condition } = this . get ( achievementId ) ; return checkCondition ( property , condition ) ; } isAchieved ( achievementId , property ) { for ( const [ achieved ] of ( property . get ( property . TYPES . ACHV ) || [ ] ) ) if ( achieved == achievementId ) return true ; return false ; } achieve ( opportunity , property ) { this . list ( property ) . filter ( ( { isAchieved } ) => ! isAchieved ) . filter ( ( { opportunity : o } ) => o == opportunity ) . filter ( ( { id } ) => this . check ( id , property ) ) . forEach ( ( { id } ) => { property . achieve ( property . TYPES . ACHV , id ) ; eventMsg . emit ( Message . Achievement , this . get ( id ) ) ; } ) ; } } export { Achievement } ; export type { IListAchievementData } ;