From 1fb9aaf8f9a3d49b6953492b04923e45dcb90a87 Mon Sep 17 00:00:00 2001 From: TeeMee123 <123teemee@gmail.com> Date: Tue, 25 Jun 2024 17:13:12 +0100 Subject: [PATCH] Update gilded-rose.ts: fixes and refactoring - Added logic for handling conjured items. - Improved flow of logic in the if else statements. - Added code comments. - Other minor refactoring changes. --- TypeScript/app/gilded-rose.ts | 57 +++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/TypeScript/app/gilded-rose.ts b/TypeScript/app/gilded-rose.ts index db58d67810..3202e98bcb 100644 --- a/TypeScript/app/gilded-rose.ts +++ b/TypeScript/app/gilded-rose.ts @@ -17,48 +17,61 @@ export class GildedRose { this.items = items; } + isItemConjured(itemName: string): boolean { + return itemName.startsWith("Conjured"); + } + updateQuality() { for (let i = 0; i < this.items.length; i++) { - if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { - if (this.items[i].quality > 0) { - if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].quality = this.items[i].quality - 1 - } - } - } else { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 + + // Process quality increase exceptions + if (this.items[i].name == 'Aged Brie' || this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { + if (this.items[i].quality <= 49) { + this.items[i].quality++; + // Process additional potential quality increases for backstage passes if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { - if (this.items[i].sellIn < 11) { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - } + if (this.items[i].sellIn <= 10 && this.items[i].quality <= 49) { + this.items[i].quality++; } - if (this.items[i].sellIn < 6) { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - } + if (this.items[i].sellIn <= 5 && this.items[i].quality <= 49) { + this.items[i].quality++; } } } + } else { + if (this.items[i].quality > 0) { + // Decrease quality except in certain cases + if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { + this.items[i].quality = this.items[i].quality - 1; + } + } } + + // Decrease 'sell in' value except in certain cases if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].sellIn = this.items[i].sellIn - 1; + let expiryRate = 1; + if(this.isItemConjured(this.items[i].name)) { + expiryRate = 2; + } + this.items[i].sellIn -= expiryRate; } + + // Process additional quality change due to expiry if (this.items[i].sellIn < 0) { if (this.items[i].name != 'Aged Brie') { if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { if (this.items[i].quality > 0) { if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].quality = this.items[i].quality - 1 + this.items[i].quality--; } } + // Process some exceptional cases } else { - this.items[i].quality = this.items[i].quality - this.items[i].quality + this.items[i].quality = 0; } } else { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 + if (this.items[i].quality <= 49) { + this.items[i].quality++; } } }