-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpurchase.ts
More file actions
104 lines (85 loc) · 2.24 KB
/
purchase.ts
File metadata and controls
104 lines (85 loc) · 2.24 KB
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
/// <reference path="modifier.ts" />
//
// Purchases.
//
// Purchases are a subtype of modifier that can be bought in the game. They provide
// information about costing that can be used to prioritise what to buy next.
//
abstract class Purchase extends Modifier {
constructor(sim: Simulator) {
super(sim);
}
abstract get name(): string;
abstract get price(): number;
abstract get isAvailable(): boolean;
abstract purchase(): void;
get longName(): string {
return this.name;
}
get purchaseTime(): number {
return this.price / this.sim.effectiveCps();
}
get pbr(): number {
return this.benefit / this.price;
}
get pvr(): number {
return this.value / this.price;
}
// Do nothing to save deleting all the requires calls since they may be useful some day
requires(name: string): this {
return this;
}
// Benefit returns the exact per second cookie generation increase. Value is used to quantify how
// much a Purchase is worth when what it offers doesn't alter the per-second gains.
get value(): number {
return this.benefit;
}
}
//
// PurchaseChain
//
// Looking ahead multiple purchases can give better results. This class is designed to represent
// those chains of purchases.
//
class PurchaseChain extends Purchase {
constructor(sim: Simulator, public purchases: Purchase[]) {
super(sim);
}
get isAvailable(): boolean {
return this.purchases.every(p => p.isAvailable);
}
apply(): void {
for (let i = 0; i < this.purchases.length; ++i) {
this.purchases[i].apply();
}
}
revoke(): void {
for (let i = this.purchases.length - 1; i >= 0; --i) {
this.purchases[i].revoke();
}
}
get name(): string {
return this.purchases.map(p => p.longName).join(" -> ");
}
get purchaseTime(): number {
let time: number = 0;
for (var i = 0; i < this.purchases.length; ++i) {
time += this.purchases[i].purchaseTime;
this.purchases[i].apply();
}
this.revoke();
return time;
}
purchase(): void {
console.log("Attempt to buy a PurchaseChain");
}
get price(): number {
let price = 0;
for (let i = 0; i < this.purchases.length; ++i) {
price += this.purchases[i].price;
this.purchases[i].apply();
}
this.revoke();
return price;
}
}