-
Notifications
You must be signed in to change notification settings - Fork 0
/
team.go
151 lines (129 loc) · 3.4 KB
/
team.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
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
package main
import (
"encoding/json"
"log"
"os"
"strings"
"github.com/davidnix/ffdraft/command"
"github.com/davidnix/ffdraft/players"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
var teamCmd = &cli.Command{
Name: "team",
Usage: "Manage a team",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "csv",
Usage: "(required) path to projections csv",
Required: true,
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "(required) your team name",
Required: true,
},
},
Action: teamInteractive,
}
const teamUsage = `
--------------------------------------------------------------------------------------------------------------------
Commands:
add [name]: adds player to your team
ceil [pos]: print the highest ceiling value for available players for each position
depth [team]: print a team's depth chart
exit, ctl+D: exits this program
find, f [name]: fuzzy finds players matching player name
floor, fl [pos]: sort by highest floor value for your team
help, h: print this help text
rm, drop [name]: removes player from your team
show, s: show your team grouped by position
team [floor|ceil] show your team sorted by floor|ceil
--------------------------------------------------------------------------------------------------------------------`
func teamInteractive(ctx *cli.Context) error {
repo, err := buildRepo(ctx.String("csv"))
if err != nil {
return err
}
team, err := loadTeam(ctx.String("name"))
if err != nil {
return err
}
log.Println("Loaded", len(repo.Available), "players")
repo.Sync(team.Players)
log.Println(len(repo.Claimed), "players on your team")
log.Println(len(repo.Available), "players remaining")
defer mustSaveTeam(team, repo)
log.Println(teamUsage)
command.Lineup(repo)
for {
in, err := command.GetInput()
if err != nil {
return err
}
input := strings.Fields(in)
var cmd string
args := []string{}
if len(input) > 0 {
cmd = input[0]
args = input[1:]
}
switch cmd {
case "team":
if len(args) > 0 && args[0] == "ceil" {
command.LineupCeil(repo)
} else {
command.LineupFloor(repo)
}
case "add":
command.Pick(repo, args)
command.Lineup(repo)
case "rm", "drop":
command.UnPick(repo, args)
command.Lineup(repo)
case "exit":
return errors.New("user canceled")
case "floor", "fl":
command.Floor(repo, args, 1000)
case "ceil":
command.Ceil(repo, args, 1000)
case "depth":
command.DepthChart(repo, args)
case "find", "f":
command.Find(repo, args)
case "show", "s":
command.Lineup(repo)
case "help", "h", "usage":
log.Println(teamUsage)
case "":
continue
default:
log.Printf("Unrecognized command %q. Type help for usage.\n", cmd)
}
}
}
func loadTeam(name string) (*players.Team, error) {
t := &players.Team{Name: name}
fname := t.Name + ".json"
existing, err := os.ReadFile(fname)
if err != nil {
log.Printf("team file %q: %v; creating new team", fname, err)
return t, nil
}
err = json.Unmarshal(existing, t)
return t, errors.Wrap(err, fname)
}
func mustSaveTeam(t *players.Team, repo *players.Repo) {
t.Sync(repo.Claimed)
fname := t.Name + ".json"
log.Println("saving team to", fname)
b, err := json.Marshal(t)
if err != nil {
panic(err)
}
err = os.WriteFile(fname, b, 0660)
if err != nil {
panic(err)
}
}