Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes #53

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions cmd/dataexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@ package cmd

import (
"fmt"
"log"

"github.com/slshen/paperscore/pkg/dataexport"
"github.com/slshen/paperscore/pkg/dataframe/pkg"
"github.com/slshen/paperscore/pkg/game"
"github.com/spf13/cobra"
)

func webdataCommand() *cobra.Command {
func dataExportCommand() *cobra.Command {
var (
re reArgs
us string
id string
dir string
gameDirs []string
)
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")
}
Expand All @@ -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
Expand All @@ -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
Expand Down
38 changes: 36 additions & 2 deletions cmd/fmt.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 1 addition & 3 deletions pkg/dataexport/dataexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 2 additions & 60 deletions pkg/dataexport/tournament.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dataexport

import (
"fmt"
"time"

"github.com/slshen/paperscore/pkg/tournament"
Expand All @@ -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
}
*/
Loading