From c33e320bda5230db1e2e971b266e662c15dbcabd Mon Sep 17 00:00:00 2001 From: Sam Shen Date: Wed, 27 Nov 2024 22:50:27 -0800 Subject: [PATCH] Various fixes * Data export handles tournaments with same name, different dates * fmt -i formats in-place --- cmd/dataexport.go | 11 +++---- cmd/fmt.go | 38 ++++++++++++++++++++-- cmd/root.go | 2 +- pkg/dataexport/dataexport.go | 4 +-- pkg/dataexport/tournament.go | 62 ++---------------------------------- 5 files changed, 44 insertions(+), 73 deletions(-) diff --git a/cmd/dataexport.go b/cmd/dataexport.go index 06cc5d1..c0a2723 100644 --- a/cmd/dataexport.go +++ b/cmd/dataexport.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "log" "github.com/slshen/paperscore/pkg/dataexport" "github.com/slshen/paperscore/pkg/dataframe/pkg" @@ -9,10 +10,9 @@ import ( "github.com/spf13/cobra" ) -func webdataCommand() *cobra.Command { +func dataExportCommand() *cobra.Command { var ( re reArgs - us string id string dir string gameDirs []string @@ -20,9 +20,6 @@ func webdataCommand() *cobra.Command { c := &cobra.Command{ Use: "data-export", RunE: func(cmd *cobra.Command, args []string) error { - if us == "" { - return fmt.Errorf("--us is required") - } if dir == "" { return fmt.Errorf("--dir is required") } @@ -37,7 +34,8 @@ func webdataCommand() *cobra.Command { if err != nil { return err } - exp := dataexport.NewDataExport(us, re) + log.Printf("read %d games\n", len(games)) + exp := dataexport.NewDataExport(re) dp, err := exp.Read(games) if err != nil { return err @@ -51,7 +49,6 @@ func webdataCommand() *cobra.Command { flags := c.Flags() re.registerFlags(flags) flags.StringVar(&id, "id", "", "The export ID") - flags.StringVar(&us, "us", "", "The us team") flags.StringVarP(&dir, "dir", "d", "", "Write web data to `dir`") flags.StringSliceVar(&gameDirs, "games", nil, "Read games from dir") return c diff --git a/cmd/fmt.go b/cmd/fmt.go index 7a7a0b6..fe956c1 100644 --- a/cmd/fmt.go +++ b/cmd/fmt.go @@ -1,23 +1,57 @@ package cmd import ( + "fmt" + "io" + "os" + "path" + "github.com/slshen/paperscore/pkg/game" "github.com/spf13/cobra" ) func fmtCommand() *cobra.Command { + var inplace bool c := &cobra.Command{ Use: "fmt", Short: "Format a game file", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - g, err := game.ReadGameFile(args[0]) + name := args[0] + newname := name + g, err := game.ReadGameFile(name) if err != nil { return err } - g.File.Write(cmd.OutOrStdout()) + var out io.Writer + if inplace { + if path.Ext(name) == ".yaml" { + newname = name[0:len(name)-5] + ".gm" + } + f, err := os.CreateTemp(path.Dir(name), fmt.Sprintf("%s*", path.Base(newname))) + if err != nil { + return err + } + out = f + } else { + out = cmd.OutOrStdout() + } + g.File.Write(out) + if inplace { + f := out.(*os.File) + if err := f.Close(); err != nil { + return err + } + if err := os.Rename(f.Name(), newname); err != nil { + return err + } + if name != newname { + return os.Remove(name) + } + } return nil }, } + c.Flags().BoolVarP(&inplace, "inplace", "i", false, "Convert file in place") return c } diff --git a/cmd/root.go b/cmd/root.go index 22cfed7..3b5fd88 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -21,7 +21,7 @@ func Root() *cobra.Command { root.AddCommand(readCommand(), boxCommand(), playByPlayCommand(), statsCommand("batting"), statsCommand("pitching"), reCommand(), tournamentCommand(), reAnalysisCommand(), - fmtCommand(), altCommand(), webdataCommand(), newGameCommand(), + fmtCommand(), altCommand(), dataExportCommand(), newGameCommand(), battingCountCommand(), battingTimesSeenPitcherCommand(), pitchingTimesSeenLineupCommand(), simCommand(), uiCommand(), diff --git a/pkg/dataexport/dataexport.go b/pkg/dataexport/dataexport.go index c948a29..ce85a54 100644 --- a/pkg/dataexport/dataexport.go +++ b/pkg/dataexport/dataexport.go @@ -13,13 +13,11 @@ import ( ) type DataExport struct { - us string re stats.RunExpectancy } -func NewDataExport(us string, re stats.RunExpectancy) *DataExport { +func NewDataExport(re stats.RunExpectancy) *DataExport { exp := &DataExport{ - us: us, re: re, } return exp diff --git a/pkg/dataexport/tournament.go b/pkg/dataexport/tournament.go index 3594e51..5453a37 100644 --- a/pkg/dataexport/tournament.go +++ b/pkg/dataexport/tournament.go @@ -1,6 +1,7 @@ package dataexport import ( + "fmt" "time" "github.com/slshen/paperscore/pkg/tournament" @@ -19,68 +20,9 @@ type Tournament struct { func newTournament(group *tournament.Group) *Tournament { return &Tournament{ - TournamentID: ToID(group.Name), + TournamentID: ToID(fmt.Sprintf("%s-%s", group.Date.Format("2006-01-02"), group.Name)), Name: group.Games[0].Tournament, Date: group.Date.Format(time.RFC3339), group: group, } } - -/* -func (t *Tournament) readGames(exp *DataExport, group *tournament.Group) error { - var idx *dataframe.Index - dat := &dataframe.Data{ - Columns: []*dataframe.Column{ - {Name: "Win", Values: dataframe.EmptyInts}, - {Name: "Loss", Values: dataframe.EmptyInts}, - {Name: "Ties", Values: dataframe.EmptyInts}, - }, - } - for _, g := range group.Games { - us, _ := g.GetUsAndThem(exp.us) - var win, loss, tie int - if us == g.Home { - switch { - case g.Final.Home > g.Final.Visitor: - win = 1 - case g.Final.Home == g.Final.Visitor: - tie = 1 - default: - loss = 1 - } - } else { - switch { - case g.Final.Visitor > g.Final.Home: - win = 1 - case g.Final.Visitor == g.Final.Home: - tie = 1 - default: - loss = 1 - } - } - dat.Columns[0].AppendInt(win) - dat.Columns[1].AppendInt(loss) - dat.Columns[2].AppendInt(tie) - if win == 1 { - t.Wins++ - } - if loss == 1 { - t.Losses++ - } - if tie == 1 { - t.Ties++ - } - game, err := newGame(exp, t.ID, g) - if err != nil { - return err - } - idx = dat.MustAppendStruct(idx, game) - } - exp.AddResource(&dataframe.DataResource{ - Description: fmt.Sprintf("Games for %s", t.Name), - Path: fmt.Sprintf("%s/games.csv", t.ID), - Data: dat, - }) - return nil -} -*/