-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbidding.js
More file actions
219 lines (211 loc) · 9.38 KB
/
Copy pathbidding.js
File metadata and controls
219 lines (211 loc) · 9.38 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// let highestBidValue = 0;
let BIDDER_ONE = 'player_1', BIDDER_TWO = 'player_2';
//at the end of bidding, open trump popup and append points to all players (showPointsTextForEachPlayer)
let INCREMENT_STEP_VALUE = 10;
//append 'your bid' and then the value
// <div class="bidContainer" style="display: inline-flex;">
// <div class="bidbuttonsContainer">
// <button>inc</button>
// </div>
// <span class="bidValueContainer">myBid:
// <span class="bidValue">20</span>
// </span>
// </div>
function makeBidDiv() {
let bidContainer = document.createElement('div');
bidContainer.className = 'bidContainer';
let bidButtonsContainer= document.createElement('div');
bidButtonsContainer.className = 'bidButtonsContainer';
let bidValueContainer = document.createElement('span');
bidValueContainer.textContent = 'myBid:';
bidValueContainer.className = 'bidValueContainer';
let bidValue = document.createElement('span');
bidValue.className = 'bidValue';
bidValue.textContent = '0';
bidValueContainer.appendChild(bidValue);
bidContainer.appendChild(bidValueContainer);
bidContainer.appendChild(bidButtonsContainer)
return bidContainer;
}
let playerDivs =document.getElementsByClassName('playerDiv');
for (player of playerDivs) {
appendAsChild(player, makeBidDiv());
}
function makeBidButton(btnType) {
let bidButton = document.createElement('button');
bidButton.className = 'bidBtn' + btnType;
bidButton.setAttribute('btnType', btnType);
bidButton.textContent = btnType;
bidButton.addEventListener('click', handleBidBtns(btnType));
return bidButton;
}
function handleBidBtns(btnType) {
//here function definitions are returned, which will get executed when click happens on bidBtns
if (btnType == 'increment') {
return handleIncrement;
}
if (btnType == 'equalise') {
return handleEqualise;
}
if (btnType == 'backout') {
return handleBackout;
}
}
function handleIncrement(e) {
let currBidder = e.target.closest('.playerDiv'),
currBidderId = currBidder.getAttribute('player'),
currBidderBidValueSpan = currBidder.querySelector('.bidValue'),
otherBidder = '', otherBidderBidValueSpan = '';
if (currBidderId == BIDDER_ONE) {
//curr Bidder is bidderOne. other bidder is bidderTwo.
otherBidder = document.getElementsByClassName(BIDDER_TWO)[0];
} else {
//curr Bidder is bidderTwo. other bidder is bidderOne.
otherBidder = document.getElementsByClassName(BIDDER_ONE)[0];
}
otherBidderBidValueSpan = otherBidder.querySelector('.bidValue');
let currBidderBidValue = Number(currBidderBidValueSpan.textContent),
otherBidderBidValue = Number(otherBidderBidValueSpan.textContent);
if (otherBidderBidValue >= currBidderBidValue) {
currBidderBidValueSpan.textContent = otherBidderBidValue + INCREMENT_STEP_VALUE;
// currBidder.querySelector('.bidBtnbackout').classList.remove('hidden');
// otherBidder.querySelector('.bidBtnbackout').classList.add('hidden');
} else {
currBidderBidValueSpan.textContent = currBidderBidValue + INCREMENT_STEP_VALUE;
}
currBidder.querySelector('.bidBtnbackout').classList.add('hidden');
otherBidder.querySelector('.bidBtnbackout').classList.remove('hidden');
}
function handleEqualise(e) {
let currBidder = e.target.closest('.playerDiv'),
currBidderId = currBidder.getAttribute('player'),
currBidderBidValueSpan = currBidder.querySelector('.bidValue'),
//equalise is shown only to BIDDER_ONE
otherBidder = document.getElementsByClassName(BIDDER_TWO)[0],
otherBidderBidValueSpan = otherBidder.querySelector('.bidValue');
if (Number(otherBidderBidValueSpan.textContent) > Number(currBidderBidValueSpan.textContent)) {
currBidderBidValueSpan.textContent = otherBidderBidValueSpan.textContent;
}
currBidder.querySelector('.bidBtnbackout').classList.add('hidden');
otherBidder.querySelector('.bidBtnbackout').classList.remove('hidden');
}
function handleBackout(e) {
//case1: bidderOne backsOut. then biddertwo becomes bidderOne, and next player becomes bidderTwo
//case2: bidderTwo backsOut. then next player becomes bidderTwo.
//place limit on next player who becomes bidderTwo. if next player is the one who started the bidding,
// then bidding complete. time to choose trump
let currBidder = e.target.closest('.playerDiv'),
currBidderId = currBidder.getAttribute('player'),
currBidderBidValueSpan = currBidder.querySelector('.bidValue'),
otherBidder = '', otherBidderBidValueSpan = '',otherBidderId = '';
if (currBidderId == BIDDER_ONE) {
//curr Bidder is bidderOne. other bidder is bidderTwo.
otherBidder = document.getElementsByClassName(BIDDER_TWO)[0];
otherBidderId = BIDDER_TWO;
} else {
//curr Bidder is bidderTwo. other bidder is bidderOne.
otherBidder = document.getElementsByClassName(BIDDER_ONE)[0];
otherBidderId = BIDDER_ONE;
}
otherBidderBidValueSpan = otherBidder.querySelector('.bidValue');
let currBidderBidValue = Number(currBidderBidValueSpan.textContent),
otherBidderBidValue = Number(otherBidderBidValueSpan.textContent),
highestBidValue = 0;
if (otherBidderBidValue >= currBidderBidValue) {
highestBidValue = otherBidderBidValue;
// currBidder.querySelector('.bidBtnbackout').classList.remove('hidden');
// otherBidder.querySelector('.bidBtnbackout').classList.add('hidden');
} else {
highestBidValue = currBidderBidValue;
}
// console.log(currBidderId, otherBidderId, highestBidValue);
let nextplayerId = '';
if (currBidderId == BIDDER_ONE) {
//case1
nextplayerId = findNextPlayerId(otherBidderId);
if (nextplayerId == BIDDER_ONE) {
biddingComplete(BIDDER_TWO);
return;
}
removeBidbtns(currBidderId);
removeBidbtns(otherBidderId);
bidBtns_bidderOne(otherBidderId);
BIDDER_ONE = otherBidderId;
BIDDER_TWO = nextplayerId;
} else {
//case2
nextplayerId = findNextPlayerId(currBidderId);
//if nextplayerId is BIDDER_ONE itself, bidding complete
if (nextplayerId == BIDDER_ONE) {
biddingComplete(BIDDER_ONE);
return;
}
removeBidbtns(currBidderId);
BIDDER_TWO = nextplayerId;
}
bidBtns_bidderTwo(nextplayerId);
addbiddingOutClassTo(currBidderId);
copyBidvalue(nextplayerId, highestBidValue);
}
function addbiddingOutClassTo(bidderId) {
let bidDiv = document.getElementsByClassName(bidderId)[0];
bidDiv.querySelector('.bidContainer').classList.add('biddedOut');
}
function copyBidvalue(copyToBidderId, value) {
let bidDiv = document.getElementsByClassName(copyToBidderId)[0];
bidDiv.querySelector('.bidValue').textContent = value;
}
function biddingComplete(bidderId) {
myLog('normal','bidding complete and won by : ' + bidderId);
//at the end of bidding, distribute cards and open trump popup and append points to all players (showPointsTextForEachPlayer)
//also remove bidContainers from all players
for (elem of document.querySelectorAll('.bidContainer') ) {
elem.remove();
}
showPointsTextForEachPlayer();
distributeCards();
setupTrumpPopup();
}
function findNextPlayerId(bidderId) {
//find next element in a cycle which does not have biddedOut class in bidContainer div,
//starting from next element fo currentBidderdId.
//Recursive solution
let playerIdSplit = bidderId.split('_');
let nextPlayerNumber = '';
if (Number(playerIdSplit[1]) == NUM_OF_PLAYERS ) {
nextPlayerNumber = 1;
} else {
nextPlayerNumber = Number(playerIdSplit[1]) + 1;
}
let nextPlayerId = playerIdSplit[0] + '_' + nextPlayerNumber;
let nextPlayerDiv = document.getElementsByClassName(nextPlayerId)[0];
if (nextPlayerDiv.querySelector('.bidContainer').classList.contains('biddedOut')) {
return findNextPlayerId(nextPlayerId);
} else {
return nextPlayerId;
}
}
function bidBtns_bidderOne(BIDDER_ONE) {
let playerDiv = document.getElementsByClassName(BIDDER_ONE)[0];
bidDiv = playerDiv.querySelector('.bidContainer');
let bidbtnscontainer = bidDiv.querySelector('.bidButtonsContainer');
bidbtnscontainer.textContent = '';
appendAsChild(bidbtnscontainer, makeBidButton('increment'));
appendAsChild(bidbtnscontainer, makeBidButton('equalise'));
appendAsChild(bidbtnscontainer, makeBidButton('backout'));
}
function bidBtns_bidderTwo(BIDDER_TWO) {
let playerDiv = document.getElementsByClassName(BIDDER_TWO)[0];
bidDiv = playerDiv.querySelector('.bidContainer');
let bidbtnscontainer = bidDiv.querySelector('.bidButtonsContainer');
bidbtnscontainer.textContent = '';
appendAsChild(bidbtnscontainer, makeBidButton('increment'));
// appendAsChild(bidbtnscontainer, makeBidButton('equalise'));
appendAsChild(bidbtnscontainer, makeBidButton('backout'));
}
function removeBidbtns(bidderId) {
let bidderDiv = document.getElementsByClassName(bidderId)[0];
bidderDiv.querySelector('.bidButtonsContainer').replaceChildren();
}
bidBtns_bidderOne(BIDDER_ONE);
bidBtns_bidderTwo(BIDDER_TWO);