diff --git a/src/panel/main.ts b/src/panel/main.ts index c3c75d15..b1737010 100644 --- a/src/panel/main.ts +++ b/src/panel/main.ts @@ -269,7 +269,11 @@ function recoverState( // Resolve friend relationships var friend = undefined; if (state.petFriend) { - friend = allPets.locate(state.petFriend); + friend = allPets.locate( + state.petFriend, + state.petType, + state.petColor, + ); if (friend) { pet.recoverFriend(friend.pet); } @@ -576,9 +580,13 @@ export function petPanelApp( }); }); case 'delete-pet': - var pet = allPets.locate(message.name); + var pet = allPets.locate( + message.name, + message.type, + message.color, + ); if (pet) { - allPets.remove(message.name); + allPets.remove(message.name, pet.type, pet.color); saveState(stateApi); stateApi?.postMessage({ command: 'info', diff --git a/src/panel/pets.ts b/src/panel/pets.ts index ca81e8c1..19571b04 100644 --- a/src/panel/pets.ts +++ b/src/panel/pets.ts @@ -53,8 +53,12 @@ export interface IPetCollection { push(pet: PetElement): void; reset(): void; seekNewFriends(): string[]; - locate(name: string): PetElement | undefined; - remove(name: string): void; + locate( + name: string, + type: PetType, + color: PetColor, + ): PetElement | undefined; + remove(name: string, type: PetType, color: PetColor): void; } export class PetCollection implements IPetCollection { @@ -85,15 +89,19 @@ export class PetCollection implements IPetCollection { }); } - remove(name: string): any { - this._pets.forEach((pet) => { - if (pet.pet.name === name) { + remove(name: string, type: PetType, color: PetColor): any { + for (let i = 0; i < this._pets.length; i++) { + const pet = this._pets[i]; + if ( + pet.pet.name === name && + pet.type === type && + pet.color === color + ) { + this._pets.splice(i, 1); pet.remove(); + return; } - }); - this._pets = this._pets.filter((pet) => { - return pet.pet.name !== name; - }); + } } seekNewFriends(): string[] { diff --git a/src/panel/states.ts b/src/panel/states.ts index ac7c827b..9d92da20 100644 --- a/src/panel/states.ts +++ b/src/panel/states.ts @@ -42,8 +42,8 @@ export class PetInstanceState { export class PetElementState { petState: PetInstanceState | undefined; - petType: PetType | undefined; - petColor: PetColor | undefined; + petType: PetType | undefined | any; + petColor: PetColor | undefined | any; elLeft: string | undefined; elBottom: string | undefined; petName: string | undefined; diff --git a/src/test/suite/panel.test.ts b/src/test/suite/panel.test.ts index bc80ee3f..150c6701 100644 --- a/src/test/suite/panel.test.ts +++ b/src/test/suite/panel.test.ts @@ -113,7 +113,7 @@ suite('Pets Test Suite', () => { collection.push(testPetElement); assert.strictEqual(collection.locate('Jerry'), testPetElement); - collection.remove('Jerry'); + collection.remove('Jerry', PetType.cat, PetColor.black); assert.strictEqual(collection.locate('Jerry'), undefined); });