Skip to content

Commit

Permalink
move lb to the db
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Whitacre committed May 19, 2024
1 parent 985cd22 commit 4bc6ad4
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 34 deletions.
1 change: 0 additions & 1 deletion api/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
*
.*

!leaderboard
!player
!go.mod
!go.sum
Expand Down
1 change: 0 additions & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ FROM gcr.io/distroless/base-debian12

WORKDIR /
COPY --from=build /hdstmevents-api /hdstmevents-api
COPY ./leaderboard /leaderboard
COPY ./player /player

EXPOSE 8080
Expand Down
77 changes: 61 additions & 16 deletions api/domain/leaderboard.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package domain

import (
"encoding/json"
"context"
"errors"
"os"
"slices"
"strings"

"github.com/jackc/pgx/v5"
)

type Leaderboard struct {
Expand All @@ -26,43 +27,87 @@ type Top struct {
Position int `json:"position"`
}

func LeaderboardGet(leaderboard *Leaderboard) error {
if leaderboard.LeaderboardId == "" {
return errors.New("LeaderboardGet: missing leaderboardId, nothing to gets")
}
type LeaderboardData struct {
LeaderboardId string
LastModified string
WeeklyId string
}

file, err := os.Open("leaderboard/" + leaderboard.LeaderboardId)
func getLeaderboardData(leaderboardId string) ([]LeaderboardData, error) {
var leaderboardData []LeaderboardData

rows, err := db.Query(
context.Background(),
`select l.LeaderboardId, l.LastModified, lw.WeeklyId
from Leaderboard l
join LeaderboardWeekly lw on l.leaderboardId = lw.LeaderboardId
where l.LeaderboardId=$1`,
leaderboardId,
)
if err != nil {
return err
return leaderboardData, err
}
defer file.Close()

jsonParser := json.NewDecoder(file)
if err = jsonParser.Decode(leaderboard); err != nil {
return err
leaderboardData, err = pgx.CollectRows(rows, pgx.RowToStructByName[LeaderboardData])
if err != nil {
return leaderboardData, err
}

return leaderboardData, nil
}

func toLeaderboard(leaderboardData []LeaderboardData, leaderboard *Leaderboard) error {
if len(leaderboardData) < 1 {
return errors.New("LeaderboardGet: no leaderboardData to create leaderboard from")
}

for i := 0; i < len(leaderboard.Weeklies); i++ {
err = WeeklyGet(leaderboard.Weeklies[i].Weekly)
leaderboard.LeaderboardId = leaderboardData[0].LeaderboardId
leaderboard.LastModified = leaderboardData[0].LastModified

for i := 0; i < len(leaderboardData); i++ {
var weekly Weekly
weekly.WeeklyId = leaderboardData[i].WeeklyId
err := WeeklyGet(&weekly)
if err != nil {
return err
}

var leaderboardWeekly LeaderboardWeekly
leaderboardWeekly.Weekly = &weekly
leaderboard.Weeklies = append(leaderboard.Weeklies, &leaderboardWeekly)

for j := 0; j < len(leaderboard.Weeklies[i].Weekly.Results); j++ {
idx := slices.IndexFunc(leaderboard.Tops, func (top *Top) bool {
return top.Player.AccountId == leaderboard.Weeklies[i].Weekly.Results[j].Player.AccountId
})
if idx == -1 {
top := &Top{}
var top Top
top.Player = leaderboard.Weeklies[i].Weekly.Results[j].Player
idx = len(leaderboard.Tops)
leaderboard.Tops = append(leaderboard.Tops, top)
leaderboard.Tops = append(leaderboard.Tops, &top)
}
leaderboard.Tops[idx].Score += leaderboard.Weeklies[i].Weekly.Results[j].Score
}
}

return nil
}

func LeaderboardGet(leaderboard *Leaderboard) error {
if leaderboard.LeaderboardId == "" {
return errors.New("LeaderboardGet: missing leaderboardId, nothing to gets")
}

leaderboardData, err := getLeaderboardData(leaderboard.LeaderboardId)
if err != nil {
return err
}

err = toLeaderboard(leaderboardData, leaderboard)
if err != nil {
return err
}

slices.SortFunc(leaderboard.Tops, func (topA *Top, topB *Top) int {
if topB.Score == topA.Score {
return strings.Compare(strings.ToLower(topA.Player.Name), strings.ToLower(topB.Player.Name))
Expand Down
16 changes: 0 additions & 16 deletions api/leaderboard/standings

This file was deleted.

17 changes: 17 additions & 0 deletions db/007_create_leaderboard.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
create table Leaderboard(
LeaderboardId varchar not null primary key,
LastModified varchar not null default ''
);

create table LeaderboardWeekly(
LeaderboardWeeklyId serial primary key,
LeaderboardId varchar not null,
WeeklyId varchar not null,
foreign key(LeaderboardId) references Leaderboard(LeaderboardId),
foreign key(WeeklyId) references Weekly(WeeklyId)
);

---- create above / drop below ----

drop table LeaderboardWeekly;
drop table Leaderboard;
15 changes: 15 additions & 0 deletions db/008_seed_leaderboard.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
insert into Leaderboard (LeaderboardId, LastModified)
values
('standings', '2024-05-17T01:10:00+00:00');

insert into LeaderboardWeekly (LeaderboardId, WeeklyId)
values
('standings', '2024-04-27'),
('standings', '2024-05-04'),
('standings', '2024-05-11'),
('standings', '2024-05-17');

---- create above / drop below ----

delete from LeaderboardWeekly;
delete from Leaderboard;

0 comments on commit 4bc6ad4

Please sign in to comment.