feat(enakgame): weekly leaderboard
GET /customer/enakgame/leaderboard ranks the customers of an organization by the EnakCoin they won from games in one week, Monday to Sunday in Asia/Jakarta. Only completed, unflagged sessions count, by the week they started; entry cost is not subtracted. Equal totals rank by who reached theirs first, so no two customers share a rank. Optional date (any day of the week, this week by default), game_id and limit (10, at most 100). The response holds the top ranks and, under me, the customer asking wherever they rank. Other customers are shown by a masked name: the first word whole, the others by their first letter; a word with a digit keeps only its first character. The EnakGame web game shows it: integration-enakgame.md §10 (checklist now §11) and RFC §11. The DB test needs TEST_DATABASE_URL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
c43baa53e1
commit
3af16a1fbc
@@ -0,0 +1,98 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"apskel-pos-be/internal/models"
|
||||
"apskel-pos-be/internal/repository"
|
||||
)
|
||||
|
||||
const (
|
||||
// leaderboardLimit is how many ranks a leaderboard shows unless asked, and
|
||||
// leaderboardMaxLimit the most it shows.
|
||||
leaderboardLimit = 10
|
||||
leaderboardMaxLimit = 100
|
||||
)
|
||||
|
||||
// Leaderboard ranks the customers of the customer's organization by the EnakCoin they
|
||||
// won from games in one week, Monday to Sunday in Asia/Jakarta: this week, or the one
|
||||
// holding q.Date. A session counts in the week it started. Refunded, expired and
|
||||
// flagged sessions count for nothing.
|
||||
func (p *GameSessionProcessor) Leaderboard(ctx context.Context, customerID uuid.UUID, q models.EnakGameLeaderboardQuery) (*models.EnakGameLeaderboard, error) {
|
||||
day := walletDay(p.now())
|
||||
if s := strings.TrimSpace(q.Date); s != "" {
|
||||
d, err := time.ParseInLocation("2006-01-02", s, walletDisplayLocation)
|
||||
if err != nil {
|
||||
return nil, gameSessionRejected("date must be YYYY-MM-DD")
|
||||
}
|
||||
day = d
|
||||
}
|
||||
weekStart := leaderboardWeekStart(day)
|
||||
filter := repository.LeaderboardFilter{
|
||||
CustomerID: customerID, From: weekStart, To: weekStart.AddDate(0, 0, 7), Limit: q.Limit,
|
||||
}
|
||||
if filter.Limit < 1 || filter.Limit > leaderboardMaxLimit {
|
||||
filter.Limit = leaderboardLimit
|
||||
}
|
||||
if s := strings.TrimSpace(q.GameID); s != "" {
|
||||
id, err := uuid.Parse(s)
|
||||
if err != nil {
|
||||
return nil, gameSessionRejected("game_id must be a UUID")
|
||||
}
|
||||
filter.GameID = &id
|
||||
}
|
||||
customer, err := p.customers.GetCustomer(ctx, customerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filter.OrganizationID = customer.OrganizationID
|
||||
rows, err := p.sessions.Leaderboard(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
board := &models.EnakGameLeaderboard{
|
||||
WeekStart: weekStart.Format("2006-01-02"), WeekEnd: weekStart.AddDate(0, 0, 6).Format("2006-01-02"),
|
||||
GameID: filter.GameID, Entries: []models.EnakGameLeaderboardEntry{},
|
||||
}
|
||||
for _, row := range rows {
|
||||
entry := models.EnakGameLeaderboardEntry{
|
||||
Rank: row.Rank, Name: maskCustomerName(row.Name), Coins: row.Coins, IsMe: row.CustomerID == customerID,
|
||||
}
|
||||
if row.Rank <= filter.Limit {
|
||||
board.Entries = append(board.Entries, entry)
|
||||
}
|
||||
if entry.IsMe {
|
||||
me := entry
|
||||
board.Me = &me
|
||||
}
|
||||
}
|
||||
return board, nil
|
||||
}
|
||||
|
||||
// leaderboardWeekStart is the Monday, at midnight in Asia/Jakarta, of the week of day.
|
||||
func leaderboardWeekStart(day time.Time) time.Time {
|
||||
day = walletDay(day)
|
||||
return day.AddDate(0, 0, -((int(day.Weekday()) + 6) % 7))
|
||||
}
|
||||
|
||||
// maskCustomerName keeps the first word of a name and the first letter of every other
|
||||
// word: "Budi Santoso" is shown as "Budi S******". A word with a digit, as when a phone
|
||||
// number was used for the name, keeps only its first character, the first word too.
|
||||
func maskCustomerName(name string) string {
|
||||
words := strings.Fields(name)
|
||||
if len(words) == 0 {
|
||||
return "Pemain"
|
||||
}
|
||||
for i, w := range words {
|
||||
if i == 0 && !strings.ContainsAny(w, "0123456789") {
|
||||
continue
|
||||
}
|
||||
r := []rune(w)
|
||||
words[i] = string(r[0]) + strings.Repeat("*", len(r)-1)
|
||||
}
|
||||
return strings.Join(words, " ")
|
||||
}
|
||||
Reference in New Issue
Block a user