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

Issue #386 (Different pets with same name all deleted) #420

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ class PetQuickPickItem implements vscode.QuickPickItem {
public readonly name_: string,
public readonly type: string,
public readonly color: string,
public readonly petId: number,
) {
this.name = name_;
this.label = name_;
this.description = `${color} ${type}`;
this.petId = petId;
}

name: string;
Expand Down Expand Up @@ -110,6 +112,7 @@ export class PetSpecification {
this.color = color;
this.type = type;
this.size = size;

if (!name) {
this.name = randomName(type);
} else {
Expand Down Expand Up @@ -194,6 +197,7 @@ interface IPetInfo {
type: PetType;
name: string;
color: PetColor;
petId: number;
}

async function handleRemovePetMessage(
Expand All @@ -209,6 +213,7 @@ async function handleRemovePetMessage(
type: parts[0] as PetType,
name: parts[1],
color: parts[2] as PetColor,
petId: Number(parts[3]),
});
});
break;
Expand All @@ -221,7 +226,12 @@ async function handleRemovePetMessage(
await vscode.window
.showQuickPick<PetQuickPickItem>(
petList.map((val) => {
return new PetQuickPickItem(val.name, val.type, val.color);
return new PetQuickPickItem(
val.name,
val.type,
val.color,
val.petId,
);
}),
{
placeHolder: vscode.l10n.t('Select the pet to remove.'),
Expand All @@ -231,10 +241,10 @@ async function handleRemovePetMessage(
if (pet) {
const panel = getPetPanel();
if (panel !== undefined) {
panel.deletePet(pet.name);
panel.deletePet(pet.name, pet.petId);
const collection = petList
.filter((item) => {
return item.name !== pet.name;
return item.petId !== pet.petId;
})
.map<PetSpecification>((item) => {
return new PetSpecification(
Expand Down Expand Up @@ -370,6 +380,7 @@ export function activate(context: vscode.ExtensionContext) {
const panel = getPetPanel();
if (panel !== undefined) {
panel.listPets();

getWebview()?.onDidReceiveMessage(
handleRemovePetMessage,
context,
Expand Down Expand Up @@ -666,7 +677,7 @@ interface IPetPanel {
throwBall(): void;
resetPets(): void;
spawnPet(spec: PetSpecification): void;
deletePet(petName: string): void;
deletePet(petName: string, petId: number): void;
listPets(): void;
rollCall(): void;
themeKind(): vscode.ColorThemeKind;
Expand Down Expand Up @@ -786,8 +797,12 @@ class PetWebviewContainer implements IPetPanel {
this.getWebview().postMessage({ command: 'roll-call' });
}

public deletePet(petName: string) {
this.getWebview().postMessage({ command: 'delete-pet', name: petName });
public deletePet(petName: string, petId: number) {
this.getWebview().postMessage({
command: 'delete-pet',
name: petName,
petId: petId,
});
}

protected getWebview(): vscode.Webview {
Expand Down Expand Up @@ -964,8 +979,12 @@ class PetPanel extends PetWebviewContainer implements IPetPanel {
this.getWebview().postMessage({ command: 'roll-call' });
}

public deletePet(petName: string): void {
this.getWebview().postMessage({ command: 'delete-pet', name: petName });
public deletePet(petName: string, petId: number): void {
this.getWebview().postMessage({
command: 'delete-pet',
name: petName,
petId: petId,
});
}

public static revive(
Expand Down
5 changes: 5 additions & 0 deletions src/panel/basepettype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class InvalidStateException {}
export abstract class BasePetType implements IPetType {
label: string = 'base';
static count: number = 0;
static idMaker: number = 0;
sequence: ISequenceTree = {
startingState: States.sitIdle,
sequenceStates: [],
Expand All @@ -27,6 +28,7 @@ export abstract class BasePetType implements IPetType {
currentStateEnum: States;
holdState: IState | undefined;
holdStateEnum: States | undefined;
petId: number;
private el: HTMLImageElement;
private collision: HTMLDivElement;
private speech: HTMLDivElement;
Expand Down Expand Up @@ -66,6 +68,9 @@ export abstract class BasePetType implements IPetType {
this._size = size;
this._speed = this.randomizeSpeed(speed);

//increment petId: unique identifier for all pets
this.petId = ++BasePetType.idMaker;

// Increment the static count of the Pet class that the constructor belongs to
(this.constructor as any).count += 1;
}
Expand Down
5 changes: 3 additions & 2 deletions src/panel/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@ export function petPanelApp(
command: 'list-pets',
text: pets
.map(
(pet) => `${pet.type},${pet.pet.name},${pet.color}`,
(pet) =>
`${pet.type},${pet.pet.name},${pet.color},${pet.pet.petId}`,
)
.join('\n'),
});
Expand All @@ -578,7 +579,7 @@ export function petPanelApp(
case 'delete-pet':
var pet = allPets.locate(message.name);
if (pet) {
allPets.remove(message.name);
allPets.remove(message.petId);
saveState(stateApi);
stateApi?.postMessage({
command: 'info',
Expand Down
10 changes: 6 additions & 4 deletions src/panel/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class PetElement {
pet: IPetType;
color: PetColor;
type: PetType;
petId: number;
remove() {
this.el.remove();
this.collision.remove();
Expand All @@ -45,6 +46,7 @@ export class PetElement {
this.pet = pet;
this.color = color;
this.type = type;
this.petId = pet.petId;
}
}

Expand All @@ -54,7 +56,7 @@ export interface IPetCollection {
reset(): void;
seekNewFriends(): string[];
locate(name: string): PetElement | undefined;
remove(name: string): void;
remove(id: number): void;
}

export class PetCollection implements IPetCollection {
Expand Down Expand Up @@ -85,14 +87,14 @@ export class PetCollection implements IPetCollection {
});
}

remove(name: string): any {
remove(id: number): any {
this._pets.forEach((pet) => {
if (pet.pet.name === name) {
if (pet.pet.petId === id) {
pet.remove();
}
});
this._pets = this._pets.filter((pet) => {
return pet.pet.name !== name;
return pet.pet.petId !== id;
});
}

Expand Down
1 change: 1 addition & 0 deletions src/panel/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PetColor, PetType } from '../common/types';
export interface IPetType {
nextFrame(): void;

petId: number;
// Special methods for actions
canSwipe: boolean;
canChase: boolean;
Expand Down
3 changes: 2 additions & 1 deletion src/test/suite/panel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ suite('Pets Test Suite', () => {
assert.ok(testPet instanceof Cat);
assert.equal(testPet.emoji, '🐱');
assert.equal(testPet.name, 'Jerry');
assert.equal(testPet.petId, 1);

const testPetElement = new pets.PetElement(
petImageEl,
Expand All @@ -113,7 +114,7 @@ suite('Pets Test Suite', () => {
collection.push(testPetElement);
assert.strictEqual(collection.locate('Jerry'), testPetElement);

collection.remove('Jerry');
collection.remove(1);
assert.strictEqual(collection.locate('Jerry'), undefined);
});

Expand Down