-
Notifications
You must be signed in to change notification settings - Fork 0
/
biddingcylce.go
88 lines (76 loc) · 2 KB
/
biddingcylce.go
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
package main
import (
"time"
"log"
)
type BiddingCycle struct {
// message channel for new nominations
biddingChan chan *Bid
// pause chan for telling cycle to pause
pauseChan chan bool
// channel for interupting cycle. pass a channel for callback once finished to avoid race condition
interuptChan chan chan bool
// bool indicating if open
open bool
}
func newBiddingCycle() *BiddingCycle {
return &BiddingCycle{
biddingChan: make(chan *Bid),
pauseChan: make(chan bool),
interuptChan: make(chan chan bool),
open: false,
}
}
// use as go routine. has callback to hub
func (d *BiddingCycle) getBids(player *Player, h *DraftHub) {
d.open = true
ticks := 30
updateCountdown(ticks, h)
biddingTicker := time.NewTicker(time.Second)
loop:
for {
select {
case <- biddingTicker.C:
ticks -= 1
updateCountdown(ticks, h)
if ticks < 1 {
biddingTicker.Stop()
h.endBidding <- player
d.open = false
break loop
}
case bid := <- d.biddingChan:
currentBid := player.bid.amount
// skip bid if owner already has top bid or bid isn't high enough
if bid.amount <= currentBid {
continue
}
// update player state
player.bid = bid
// update draftState
h.draftState.CurrentBid = bid.amount
h.draftState.CurrentBidderId = bid.bidderId
// reset timer if ticks < 12 seconds
if ticks < 15 {
ticks = 15
updateCountdown(ticks, h)
}
broadcastNewPlayerBid(player, h)
case shouldPause := <- d.pauseChan:
if shouldPause {
biddingTicker.Stop()
} else {
// just to make sure last one is stopped before we start new one
biddingTicker.Stop()
biddingTicker = time.NewTicker(time.Second)
}
case callbackChan := <- d.interuptChan:
log.Println("interupt bidding cycle")
biddingTicker.Stop()
d.open = false
// callback
callbackChan <- true
break loop
}
}
}