Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
* Data export handles tournaments with same name, different dates

* fmt -i formats in-place
  • Loading branch information
slshen committed Nov 28, 2024
1 parent 1bc5ad2 commit 279f471
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 71 deletions.
9 changes: 3 additions & 6 deletions cmd/dataexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"log"

"github.com/slshen/paperscore/pkg/dataexport"
"github.com/slshen/paperscore/pkg/dataframe/pkg"
Expand All @@ -12,17 +13,13 @@ import (
func webdataCommand() *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.Println(fmt.Sprintf("read %d games", len(games)))

Check failure on line 37 in cmd/dataexport.go

View workflow job for this annotation

GitHub Actions / build

S1038: should use log.Printf(...) instead of log.Println(fmt.Sprintf(...)) (gosimple)
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
}
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
}
*/

0 comments on commit 279f471

Please sign in to comment.