-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.ts
102 lines (98 loc) · 3.39 KB
/
Game.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
import * as config from './config.json';
import { Drug } from './Drug';
import { Save } from './Save.js';
import { Dealer } from './Dealer.js';
import { PurchaseResult } from './PurchaseResult.js';
import { Client } from './Client.js';
import { CombineResult } from './CombineResult.js';
import { Upgrade } from './Upgrade.js';
import { LevelUpgrade } from './LevelUpgrade.js';
import { UpgradeResult } from './UpgradeResult.js';
import { Constants } from './Constants.js';
import { AdvertisingUpgrade } from './AdvertisingUpgrade.js';
import { YieldUpgrade } from './YieldUpgrade.js';
import { SellResult } from './SellResult.js';
import { Recipe } from './Recipe.js';
export class Game {
private save: Save;
private upgrades: Upgrade[];
private yieldUpgrade: YieldUpgrade;
public constructor(save?: string) {
if (!save) this.save = new Save();
else {
this.save = JSON.parse(save);
this.save = Object.assign(new Save, this.save);
this.save.assignChildren();
}
//console.log(new Drug([1, 2, 1, 3, 1, 4, 1, 5], 1, 10).getYield());
//let newSave: Save = (<any>Object).assign(new Save, JSON.parse(JSON.stringify(this.save)));
//newSave.assignChildren();
//this.save = newSave;
//console.log(newSave.getClients());
//console.log(newSave.getClients()[0].getDrugFilter().getFilterType());
//console.log(newSave.getWallet().getMoney())
//console.log(newSave.getDrugs());
this.upgrades = [
new LevelUpgrade(this.levelUp.bind(this)),
new AdvertisingUpgrade(this.advertise.bind(this)),
new YieldUpgrade(() => { })
];
this.yieldUpgrade = <YieldUpgrade>this.upgrades[2];
this.upgrades.forEach((upgrade: Upgrade, i: number) => {
upgrade.setState(this.save.getUpgradeStates()[i]);
});
}
public getDrugs(): Drug[] {
return this.save.getDrugs();
}
public getValidDrugs(client: Client): Drug[] {
return this.save.getValidDrugs(client);
}
public getLevel(): number {
return this.save.getLevel();
}
public combineDrugs(drug1: Drug, drug2: Drug, quantity1: number, quantity2: number): CombineResult {
return this.save.combineDrugs(drug1, drug2, quantity1, quantity2, this.yieldUpgrade.getBonus());
}
public getDealers(): Dealer[] {
return this.save.getDealers();
}
public getClients(): Client[] {
return this.save.getClients();
}
public getUpgrades(): Upgrade[] {
return this.upgrades;
}
public getRecipes(): Recipe[] {
return this.save.getRecipes();
}
public reset(): void {
this.save = new Save();
}
private levelUp(): void {
this.save.incrementLevel();
const nextDealer: Dealer = Constants.defaultDealers()[this.save.getLevel()];
if (nextDealer) this.save.addDealer(nextDealer);
this.save.addClient(new Client(this.save.getLevel()));
}
private advertise(): void {
for (let i: number = 0; i < 3; i++) {
this.save.addClient(new Client(this.save.getLevel()));
}
}
public buyFromDealer(dealer: Dealer, quantity: number = 1): PurchaseResult {
return dealer.purchase(quantity, this.save.getWallet(), this.save.getDrugController());
}
public sellToClient(drug: Drug, client: Client, quantity: number = 1): SellResult {
return client.sellDrug(drug, this.save.getWallet(), this.save.getDrugController(), quantity);
}
public buyUpgrade(upgrade: Upgrade): UpgradeResult {
return upgrade.buy(this.save.getWallet());
}
public getMoney(): number {
return this.save.getWallet().getMoney();
}
public getSerializedSave(): string {
return JSON.stringify(this.save);
}
}