-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.mjs
102 lines (78 loc) · 1.84 KB
/
result.mjs
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
export function Result(...players) {
this.players = new Set(players);
this.tricks = new Set();
}
Result.prototype.claim = function(trick) {
this.tricks.add(trick);
};
Result.prototype.add = function(player) {
let result = player.result;
this.players.add(player);
let iterator = result.tricks.values();
for (let trick of iterator) {
this.tricks.add(trick);
}
result.tricks.clear();
};
Result.prototype.points = function() {
let iterator = this.tricks.values(), points = 0;
for (let trick of iterator) {
points += trick.points();
}
return points;
};
Result.prototype.matadors = function(trumps) {
let cards = new Set();
let iterator = this.tricks.values();
for (let trick of iterator) {
for (let { player, card } of trick) {
if (!trumps.contains(card)) {
continue;
}
if (!this.players.has(player)) {
continue;
}
cards.add(card);
}
}
let order = Array.from(trumps).reverse();
let count = 0;
for (let card of order) {
if (!cards.has(card)) {
break;
}
count++;
}
return count;
};
Result.compare = function(result, other) {
let winner = result, loser = other;
if (other.points() >= result.points()) {
winner = other;
loser = result;
}
return { winner, loser };
};
Result.multiplier = function(result, contract) {
let { winner, loser } = result;
let { partner, order: { trumps } } = contract;
let multiplier = 1;
if (!partner && winner.players.size == 1) {
multiplier += 3;
}
var matadors = winner.matadors(trumps);
if (matadors >= 3) {
multiplier += matadors;
}
var matadors = loser.matadors(trumps);
if (matadors >= 3) {
multiplier += matadors;
}
if (partner && loser.points() <= 30) {
multiplier++;
}
if (partner && loser.points() == 0) {
multiplier++;
}
return multiplier;
};