Skip to content

Commit

Permalink
[bgstats] added method to fix scores in early stats
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed Jan 23, 2024
1 parent a69ebca commit 90c757a
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 24 deletions.
48 changes: 24 additions & 24 deletions cmd/servus-bgstats/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ type BGPlay struct {
BGStats []BGStat
}

func getBGPlays(DB *sqlx.DB) ([]BGPlay, error) {
rs := []BGPlay{}
q := `
select
p.id,
g.boardgame_id,
l.location_id,
p.play_id,
p.play_date
from
tbgstatsplays p,
tbgstatsgames g,
tbgstatslocations l
where
p.game_id = g.id and
p.location_id = l.id
`
err := DB.Select(&rs, q)
if err != nil {
return nil, err
}

return rs, nil
}
// func getBGPlays(DB *sqlx.DB) ([]BGPlay, error) {
// rs := []BGPlay{}
// q := `
// select
// p.id,
// g.boardgame_id,
// l.location_id,
// p.play_id,
// p.play_date
// from
// tbgstatsplays p,
// tbgstatsgames g,
// tbgstatslocations l
// where
// p.game_id = g.id and
// p.location_id = l.id
// `
// err := DB.Select(&rs, q)
// if err != nil {
// return nil, err
// }

// return rs, nil
// }

func getMappedBGPlays(DB *sqlx.DB) ([]BGPlay, error) {
rs := []BGPlay{}
Expand Down
4 changes: 4 additions & 0 deletions cmd/servus-bgstats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func main() {
Name: "cooperative",
Action: cooperative,
},
{
Name: "score",
Action: score,
},
},
}

Expand Down
109 changes: 109 additions & 0 deletions cmd/servus-bgstats/score.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

import (
"log"

"github.com/DictumMortuum/servus-extapi/pkg/db"
"github.com/DictumMortuum/servus/pkg/models"
"github.com/jmoiron/sqlx"
"github.com/urfave/cli/v2"
)

func updateScore(DB *sqlx.DB, id int64, score float64) error {
q := `
update
tboardgamestats
set
data = JSON_SET(
IFNULL(data, JSON_OBJECT()),'$.score', :score
)
where
id = :id
`
_, err := DB.NamedExec(q, map[string]any{
"id": id,
"score": score,
})
if err != nil {
return err
}

return nil
}

func score(c *cli.Context) error {
DB, err := db.DatabaseX()
if err != nil {
return err
}
defer DB.Close()

type stats struct {
Id int64 `json:"id,omitempty"`
Data models.Json `json:"data,omitempty"`
}

// q := `
// select
// s.id,
// s.data
// from
// tboardgamestats s
// where
// json_extract(s.data, '$.score') is null
// `

q := `
select
s.id,
s.data
from
tboardgamestats s
where
json_extract(s.data, '$.score') = 1 and json_extract(s.data, '$.winner') = "false"
`

rs := []stats{}
err = DB.Select(&rs, q)
if err != nil {
return err
}

for _, item := range rs {
score := 0.0
flag := true
// for _, val := range item.Data {
// switch val := val.(type) {
// case int:
// case float64:
// {
// flag = true
// score += val
// }
// case bool:
// {
// flag = true
// if val {
// score = 1
// } else {
// score = 0
// }
// }
// default:
// {
// // log.Println(val, "not int")
// }
// }
// }

if flag {
log.Println(item.Data, score)
err := updateScore(DB, item.Id, 0)
if err != nil {
return err
}
}
}

return nil
}

0 comments on commit 90c757a

Please sign in to comment.