Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Pet Command Takes in the Name, Type and Colour of the Pet. #400

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
14 changes: 11 additions & 3 deletions src/panel/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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',
Expand Down
26 changes: 17 additions & 9 deletions src/panel/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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[] {
Expand Down
4 changes: 2 additions & 2 deletions src/panel/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/panel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down