-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
56 lines (47 loc) · 1.18 KB
/
cli.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
package poker
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"time"
)
const PlayerPrompt = "Please enter the number of players: "
type CLI struct {
playerStore PlayerStore
in *bufio.Scanner
alerter BlindAlerter
out io.Writer
}
func NewCLI(store PlayerStore, in io.Reader, out io.Writer, alerter BlindAlerter) *CLI {
return &CLI {
playerStore:store,
in: bufio.NewScanner(in),
alerter:alerter,
out:out,
}
}
func (cli *CLI) PlayPoker() {
fmt.Fprint(cli.out, PlayerPrompt)
numberOfPlayers, _ := strconv.Atoi(cli.readLine())
cli.scheduleBlindAlerts(numberOfPlayers)
userInput := cli.readLine()
cli.playerStore.RecordWin(extractWinner(userInput))
}
func (c *CLI) scheduleBlindAlerts(numberOfPlayers int) {
blindIncrement := time.Duration(5 + numberOfPlayers) * time.Minute
blinds := []int{100, 200, 300, 400, 500, 600, 800, 1000, 2000, 4000, 8000}
blindTime := 0 * time.Second
for _, blind := range blinds {
c.alerter.ScheduleAlertAt(blindTime, blind)
blindTime = blindTime + blindIncrement
}
}
func extractWinner(userInput string) string {
return strings.Replace(userInput, " wins", "", 1)
}
func (cli *CLI) readLine() string {
cli.in.Scan()
return cli.in.Text()
}