-
Notifications
You must be signed in to change notification settings - Fork 5
/
chipCollection.model.ts
109 lines (96 loc) · 3.18 KB
/
chipCollection.model.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
import { IChip } from '../chip/chip.interface';
import { IChipCollection } from './chipCollection.interface';
import { IObjectComparer } from '../../common/objectComparer.interface';
import { StringifyComparer } from '../../common/stringifyComparer.model';
import { IChipService } from '../../services/chipService.interface';
import { ChipService } from '../../services/chip.service';
/**
* Basic class to represent a grouping of IChips.
*
* Supports activities like: getting chips,
* adding chips, taking specific chip, taking
* chips by value, getting value of chips.
*/
export class ChipCollection implements IChipCollection {
private objectComparer: IObjectComparer = new StringifyComparer();
private chipService: IChipService = new ChipService();
constructor (private chips: IChip[] = []) {
}
public addChip (chip: IChip): this {
return this.addChips([chip]);
}
public addChips (chips: IChip[]): this {
this.getChips().unshift(...chips);
return this;
}
public getChips (): IChip[] {
return this.chips;
}
public setChips (chips: IChip[]): this {
this.chips = chips;
return this;
}
public getChipCount (): number {
return this.getChips().length;
}
public isEmpty (): boolean {
return this.getChipCount() === 0;
}
public removeChips (chips: IChip[]): IChipCollection {
if (chips.length === 0) {
return this;
}
this.chipService.removeChipsFromStack(this.getChips(), chips);
return this;
}
/**
* Condense chips into fewer but larger denominations.
* Uses the first chip's class to create new colored chips with.
*/
public colorUp (): IChipCollection {
if (this.getChipCount() === 0) {
return this;
}
const newChips = this.chipService.colorUp(this.getChips(), this.getChips()[0].constructor as any);
this.setChips(newChips);
return this;
}
/**
* Returns first identified index position of chip in
* collection.
* @param chip Object to search for in collection
*/
public indexOfChip (chip: IChip): number {
for (let i = 0; i < this.getChipCount(); i++) {
const loopCard = this.getChips()[i];
if (this.objectComparer.areEquivalent(chip, loopCard)) {
return i;
}
}
return -1;
}
/**
* Return value of Chips currently in ChipCollection (via `IChipCollection.getChips()`).
* Optionally, can pass in `IChip[]` to evaluate a manually passed in value.
* @param chips Optional, value of passed in chips. If no chips passed in,
* it will evaluate value of current ChipCollection.
*/
public getValue (chips?: IChip[] | undefined): number {
if (chips === undefined) {
chips = this.getChips();
}
return this.chipService.valueOfChips(chips);
}
/**
* Will pull `IChip`s from `IChipCollection` to satisfy
* the `amount` requested.
* @param amount Value to take from IChipCollection
*/
public takeValue (amount: number): IChip[] {
const currentValue = this.getValue();
if (amount > currentValue) {
throw new Error(`Not enough chips (${currentValue}) to satisfy requested amount ${amount}`);
}
return this.chipService.makeChange(this, amount, this.getChips()[0].constructor as any);
}
}