-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
408 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TABLE IF NOT EXISTS gamification ( | ||
id INTEGER PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
score INTEGER NOT NULL, | ||
avatar VARCHAR(255) | ||
); | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP TABLE IF EXISTS gamification; | ||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-- CRUD | ||
|
||
-- name: GetAllGamification :many | ||
SELECT * | ||
FROM gamification; | ||
|
||
-- name: CreateGamification :one | ||
INSERT INTO gamification (name, score, avatar) | ||
VALUES (?, ?, ?) | ||
RETURNING *; | ||
|
||
-- name: DeleteGamification :execrows | ||
DELETE FROM gamification | ||
WHERE id = ?; | ||
|
||
|
||
-- Other | ||
|
||
|
||
-- name: UpdateGamificationScore :one | ||
UPDATE gamification | ||
SET score = ? | ||
WHERE id = ? | ||
RETURNING *; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package cmd | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/zeusWPI/scc/internal/pkg/db" | ||
"github.com/zeusWPI/scc/internal/pkg/gamification" | ||
"github.com/zeusWPI/scc/pkg/config" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Gamification starts the gamification instance | ||
func Gamification(db *db.DB) (*gamification.Gamification, chan bool) { | ||
gam := gamification.New(db) | ||
done := make(chan bool) | ||
|
||
go gamificationPeriodicUpdate(gam, done) | ||
|
||
return gam, done | ||
} | ||
|
||
func gamificationPeriodicUpdate(gam *gamification.Gamification, done chan bool) { | ||
interval := config.GetDefaultInt("gamification.interval_s", 3600) | ||
zap.S().Info("Gamification: Starting periodic leaderboard update with an interval of ", interval, " seconds") | ||
|
||
ticker := time.NewTicker(time.Duration(interval) * time.Second) | ||
defer ticker.Stop() | ||
|
||
// Run immediatly once | ||
zap.S().Info("Gamification: Updating leaderboard") | ||
err := gam.Update() | ||
if err != nil { | ||
zap.S().Error("gamification: Error updating leaderboard\n", err) | ||
} | ||
|
||
for { | ||
select { | ||
case <-done: | ||
zap.S().Info("Gamification: Stopping periodic leaderboard update") | ||
return | ||
case <-ticker.C: | ||
// Update leaderboard | ||
zap.S().Info("Gamification: Updating leaderboard") | ||
err := gam.Update() | ||
if err != nil { | ||
zap.S().Error("gamification: Error updating leaderboard\n", err) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dto | ||
|
||
import "github.com/zeusWPI/scc/internal/pkg/db/sqlc" | ||
|
||
// Gamification represents the DTO object for gamification | ||
type Gamification struct { | ||
ID int64 `json:"id"` | ||
Name string `json:"github_name"` | ||
Score int64 `json:"score"` | ||
Avatar string `json:"avatar_url"` | ||
} | ||
|
||
// GamificationDTO converts a sqlc Gamification object to a DTO gamification | ||
func GamificationDTO(gam sqlc.Gamification) *Gamification { | ||
return &Gamification{ | ||
ID: gam.ID, | ||
Name: gam.Name, | ||
Score: gam.Score, | ||
Avatar: gam.Avatar, | ||
} | ||
} | ||
|
||
// CreateParams converts a Gamification DTO to a sqlc CreateGamificationParams object | ||
func (g *Gamification) CreateParams() sqlc.CreateGamificationParams { | ||
return sqlc.CreateGamificationParams{ | ||
Name: g.Name, | ||
Score: g.Score, | ||
Avatar: g.Avatar, | ||
} | ||
} | ||
|
||
// UpdateScoreParams converts a Gamification DTO to a sqlc UpdateScoreParams object | ||
func (g *Gamification) UpdateScoreParams() sqlc.UpdateGamificationScoreParams { | ||
return sqlc.UpdateGamificationScoreParams{ | ||
ID: g.ID, | ||
Score: g.Score, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package gamification | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/zeusWPI/scc/internal/pkg/db/dto" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (g *Gamification) getLeaderboard() (*[]*dto.Gamification, error) { | ||
zap.S().Info("Gamification: Getting leaderboard") | ||
|
||
req := fiber.Get(g.api+"/top4").Set("Accept", "application/json") | ||
|
||
res := new([]*dto.Gamification) | ||
status, _, errs := req.Struct(res) | ||
if len(errs) > 0 { | ||
return nil, errors.Join(append(errs, errors.New("Gamification: Leaderboard API request failed"))...) | ||
} | ||
if status != fiber.StatusOK { | ||
return nil, fmt.Errorf("Gamification: Leaderboard API request returned bad status code %d", status) | ||
} | ||
|
||
errs = make([]error, 0) | ||
for _, gam := range *res { | ||
if err := dto.Validate.Struct(gam); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
return res, errors.Join(errs...) | ||
} | ||
|
||
func downloadAvatar(gam dto.Gamification) (string, error) { | ||
req := fiber.Get(gam.Avatar) | ||
status, body, errs := req.Bytes() | ||
if errs != nil { | ||
return "", errors.Join(append(errs, errors.New("Gamification: Download avatar request failed"))...) | ||
} | ||
if status != fiber.StatusOK { | ||
return "", fmt.Errorf("Gamification: Download avatar returned bad status code %d", status) | ||
} | ||
|
||
location := fmt.Sprintf("public/%s.png", gam.Name) | ||
out, err := os.Create(location) | ||
if err != nil && err != os.ErrExist { | ||
return "", err | ||
} | ||
defer func() { | ||
_ = out.Close() | ||
}() | ||
|
||
_, err = io.Copy(out, bytes.NewReader(body)) | ||
|
||
return location, err | ||
} |
Oops, something went wrong.