Fixed Conjoured items

This commit is contained in:
Audrius Baltusis 2020-09-08 21:26:45 +03:00
parent eac154af0b
commit bc2126c1c5

View File

@ -6,60 +6,115 @@ class Item {
} }
} }
class Shop { class StandardItem extends Item {
constructor(items=[]){ itemTomorrow() {
this.items = items; this.quality = this.qualityTomorrow();
this.sellIn--;
return this;
} }
updateQuality() {
for (var i = 0; i < this.items.length; i++) { qualityTomorrow() {
if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { let qualityTomorrow = this.quality - this.calculateDepreciation();
if (this.items[i].quality > 0) { if (qualityTomorrow < 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { return 0;
this.items[i].quality = this.items[i].quality - 1; }
if (qualityTomorrow >= 50) {
return 50;
}
return qualityTomorrow;
}
calculateDepreciation() {
return this.sellIn <= 0 ? 2 : 1;
} }
} }
} else {
if (this.items[i].quality < 50) { class Sulfuras extends Item {
this.items[i].quality = this.items[i].quality + 1; itemTomorrow() {
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { return this;
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 < 6) {
if (this.items[i].quality < 50) { class ConjuredItem extends StandardItem {
this.items[i].quality = this.items[i].quality + 1; calculateDepreciation() {
return this.sellIn <= 0 ? 4 : 2;
} }
} }
}
} class BackStagePass extends StandardItem {
} calculateDepreciation() {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { switch (true) {
this.items[i].sellIn = this.items[i].sellIn - 1; case this.sellIn <= 0:
} return this.quality;
if (this.items[i].sellIn < 0) { case this.sellIn <= 5:
if (this.items[i].name != 'Aged Brie') { return -3;
if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { case this.sellIn <= 10:
if (this.items[i].quality > 0) { return -2;
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { default:
this.items[i].quality = this.items[i].quality - 1; return -1;
}
}
} else {
this.items[i].quality = this.items[i].quality - this.items[i].quality;
}
} else {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1;
}
} }
} }
} }
return this.items; class AgedBrie extends StandardItem {
calculateDepreciation() {
return this.sellIn <= 0 ? -2 : -1;
} }
} }
class ItemTypeFactory {
constructor() {
this.setupItemTypes();
this.setupClasses();
}
setupItemTypes() {
this.itemTypes = new Map();
this.itemTypes.set('Aged Brie', 'AgedBrie');
this.itemTypes.set('Backstage passes', 'BackStagePass');
this.itemTypes.set('Conjured', 'ConjuredItem');
this.itemTypes.set('Sulfuras', 'Sulfuras');
this.itemTypes.set('Standard', 'StandardItem');
}
setupClasses() {
this.availableClasses = {
StandardItem,
Sulfuras,
ConjuredItem,
BackStagePass,
AgedBrie
};
}
getClassName(name) {
const itemType = [...this.itemTypes]
.filter(([key, value]) => name.includes(key))
.map(([key, value]) => key)[0];
return this.itemTypes.get(itemType || this.defaultClassName);
}
createItemUsingType(item) {
const {name, sellIn, quality} = item;
return new this.availableClasses[this.getClassName(name)](name, sellIn, quality);
}
get defaultClassName() {
return 'Standard';
}
}
class Shop {
constructor(items = []) {
const itemTypeFactory = new ItemTypeFactory();
this.items = items.map((item) => itemTypeFactory.createItemUsingType(item));
}
updateQuality() {
return this.items.map((item) => item.itemTomorrow());
}
}
module.exports = { module.exports = {
Item, Item,
Shop Shop