Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions app/v1/country.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1

import (
"strings"

"github.com/osuAkatsuki/akatsuki-api/common"
)

Expand All @@ -14,10 +16,8 @@ type MultiCount struct {
Countries []Country `json:"countries"`
}

func CountriesGET(md common.MethodData) common.CodeMessager {
var r MultiCount

r.Countries = []Country{
func allCountries() []Country {
return []Country{
{
Name: "Andorra",
Code: "AD",
Expand Down Expand Up @@ -1023,7 +1023,28 @@ func CountriesGET(md common.MethodData) common.CodeMessager {
Code: "XX",
},
}
}

var countryNames = func() map[string]string {
countries := allCountries()
names := make(map[string]string, len(countries))
for _, country := range countries {
names[country.Code] = country.Name
}
return names
}()

func countryName(countryCode string) string {
if name, ok := countryNames[strings.ToUpper(countryCode)]; ok {
return name
}
return "Unknown country"
}

func CountriesGET(md common.MethodData) common.CodeMessager {
var r MultiCount

r.Countries = allCountries()
r.Code = 200
return r
}
36 changes: 22 additions & 14 deletions app/v1/self.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ type userSettingsData struct {
singleBadge
Show *bool `json:"show"`
} `json:"custom_badge"`
PlayStyle *int `json:"play_style"`
VanillaPPLeaderboards *bool `json:"vanilla_pp_leaderboards"`
LeaderboardSize *int `json:"leaderboard_size"`
PlayStyle *int `json:"play_style"`
VanillaPPLeaderboards *bool `json:"vanilla_pp_leaderboards"`
LeaderboardSize *int `json:"leaderboard_size"`
UserTitle *string `json:"user_title"`
}

Expand Down Expand Up @@ -145,11 +145,11 @@ type userTitleResponse struct {

type userSettingsResponse struct {
common.ResponseBase
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
UserTitle userTitleResponse `json:"user_title"`
EligibleTitles []eligibleTitle `json:"eligible_titles"`
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
UserTitle userTitleResponse `json:"user_title"`
EligibleTitles []eligibleTitle `json:"eligible_titles"`
userSettingsData
}

Expand All @@ -159,10 +159,6 @@ type userSettingsResponse struct {
// - Badge-based titles: Check if user has specific badges by ID
// - Titles are returned in a specific priority order (to accomodate default title selection)
func getEligibleTitles(md common.MethodData, userID int, privileges uint64) ([]eligibleTitle, error) {
titles := make([]eligibleTitle, 0)

userPrivs := common.UserPrivileges(privileges)

// Check badges first (they have higher priority)
rows, err := md.DB.Query("SELECT b.id FROM user_badges ub "+
"INNER JOIN badges b ON ub.badge = b.id WHERE user = ?", userID)
Expand Down Expand Up @@ -200,7 +196,19 @@ func getEligibleTitles(md common.MethodData, userID int, privileges uint64) ([]e
}
}

// Return titles in priority order as specified in the HTML template
return getEligibleTitlesFromFlags(privileges, hasBot, hasDesign, hasScorewatcher, hasChampion), nil
}

func getEligibleTitlesFromFlags(
privileges uint64,
hasBot bool,
hasDesign bool,
hasScorewatcher bool,
hasChampion bool,
) []eligibleTitle {
titles := make([]eligibleTitle, 0)
userPrivs := common.UserPrivileges(privileges)

if hasBot {
titles = append(titles, eligibleTitle{ID: "bot", Title: "CHAT BOT"})
}
Expand Down Expand Up @@ -253,7 +261,7 @@ func getEligibleTitles(md common.MethodData, userID int, privileges uint64) ([]e
titles = append(titles, eligibleTitle{ID: "donor", Title: "SUPPORTER"})
}

return titles, nil
return titles
}

// getTitleFromID converts a machine-readable title ID to human-readable title
Expand Down
Loading