1
+ "use strict" ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+ exports . getAllowedBids = exports . getLastNoSpecialBid = exports . getLastNotPassBid = void 0 ;
7
+
8
+ var _Suits = require ( "./Suits.js" ) ;
9
+
10
+ var _SpecialBids = require ( "./SpecialBids.js" ) ;
11
+
12
+ require ( "lodash.product" ) ;
13
+
14
+ var _lodash2 = _interopRequireDefault ( require ( "lodash" ) ) ;
15
+
16
+ var _positions = require ( "./positions.js" ) ;
17
+
18
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
19
+
20
+ //TODO refactor this naive implementation
21
+ const allBids = _lodash2 . default . chain ( _lodash2 . default . product ( _lodash2 . default . range ( 1 , 8 ) , _lodash2 . default . values ( _Suits . EXTENDED_SUITS ) ) ) . map ( e => {
22
+ const iface = {
23
+ isHigerThan : function ( bid = {
24
+ level : 0 ,
25
+ suit : 0
26
+ } ) {
27
+ return bid . level < this . level || bid . level === this . level && bid . suit < this . suit ;
28
+ }
29
+ } ;
30
+ return Object . assign ( Object . create ( iface ) , {
31
+ level : e [ 0 ] ,
32
+ suit : e [ 1 ]
33
+ } ) ;
34
+ } ) . concat ( _lodash2 . default . map ( _lodash2 . default . values ( _SpecialBids . SPECIAL_BIDS ) , bid => {
35
+ return {
36
+ bid
37
+ } ;
38
+ } ) ) . value ( ) ;
39
+
40
+ const isSpecial = bid => {
41
+ return _lodash2 . default . values ( _SpecialBids . SPECIAL_BIDS ) . indexOf ( bid . bid ) > - 1 ;
42
+ } ;
43
+
44
+ const getLastNotPassBid = bidding => {
45
+ return _lodash2 . default . findLast ( bidding , bid => {
46
+ return bid . bid !== _SpecialBids . SPECIAL_BIDS . PASS ;
47
+ } ) ;
48
+ } ;
49
+
50
+ exports . getLastNotPassBid = getLastNotPassBid ;
51
+
52
+ const getLastNoSpecialBid = bidding => {
53
+ return _lodash2 . default . findLast ( bidding , bid => {
54
+ return ! isSpecial ( bid ) ;
55
+ } ) ;
56
+ } ;
57
+
58
+ exports . getLastNoSpecialBid = getLastNoSpecialBid ;
59
+
60
+ const isAllowed = ( {
61
+ bid,
62
+ bidding,
63
+ position
64
+ } ) => {
65
+ const lastNotPassBid = getLastNotPassBid ( bidding ) ;
66
+ const lastNoSpecialBid = getLastNoSpecialBid ( bidding ) ;
67
+
68
+ if ( lastNotPassBid ) {
69
+ //Redouble allowed is previous bid was opponent
70
+ if ( bid . bid === _SpecialBids . SPECIAL_BIDS . REDOUBLE ) {
71
+ return lastNotPassBid . bid === _SpecialBids . SPECIAL_BIDS . DOUBLE && ! ( 0 , _positions . arePartners ) ( lastNotPassBid . position , position ) ;
72
+ } //Double only if last bid is not special and made by opponent
73
+
74
+
75
+ if ( bid . bid === _SpecialBids . SPECIAL_BIDS . DOUBLE ) {
76
+ return ! isSpecial ( lastNotPassBid ) && ! ( 0 , _positions . arePartners ) ( lastNotPassBid . position , position ) ;
77
+ }
78
+
79
+ if ( ! isSpecial ( bid ) ) {
80
+ return bid . isHigerThan ( lastNoSpecialBid ) ;
81
+ }
82
+ } else {
83
+ if ( isSpecial ( bid ) ) {
84
+ return bid . bid === _SpecialBids . SPECIAL_BIDS . PASS ;
85
+ }
86
+ }
87
+
88
+ return true ;
89
+ } ;
90
+
91
+ const getAllowedBids = ( {
92
+ bidding,
93
+ position
94
+ } ) => {
95
+ const lastBid = getLastNotPassBid ( bidding ) ;
96
+ return _lodash2 . default . chain ( allBids ) . filter ( bid => {
97
+ return isAllowed ( {
98
+ bid,
99
+ bidding,
100
+ position
101
+ } ) ;
102
+ } ) . groupBy ( element => {
103
+ return element . level || "special" ;
104
+ } ) . value ( ) ;
105
+ } ;
106
+
107
+ exports . getAllowedBids = getAllowedBids ;
0 commit comments