You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.7 KiB
78 lines
1.7 KiB
package catalog
|
|
|
|
import "slices"
|
|
|
|
// Game describes an installed, internally published version. The registry
|
|
// never imports manifests from a player request or the legacy application.
|
|
type Game struct {
|
|
ID string
|
|
Version string
|
|
Title string
|
|
Summary string
|
|
// DefaultLocale labels Title and Summary; Translations holds additional copies.
|
|
DefaultLocale string
|
|
Translations map[string]Translation
|
|
MinPlayers int
|
|
MaxPlayers int
|
|
Published bool
|
|
}
|
|
|
|
type Translation struct {
|
|
Title string
|
|
Summary string
|
|
}
|
|
|
|
type Registry struct {
|
|
games []Game
|
|
}
|
|
|
|
func NewRegistry(games []Game) *Registry {
|
|
copyGames := slices.Clone(games)
|
|
for i := range copyGames {
|
|
copyGames[i].Translations = make(map[string]Translation, len(games[i].Translations))
|
|
for locale, copy := range games[i].Translations {
|
|
copyGames[i].Translations[locale] = copy
|
|
}
|
|
}
|
|
return &Registry{games: copyGames}
|
|
}
|
|
|
|
func (g Game) Text(locale string) (Translation, string) {
|
|
defaultLocale := g.DefaultLocale
|
|
if defaultLocale == "" {
|
|
defaultLocale = "es"
|
|
}
|
|
if locale == defaultLocale {
|
|
return Translation{Title: g.Title, Summary: g.Summary}, defaultLocale
|
|
}
|
|
if translation, ok := g.Translations[locale]; ok && translation.Title != "" && translation.Summary != "" {
|
|
return translation, locale
|
|
}
|
|
return Translation{Title: g.Title, Summary: g.Summary}, defaultLocale
|
|
}
|
|
|
|
func (r *Registry) Published() []Game {
|
|
result := make([]Game, 0)
|
|
for _, game := range r.games {
|
|
if game.Published {
|
|
result = append(result, game)
|
|
}
|
|
}
|
|
slices.SortFunc(result, func(a, b Game) int {
|
|
if a.Title < b.Title {
|
|
return -1
|
|
}
|
|
if a.Title > b.Title {
|
|
return 1
|
|
}
|
|
if a.ID < b.ID {
|
|
return -1
|
|
}
|
|
if a.ID > b.ID {
|
|
return 1
|
|
}
|
|
return 0
|
|
})
|
|
return result
|
|
}
|