-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettle.js
More file actions
278 lines (222 loc) Β· 7.27 KB
/
settle.js
File metadata and controls
278 lines (222 loc) Β· 7.27 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
function passLine ({ bets, hand, rules }) {
if (!bets?.pass?.line) {
if (process.env.DEBUG) console.log('[decision] no pass line bet')
return { bets }
}
const actionResults = ['seven out', 'point win', 'comeout win', 'comeout loss']
const betHasAction = actionResults.includes(hand.result)
if (!betHasAction) {
if (process.env.DEBUG) console.log(`[decision] pass line bet carries over (${hand.result})`)
return { bets } // keep bets intact if no action
}
const payout = {
type: hand.result,
principal: bets.pass.line.amount,
profit: bets.pass.line.amount * 1
}
delete bets.pass.line // clear pass line bet on action
if (hand.result === 'comeout loss' || hand.result === 'seven out') {
if (process.env.DEBUG) console.log(`[payout] pass line loss -$${payout.principal}`)
return { bets }
}
if (process.env.DEBUG) {
console.log(`[payout] ${payout.type} $${payout.principal + payout.profit}`)
}
return { payout, bets }
}
function passOdds ({ bets, hand, rules }) {
if (!bets?.pass?.odds) {
if (process.env.DEBUG) console.log('[decision] no pass odds bet')
return { bets }
}
const actionResults = ['seven out', 'point win']
const betHasAction = actionResults.includes(hand.result)
if (!betHasAction) {
if (process.env.DEBUG) console.log(`[decision] pass odds bet carries over (${hand.result})`)
return { bets } // keep bets intact if no action
}
const payouts = {
4: 2,
5: 3 / 2,
6: 6 / 5,
8: 6 / 5,
9: 3 / 2,
10: 2
}
const payout = {
type: 'pass odds win',
principal: bets.pass.odds.amount,
profit: bets.pass.odds.amount * payouts[hand.diceSum]
}
delete bets.pass.odds // clear pass odds bet on action
if (hand.result === 'seven out') {
if (process.env.DEBUG) console.log(`[payout] pass odds loss -$${payout.principal}`)
// don't return the payout argument for a loss
return { bets }
}
if (process.env.DEBUG) {
console.log(`[payout] ${payout.type} $${payout.principal + payout.profit}`)
}
return { payout, bets }
}
function placeBet ({ bets, hand, placeNumber }) {
const label = placeNumber === 6 ? 'six' : placeNumber === 8 ? 'eight' : String(placeNumber)
if (!bets?.place?.[label]) {
if (process.env.DEBUG) console.log(`[decision] no place ${placeNumber} bet`)
return { bets }
}
if (hand.result === 'point set') {
if (process.env.DEBUG) console.log(`[decision] place ${placeNumber} bet waits for next roll`)
return { bets }
}
if (hand.diceSum === 7 && hand.result === 'seven out') {
if (process.env.DEBUG) console.log(`[decision] place ${placeNumber} loss -$${bets.place[label].amount}`)
delete bets.place[label]
if (Object.keys(bets.place).length === 0) delete bets.place
return { bets }
}
// place bets are off on comeout. if a point is set matching the place number, the bet doesn't win
if (hand.diceSum === placeNumber && hand.result !== 'point set') {
const payouts = {
6: 7 / 6,
8: 7 / 6
}
const payout = {
type: `place ${placeNumber} win`,
principal: bets.place[label].amount,
profit: bets.place[label].amount * payouts[placeNumber]
}
// bet comes down on a win to give control to the betting module to put it back up if desired
delete bets.place[label]
return { payout, bets }
}
if (process.env.DEBUG) console.log(`[decision] place ${placeNumber} bet stands`)
return { bets }
}
function placeSix (opts) {
return placeBet({ ...opts, placeNumber: 6 })
}
function placeEight (opts) {
return placeBet({ ...opts, placeNumber: 8 })
}
function comeLine ({ bets, hand }) {
if (!bets?.come) {
if (process.env.DEBUG) console.log('[decision] no come bets')
return { bets }
}
const payouts = []
bets.come.points = bets.come.points || {}
if (bets.come.points) {
Object.keys(bets.come.points).forEach(point => {
const remainingBets = []
bets.come.points[point].forEach(bet => {
if (hand.result === 'seven out') {
if (process.env.DEBUG) console.log(`[decision] come line ${point} loss -$${bet.line.amount}`)
return
}
if (hand.diceSum === Number(point)) {
const linePayout = {
type: 'come line win',
principal: bet.line.amount,
profit: bet.line.amount
}
payouts.push(linePayout)
if (bet.odds) {
const oddsPayouts = {
4: 2,
5: 3 / 2,
6: 6 / 5,
8: 6 / 5,
9: 3 / 2,
10: 2
}
payouts.push({
type: 'come odds win',
principal: bet.odds.amount,
profit: bet.odds.amount * oddsPayouts[point]
})
}
return
}
remainingBets.push(bet)
})
if (remainingBets.length) {
bets.come.points[point] = remainingBets
} else {
delete bets.come.points[point]
}
})
if (Object.keys(bets.come.points).length === 0) {
delete bets.come.points
}
}
const pending = bets.come.pending || []
if (pending.length) {
const immediateWins = [7, 11]
const immediateLosses = [2, 3, 12]
pending.forEach(bet => {
if (immediateWins.includes(hand.diceSum)) {
const payout = {
type: 'come line win',
principal: bet.amount,
profit: bet.amount
}
payouts.push(payout)
} else if (immediateLosses.includes(hand.diceSum)) {
if (process.env.DEBUG) console.log(`[decision] come line loss -$${bet.amount}`)
} else {
bets.come.points = bets.come.points || {}
bets.come.points[hand.diceSum] = bets.come.points[hand.diceSum] || []
bets.come.points[hand.diceSum].push({ line: { amount: bet.amount } })
if (process.env.DEBUG) console.log(`[decision] come line moves to ${hand.diceSum}`)
}
})
delete bets.come.pending
}
if (bets.come && Object.keys(bets.come).length === 0) {
delete bets.come
}
return { bets, payouts }
}
function all ({ bets, hand, rules }) {
const payouts = []
const passLineResult = passLine({ bets, hand, rules })
bets = passLineResult.bets
payouts.push(passLineResult.payout)
const passOddsResult = passOdds({ bets, hand, rules })
bets = passOddsResult.bets
payouts.push(passOddsResult.payout)
const comeLineResult = comeLine({ bets, hand })
bets = comeLineResult.bets
payouts.push(...(comeLineResult.payouts || []))
const placeSixResult = placeSix({ bets, hand })
bets = placeSixResult.bets
payouts.push(placeSixResult.payout)
const placeEightResult = placeEight({ bets, hand })
bets = placeEightResult.bets
payouts.push(placeEightResult.payout)
bets.payouts = payouts.reduce((memo, payout) => {
if (!payout) return memo
if (process.env.DEBUG) console.log(`[payout] ${payout.type} $${payout.principal + payout.profit} (principal: ${payout.principal}, profit: ${payout.profit})`)
memo.principal += payout.principal
memo.profit += payout.profit
memo.total += payout.principal + payout.profit
memo.ledger.push(payout)
return memo
}, {
principal: 0,
profit: 0,
total: 0,
ledger: []
})
return bets
}
module.exports = {
passLine,
passOdds,
placeBet,
placeSix,
placeEight,
comeLine,
all
}