Skip to content

Instantly share code, notes, and snippets.

@JeanBarriere
Created January 10, 2020 16:18
Show Gist options
  • Save JeanBarriere/4512f20d93390641b9a03582b6c9de0a to your computer and use it in GitHub Desktop.
Save JeanBarriere/4512f20d93390641b9a03582b6c9de0a to your computer and use it in GitHub Desktop.
Gilded Rose
import { expect } from 'chai';
import { Item, GildedRose } from '../app/gilded-rose';
describe('Gilded Rose', function () {
it('should foo', function() {
const items = [
new Item("+5 Dexterity Vest", 10, 20), //
new Item("Aged Brie", 2, 0), //
new Item("Elixir of the Mongoose", 5, 7), //
new Item("Sulfuras, Hand of Ragnaros", 0, 80), //
new Item("Sulfuras, Hand of Ragnaros", -1, 80),
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
// this conjured item does not work properly yet
new Item("Conjured Aged Brie Mana Cake", 3, 6)
]
const reloaded = [
new Item("+5 Dexterity Vest", 10, 20), //
new Item("Aged Brie", 2, 0), //
new Item("Elixir of the Mongoose", 5, 7), //
new Item("Sulfuras, Hand of Ragnaros", 0, 80), //
new Item("Sulfuras, Hand of Ragnaros", -1, 80),
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
new Item("Conjured Aged Brie Mana Cake", 3, 6)
]
const gildedRose = new GildedRose(items);
const gildedRoseReloaded = new GildedRose(reloaded);
var days: number = 99;
for (let i = 0; i < days; i++) {
console.log("-------- day " + i + " --------");
console.log("name, sellIn, quality");
for (let idx = 0; idx < 8; idx++) {
console.log(reloaded[idx].name, reloaded[idx].quality, items[idx].quality)
expect(reloaded[idx].name).equal(items[idx].name)
expect(reloaded[idx].sellIn).equal(items[idx].sellIn)
expect(reloaded[idx].quality).equal(items[idx].quality)
}
console.log(reloaded[8].name, reloaded[8].quality, items[8].quality) // just show the conjured quality
gildedRose.updateQuality();
gildedRoseReloaded.updateQualityReloaded();
}
});
});
export class Item {
name: string;
sellIn: number;
quality: number;
constructor(name, sellIn, quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
}
export class ItemsFactory {
private self: Item
static with(item: Item): ItemsFactory {
return new ItemsFactory(item)
}
constructor (item: Item) {
this.self = item
}
private get isLegendary(): boolean {
return this.self.name.includes('Sulfuras')
}
private get isConjured(): boolean {
return this.self.name.includes('Conjured')
}
private get getsBetter(): boolean {
return this.self.name.includes('Aged Brie') || this.isBackstagePass
}
private get isBackstagePass(): boolean {
return this.self.name.includes('Backstage passes')
}
private get backstagePassQualityToDecrease(): number {
let ret = 1
if (this.self.sellIn <= 10) ret++
if (this.self.sellIn <= 5) ret++
return ret
}
private get quality (): number {
if (this.isLegendary) return 80
if (this.isBackstagePass && this.self.sellIn <= 0) return 0
let toDecrease = !this.isBackstagePass ? 1 : this.backstagePassQualityToDecrease
if (this.getsBetter) toDecrease *= -1
if (this.self.sellIn <= 0) toDecrease *= 2
if (this.isConjured) toDecrease *= 2
if (this.self.quality - toDecrease > 50) return 50
if (this.self.quality - toDecrease < 0) return 0
return this.self.quality - toDecrease
}
private get sellIn (): number {
return this.self.sellIn - (this.isLegendary ? 0 : 1)
}
public dailyUpdate () {
this.self.quality = this.quality
this.self.sellIn = this.sellIn
}
}
export class GildedRose {
items: Array<Item>;
constructor(items: Array<Item>) {
this.items = items;
}
updateQualityReloaded () {
for (const item of this.items) {
ItemsFactory.with(item).dailyUpdate()
}
return this.items
}
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
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 < 6) {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
}
}
}
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].sellIn = this.items[i].sellIn - 1;
}
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
}
}
} 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment