Skip to content

Commit

Permalink
[player] improved distinct games, added games not played within the year
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed Dec 6, 2023
1 parent 6aad9ed commit d610d27
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
62 changes: 62 additions & 0 deletions cmd/servus-player/distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,65 @@ func GetDistinctGames(req *model.Map, res *model.Map) error {

return nil
}

func GetOldDistinctGames(req *model.Map, res *model.Map) error {
id, err := req.GetInt64("id")
if err != nil {
return err
}

rs := []DistinctBoardgame{}
yearFlag, err := req.GetBool("year_flag")
if err != nil {
return err
}

if !yearFlag {
res.Set("old_distinct", rs)
return nil
}

url, err := req.GetString("url")
if err != nil {
return err
}

DB, err := req.GetDB()
if err != nil {
return err
}

RDB, err := req.GetRedis()
if err != nil {
return err
}

err = db.CachedSelect(DB, RDB, "GetOldDistinctGames"+url, &rs, fmt.Sprintf(`
select
g.id,
g.name,
g.square200,
max(p.date) date
from
tboardgames g,
tboardgamestats s,
tboardgameplays p
where
g.id = s.boardgame_id and
s.play_id = p.id and
s.player_id = ?
group by
1
having
%s
order by
date desc
`, db.YearHavingConstraint(req)), id)
if err != nil {
return err
}

res.Set("old_distinct", rs)

return nil
}
3 changes: 2 additions & 1 deletion cmd/servus-player/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func Version(c *gin.Context) {
rs := map[string]any{
"version": "v0.0.15",
"version": "v0.0.16",
}
c.AbortWithStatusJSON(200, rs)
}
Expand Down Expand Up @@ -66,6 +66,7 @@ func main() {
middleware.BindN,
adapter.A(GetPlayerDetail),
adapter.A(GetDistinctGames),
adapter.A(GetOldDistinctGames),
middleware.Result,
)

Expand Down
14 changes: 14 additions & 0 deletions pkg/db/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ func YearConstraint(req *model.Map, start string) string {
return q
}

func YearHavingConstraint(req *model.Map) string {
q := ""

yearFlag, err := req.GetBool("year_flag")
if err == nil && yearFlag {
year, err := req.GetInt64("year")
if err == nil && year != 0 {
q = fmt.Sprintf("MAX(date) < '%d-01-01'", year)
}
}

return q
}

func Limit(req *model.Map, n int64) string {
if n == -1 {
return ""
Expand Down

0 comments on commit d610d27

Please sign in to comment.