import { Player , PlayerId } from '../Player' ; import { IMilestone } from './IMilestone' ; export interface ClaimedMilestone { milestone : IMilestone ; player : Player ; } export interface SerializedClaimedMilestone { name ? : string ; playerId ? : PlayerId ; } export function serializeClaimedMilestones ( claimedMilestones ) { return claimedMilestones . map ( ( claimedMilestone ) => { return { name : claimedMilestone . milestone . name , playerId : claimedMilestone . player . id , } ; } ) ; } export function deserializeClaimedMilestones ( claimedMilestones , players , milestones ) { return claimedMilestones . map ( ( element ) => { const milestoneName = element . name ; if ( milestoneName === undefined ) { throw new Error ( 'Milestone name not found' ) ; } const milestone = milestones . find ( ( milestone ) => milestone . name === milestoneName ) ; if ( milestone === undefined ) { throw new Error ( `Milestone ${ milestoneName } not found when rebuilding Claimed Milestone` ) ; } const playerId = element . playerId ; if ( playerId === undefined ) { throw new Error ( `Player ID not found when rebuilding claimed milestone ${ milestoneName }` ) ; } const player = players . find ( ( player ) => player . id === playerId ) ; if ( player === undefined ) { throw new Error ( `Player ${ playerId } not found when rebuilding claimed milestone ${ milestoneName }` ) ; } return { milestone , player } ; } ) ; }