Skip to content

Commit

Permalink
Plays are now case-insensitive (#50)
Browse files Browse the repository at this point in the history
* lexer has a PA and Command mode

* gamefile writes conf, cr markers

* ui reformats gamefile on save

* playbyplay notes location
  • Loading branch information
slshen authored Oct 3, 2024
1 parent 96c7d68 commit 885b567
Show file tree
Hide file tree
Showing 19 changed files with 296 additions and 139 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Setting up go
uses: actions/setup-go@v2
with:
go-version: '1.19'
go-version: '1.22'
- name: Caching go modules
uses: actions/cache@v2
with:
Expand All @@ -39,7 +39,7 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- name: Installing golangci-lint
run: wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.47.2
run: wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.58.1
- name: Build and Unit Test
run: ./hack/build.sh

10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
module github.com/slshen/sb

go 1.18
go 1.22.0

toolchain go1.22.3

require (
github.com/alecthomas/participle/v2 v2.1.0
github.com/alecthomas/participle/v2 v2.1.1
github.com/gdamore/tcell/v2 v2.7.1
github.com/hashicorp/go-multierror v1.1.1
github.com/kr/text v0.2.0
github.com/mitchellh/go-wordwrap v1.0.1
github.com/mitchellh/mapstructure v1.5.0
github.com/rivo/tview v0.0.0-20240921122403-a64fc48d7654
github.com/spf13/cobra v1.7.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
17 changes: 10 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
github.com/alecthomas/assert/v2 v2.3.0 h1:mAsH2wmvjsuvyBvAmCtm7zFsBlb8mIHx5ySLVdDZXL0=
github.com/alecthomas/participle/v2 v2.1.0 h1:z7dElHRrOEEq45F2TG5cbQihMtNTv8vwldytDj7Wrz4=
github.com/alecthomas/participle/v2 v2.1.0/go.mod h1:Y1+hAs8DHPmc3YUFzqllV+eSQ9ljPTk0ZkPMtEdAx2c=
github.com/alecthomas/assert/v2 v2.3.0/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/participle/v2 v2.1.1 h1:hrjKESvSqGHzRb4yW1ciisFJ4p3MGYih6icjJvbsmV8=
github.com/alecthomas/participle/v2 v2.1.1/go.mod h1:Y1+hAs8DHPmc3YUFzqllV+eSQ9ljPTk0ZkPMtEdAx2c=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -16,6 +18,7 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand Down Expand Up @@ -45,12 +48,12 @@ github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down
4 changes: 2 additions & 2 deletions pkg/game/gamemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (m *gameMachine) handleActualPlay(play *gamefile.ActualPlay, lastState *Sta
state.Pitches = state.LastState.Pitches + Pitches(play.PitchSequence)
state.Batter = state.LastState.Batter
} else {
state.Batter = m.battingTeam.parsePlayerID(play.Batter.String())
state.Batter = m.battingTeam.parsePlayerID(play.Batter)
state.Pitches = Pitches(play.PitchSequence)
}
if state.Batter == "" {
Expand Down Expand Up @@ -390,7 +390,7 @@ func (m *gameMachine) handlePlayCode(play gamefile.Play, state *State) error {
state.Complete = true
case pp.playIs("DGR"):
state.Play = Play{
Type: Double,
Type: GroundRuleDouble,
}
m.impliedAdvance(play, state, "B-2")
state.Complete = true
Expand Down
35 changes: 34 additions & 1 deletion pkg/game/modifiers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package game

import "strings"
import (
"regexp"
"strconv"
"strings"
)

type Modifiers []string

Expand All @@ -23,6 +27,12 @@ const (
)

type Trajectory string
type Location struct {
Fielder int
Length string
}

var locationRe = regexp.MustCompile(`[A-Z]+([1-9])(S|D)?`)

func (mods Modifiers) Trajectory() Trajectory {
for _, m := range mods {
Expand Down Expand Up @@ -53,6 +63,29 @@ func (mods Modifiers) Trajectory() Trajectory {
return ""
}

func (mods Modifiers) Location() *Location {
for _, m := range mods {
if m[0] != 'E' {
rm := locationRe.FindStringSubmatch(m)
if rm != nil {
// F8S = short center, P6D deep shortstop
fielder, _ := strconv.Atoi(rm[1])
var length string
if rm[2] == "D" {
length = "deep"
} else if rm[2] == "S" {
length = "short"
}
return &Location{
Fielder: fielder,
Length: length,
}
}
}
}
return nil
}

func (mods Modifiers) Contains(codes ...string) bool {
for _, m := range mods {
for _, code := range codes {
Expand Down
3 changes: 2 additions & 1 deletion pkg/game/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type PlayType byte
const (
Single PlayType = iota
Double
GroundRuleDouble
Triple
HomeRun
CaughtStealing
Expand Down Expand Up @@ -63,7 +64,7 @@ func (p *Play) Is(ts ...PlayType) bool {
}

func (p *Play) IsHit() bool {
return p.Type == Single || p.Type == Double || p.Type == Triple || p.Type == HomeRun
return p.Type == Single || p.Type == Double || p.Type == Triple || p.Type == HomeRun || p.Type == GroundRuleDouble
}

func (p *Play) IsStrikeOut() bool {
Expand Down
59 changes: 30 additions & 29 deletions pkg/game/playtype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 63 additions & 29 deletions pkg/gamefile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,30 @@ type TeamEvents struct {

type Property struct {
Pos Position
Key string `parser:"@Ident"`
Value string `parser:"@Text (NL+|EOF)"`
Key string `parser:"@Key"`
Value string `parser:"@Value (NL+|EOF)"`
}

type Event struct {
Pos Position
Play *ActualPlay `parser:"@@"`
Afters []*After `parser:" @@*"`
Comment string `parser:" @Text? (NL|EOF)"`
Alternative *Alternative `parser:"| 'alt' @@ (NL|EOF)"`
Pitcher string `parser:"| ('pitcher'|'pitching') @Code (NL|EOF)"`
RAdjRunner Numbers `parser:"| 'radj' @Numbers"`
RAdjBase string `parser:" @Code (NL|EOF)"`
Score string `parser:"| 'score' @Code (NL|EOF)"`
Final string `parser:"| 'final' @Code (NL|EOF)"`
HSubEnter Numbers `parser:"| 'hsub' @Numbers"`
HSubFor Numbers `parser:" 'for' @Code (NL|EOF)"` // TODO why @Code
VSubEnter Numbers `parser:"| 'vsub' @Numbers"`
VSubFor Numbers `parser:" 'for' @Code (NL|EOF)"`
Alternative *Alternative `parser:"'alt' @@ (NL|EOF)"`
Pitcher string `parser:"| ('pitcher'|'pitching') @Token (NL|EOF)"`
RAdjRunner Numbers `parser:"| 'radj' @Token"`
RAdjBase string `parser:" @Token (NL|EOF)"`
Score string `parser:"| 'score' @Token (NL|EOF)"`
Final string `parser:"| 'final' @Token (NL|EOF)"`
HSubEnter string `parser:"| 'hsub' @Token"`
HSubFor string `parser:" 'for' @Token (NL|EOF)"`
VSubEnter string `parser:"| 'vsub' @Token"`
VSubFor string `parser:" 'for' @Token (NL|EOF)"`
Play *ActualPlay `parser:"| @@"`
Afters []*After `parser:" @@*"`
Comment string `parser:" @Comment? (NL|EOF)"`
Empty bool `parser:"| @NL"`
}

type After struct {
CourtesyRunner *string `parser:"'cr' @Code"`
CourtesyRunner *string `parser:"'cr' @Token"`
Conference *bool `parser:"| @'conf'"`
}

Expand All @@ -68,21 +68,21 @@ type Play interface {

type ActualPlay struct {
Pos Position
PlateAppearance Numbers `parser:"((@Numbers"`
Batter Numbers `parser:" @Numbers)"`
ContinuedPlateAppearance bool `parser:" | @Dots)"`
PitchSequence string `parser:" @Code"`
Code string `parser:" @Code"`
Advances []string `parser:" @Code*"`
ContinuedPlateAppearance bool `parser:"((@'...')"`
PlateAppearance Numbers `parser:" | (@PA"`
Batter string `parser:" @Token))"`
PitchSequence string `parser:" @Token"`
Code string `parser:" @Token"`
Advances []string `parser:" @Advance*"`
}

var _ Play = (*ActualPlay)(nil)

type Alternative struct {
Pos Position
Code string `parser:"@Code"`
Advances []string `parser:"@Code*"`
Comment string `parser:" @Text?"`
Code string `parser:"@Token"`
Advances []string `parser:"@Advance*"`
Comment string `parser:" @Comment?"`
}

var _ Play = (*Alternative)(nil)
Expand All @@ -105,6 +105,27 @@ func (f *File) Parse(r io.Reader) error {
return nil
}

func (p *ActualPlay) normalize() {
if p == nil {
return
}
for i, adv := range p.Advances {
p.Advances[i] = strings.ToUpper(adv)
}
p.Code = strings.ToUpper(p.Code)
p.PitchSequence = strings.ToUpper(p.PitchSequence)
}

func (a *Alternative) normalize() {
if a == nil {
return
}
for i, adv := range a.Advances {
a.Advances[i] = strings.ToUpper(adv)
}
a.Code = strings.ToUpper(a.Code)
}

func (f *File) Validate() error {
f.Properties = make(map[string]string)
f.PropertyPos = make(map[string]Position)
Expand All @@ -113,6 +134,11 @@ func (f *File) Validate() error {
f.PropertyPos[prop.Key] = prop.Pos
}
for _, te := range f.TeamEvents {
for _, event := range te.Events {
// make codes upper code
event.Play.normalize()
event.Alternative.normalize()
}
switch te.HomeOrVisitor {
case "homeplays":
if f.HomeEvents != nil {
Expand Down Expand Up @@ -186,16 +212,16 @@ func (f *File) writeEvents(w io.Writer, name string, events []*Event) {
} else {
pa += 1
}
fmt.Fprintf(w, "%d %s ", pa, play.Batter.String())
fmt.Fprintf(w, "%d %s ", pa, play.Batter)
} else {
fmt.Fprintf(w, " ... ")
}
fmt.Fprintf(w, "%s ", play.PitchSequence)
f.writeCodeAdvancesComment(w, play.Code, play.Advances, event.Comment)
f.writeCodeAdvancesComment(w, play.Code, play.Advances, event.Afters, event.Comment)
case event.Alternative != nil:
alt := event.Alternative
fmt.Fprintf(w, " alt")
f.writeCodeAdvancesComment(w, alt.Code, alt.Advances, alt.Comment)
f.writeCodeAdvancesComment(w, alt.Code, alt.Advances, nil, alt.Comment)
case event.Pitcher != "":
fmt.Fprintf(w, "pitching %s\n", event.Pitcher)
case event.RAdjBase != "":
Expand All @@ -209,11 +235,19 @@ func (f *File) writeEvents(w io.Writer, name string, events []*Event) {
fmt.Fprintln(w)
}

func (f *File) writeCodeAdvancesComment(w io.Writer, code string, advances []string, comment string) {
func (f *File) writeCodeAdvancesComment(w io.Writer, code string, advances []string, afters []*After, comment string) {
fmt.Fprintf(w, "%s", code)
for _, adv := range advances {
fmt.Fprintf(w, " %s", adv)
}
for _, aft := range afters {
if aft.Conference != nil {
fmt.Fprint(w, " conf")
}
if aft.CourtesyRunner != nil {
fmt.Fprintf(w, " cr %s", *aft.CourtesyRunner)
}
}
if comment != "" {
fmt.Fprintf(w, " : %s", comment)
}
Expand Down
Loading

0 comments on commit 885b567

Please sign in to comment.