Skip to content

Commit

Permalink
fix delete all pets with the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
thuy3nana committed Dec 21, 2024
1 parent cd3b4af commit 71125e0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ async function handleRemovePetMessage(
if (pet) {
const panel = getPetPanel();
if (panel !== undefined) {
panel.deletePet(pet.name);
panel.deletePet(pet.name, pet.type, pet.color);
const collection = petList
.filter((item) => {
return item.name !== pet.name;
Expand Down Expand Up @@ -689,7 +689,7 @@ interface IPetPanel {
throwBall(): void;
resetPets(): void;
spawnPet(spec: PetSpecification): void;
deletePet(petName: string): void;
deletePet(petName: string, petType: string, petColor: string): void;
listPets(): void;
rollCall(): void;
themeKind(): vscode.ColorThemeKind;
Expand Down Expand Up @@ -812,10 +812,12 @@ class PetWebviewContainer implements IPetPanel {
void this.getWebview().postMessage({ command: 'roll-call' });
}

public deletePet(petName: string) {
public deletePet(petName: string, petType: string, petColor: string) {
void this.getWebview().postMessage({
command: 'delete-pet',
name: petName,
type: petType,
color: petColor
});
}

Expand Down Expand Up @@ -993,10 +995,12 @@ class PetPanel extends PetWebviewContainer implements IPetPanel {
void this.getWebview().postMessage({ command: 'roll-call' });
}

public deletePet(petName: string): void {
public deletePet(petName: string, petType: string, petColor: string): void {
void this.getWebview().postMessage({
command: 'delete-pet',
name: petName,
type: petType,
color: petColor
});
}

Expand Down
9 changes: 3 additions & 6 deletions src/panel/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ function calculateFloor(size: PetSize, theme: Theme): number {
function handleMouseOver(e: MouseEvent) {
var el = e.currentTarget as HTMLDivElement;
allPets.pets.forEach((element) => {
if (element.collision === el) {
if (!element.pet.canSwipe) {
return;
}
if (element.collision === el && element.pet.canSwipe) {
element.pet.swipe();
}
});
Expand Down Expand Up @@ -576,9 +573,9 @@ export function petPanelApp(
});
});
case 'delete-pet':
var pet = allPets.locate(message.name);
var pet = allPets.locatePet(message.name, message.type, message.color);
if (pet) {
allPets.remove(message.name);
allPets.remove(pet);
saveState(stateApi);
stateApi?.postMessage({
command: 'info',
Expand Down
15 changes: 11 additions & 4 deletions src/panel/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export interface IPetCollection {
reset(): void;
seekNewFriends(): string[];
locate(name: string): PetElement | undefined;
remove(name: string): void;
locatePet(name: string, type: string, color: string): PetElement | undefined;
remove(pet: PetElement): void;
}

export class PetCollection implements IPetCollection {
Expand Down Expand Up @@ -88,14 +89,20 @@ export class PetCollection implements IPetCollection {
});
}

remove(name: string): any {
locatePet(name: string, type: string, color: string): PetElement | undefined {
return this._pets.find((collection) => {
return collection.pet.name === name && collection.type === type && collection.color === color;
});
}

remove(targetPet: PetElement): any {
this._pets.forEach((pet) => {
if (pet.pet.name === name) {
if (pet === targetPet) {
pet.remove();
}
});
this._pets = this._pets.filter((pet) => {
return pet.pet.name !== name;
return pet !== targetPet;
});
}

Expand Down

0 comments on commit 71125e0

Please sign in to comment.