-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
128 lines (103 loc) · 2.79 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { productType } from "./constants";
export class StoreFront {
items: Item[];
constructor(items: Item[]) {
this.items = items;
}
updateQuality(): void {
// minus 1 if quality > 0
// minus sellDays by 1
// minus 1 again if sellDays <= 0 && quality > 0
// minus sellIn days by 1
// if sellIn days < 0 ? quality -2 : quality -1
// Math.max()
for (let item of this.items) {
this.updateSellDays(item);
switch (item.type) {
case productType.legendary:
break;
case productType.agedBrie:
this.updateAgedBrieItemQuality(item);
break;
case productType.backstagePass:
this.updateBackstagePassItemQuality(item);
break;
case productType.conjured:
this.updateConjuredItemQuality(item);
break;
default:
this.updateNormalItemQuality(item);
break;
}
}
}
updateSellDays(item: Item): void {
if (item.type !== productType.legendary) {
item.sellDays--;
}
}
updateNormalItemQuality(item: Item): void {
if (item.sellDays < 0) {
item.quality -= 2;
} else if (item.quality > 0) {
item.quality--;
}
item.quality = Math.max(item.quality, 0);
}
updateConjuredItemQuality(item: Item): void {
if (item.sellDays < 0) {
item.quality -= 4;
} else if (item.quality > 0) {
item.quality -= 2;
}
item.quality = Math.max(item.quality, 0);
}
updateBackstagePassItemQuality(item: Item): void {
if (item.sellDays <= 0) {
item.quality = 0;
} else if (item.sellDays < 6) {
item.quality += 3;
} else if (item.sellDays < 11) {
item.quality += 2;
} else {
item.quality++;
}
item.quality = Math.min(item.quality, 50);
}
updateAgedBrieItemQuality(item: Item): void {
if (item.sellDays < 0) {
item.quality += 2;
} else {
item.quality++;
}
item.quality = Math.min(item.quality, 50);
}
}
export class Item {
name: string;
type: string;
quality: number;
sellDays: number;
constructor(name: string, type: string, quality: number, sellDays: number) {
const isLegendaryItem = type === productType.legendary;
this.name = name;
this.type = type;
this.quality = isLegendaryItem ? 80 : quality;
this.sellDays = isLegendaryItem ? 0 : sellDays;
}
toString(): string {
return `${this.name}, ${this.type}, ${this.quality}, ${this.sellDays}`;
}
}
export class LegendaryItem {
name: string;
type: string;
quality: number;
sellDays: number;
constructor(name: string) {
(this.name = name), (this.type = productType.legendary), (this.quality = 80), (this.sellDays = 0);
}
toString(): string {
return `${this.name}, ${this.type}, ${this.quality}, ${this.sellDays}`;
}
}