You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

416 lines
9.9 KiB
Go

package valueobjects
import (
"fmt"
"github.com/knowfoolery/backend/shared/errors"
)
type SortOrder string
const (
SortOrderAsc SortOrder = "asc"
SortOrderDesc SortOrder = "desc"
)
func (so SortOrder) IsValid() bool {
switch so {
case SortOrderAsc, SortOrderDesc:
return true
default:
return false
}
}
type DisplayFormat string
const (
DisplayFormatTable DisplayFormat = "table"
DisplayFormatCards DisplayFormat = "cards"
DisplayFormatChart DisplayFormat = "chart"
DisplayFormatMinimal DisplayFormat = "minimal"
)
func (df DisplayFormat) IsValid() bool {
switch df {
case DisplayFormatTable, DisplayFormatCards, DisplayFormatChart, DisplayFormatMinimal:
return true
default:
return false
}
}
type RankDisplay string
const (
RankDisplayNumeric RankDisplay = "numeric"
RankDisplayTrophies RankDisplay = "trophies"
RankDisplayMedals RankDisplay = "medals"
RankDisplayCustom RankDisplay = "custom"
)
func (rd RankDisplay) IsValid() bool {
switch rd {
case RankDisplayNumeric, RankDisplayTrophies, RankDisplayMedals, RankDisplayCustom:
return true
default:
return false
}
}
type DisplaySettings struct {
format DisplayFormat
sortOrder SortOrder
rankDisplay RankDisplay
showPlayerNames bool
showScores bool
showRanks bool
showAvatars bool
showBadges bool
showStatistics bool
showTimestamps bool
entriesPerPage int32
animateChanges bool
highlightTop int32
customColors map[string]string
customLabels map[string]string
metadata map[string]interface{}
}
func NewDisplaySettings() *DisplaySettings {
return &DisplaySettings{
format: DisplayFormatTable,
sortOrder: SortOrderDesc,
rankDisplay: RankDisplayNumeric,
showPlayerNames: true,
showScores: true,
showRanks: true,
showAvatars: false,
showBadges: false,
showStatistics: false,
showTimestamps: false,
entriesPerPage: 10,
animateChanges: true,
highlightTop: 3,
customColors: make(map[string]string),
customLabels: make(map[string]string),
metadata: make(map[string]interface{}),
}
}
func (ds *DisplaySettings) Format() DisplayFormat {
return ds.format
}
func (ds *DisplaySettings) SortOrder() SortOrder {
return ds.sortOrder
}
func (ds *DisplaySettings) RankDisplay() RankDisplay {
return ds.rankDisplay
}
func (ds *DisplaySettings) ShowPlayerNames() bool {
return ds.showPlayerNames
}
func (ds *DisplaySettings) ShowScores() bool {
return ds.showScores
}
func (ds *DisplaySettings) ShowRanks() bool {
return ds.showRanks
}
func (ds *DisplaySettings) ShowAvatars() bool {
return ds.showAvatars
}
func (ds *DisplaySettings) ShowBadges() bool {
return ds.showBadges
}
func (ds *DisplaySettings) ShowStatistics() bool {
return ds.showStatistics
}
func (ds *DisplaySettings) ShowTimestamps() bool {
return ds.showTimestamps
}
func (ds *DisplaySettings) EntriesPerPage() int32 {
return ds.entriesPerPage
}
func (ds *DisplaySettings) AnimateChanges() bool {
return ds.animateChanges
}
func (ds *DisplaySettings) HighlightTop() int32 {
return ds.highlightTop
}
func (ds *DisplaySettings) CustomColors() map[string]string {
result := make(map[string]string)
for k, v := range ds.customColors {
result[k] = v
}
return result
}
func (ds *DisplaySettings) CustomLabels() map[string]string {
result := make(map[string]string)
for k, v := range ds.customLabels {
result[k] = v
}
return result
}
func (ds *DisplaySettings) Metadata() map[string]interface{} {
result := make(map[string]interface{})
for k, v := range ds.metadata {
result[k] = v
}
return result
}
func (ds *DisplaySettings) SetFormat(format DisplayFormat) error {
if !format.IsValid() {
return errors.ErrInvalidDisplayFormat
}
ds.format = format
return nil
}
func (ds *DisplaySettings) SetSortOrder(order SortOrder) error {
if !order.IsValid() {
return errors.ErrInvalidSortOrder
}
ds.sortOrder = order
return nil
}
func (ds *DisplaySettings) SetRankDisplay(display RankDisplay) error {
if !display.IsValid() {
return errors.ErrInvalidRankDisplay
}
ds.rankDisplay = display
return nil
}
func (ds *DisplaySettings) SetShowPlayerNames(show bool) {
ds.showPlayerNames = show
}
func (ds *DisplaySettings) SetShowScores(show bool) {
ds.showScores = show
}
func (ds *DisplaySettings) SetShowRanks(show bool) {
ds.showRanks = show
}
func (ds *DisplaySettings) SetShowAvatars(show bool) {
ds.showAvatars = show
}
func (ds *DisplaySettings) SetShowBadges(show bool) {
ds.showBadges = show
}
func (ds *DisplaySettings) SetShowStatistics(show bool) {
ds.showStatistics = show
}
func (ds *DisplaySettings) SetShowTimestamps(show bool) {
ds.showTimestamps = show
}
func (ds *DisplaySettings) SetEntriesPerPage(entries int32) error {
if entries <= 0 || entries > 100 {
return errors.ErrInvalidEntriesPerPage
}
ds.entriesPerPage = entries
return nil
}
func (ds *DisplaySettings) SetAnimateChanges(animate bool) {
ds.animateChanges = animate
}
func (ds *DisplaySettings) SetHighlightTop(top int32) error {
if top < 0 || top > 10 {
return errors.ErrInvalidHighlightCount
}
ds.highlightTop = top
return nil
}
func (ds *DisplaySettings) SetCustomColor(key, color string) error {
if key == "" {
return errors.ErrInvalidColorKey
}
if color == "" {
return errors.ErrInvalidColorValue
}
ds.customColors[key] = color
return nil
}
func (ds *DisplaySettings) RemoveCustomColor(key string) {
delete(ds.customColors, key)
}
func (ds *DisplaySettings) SetCustomLabel(key, label string) error {
if key == "" {
return errors.ErrInvalidLabelKey
}
if label == "" {
return errors.ErrInvalidLabelValue
}
ds.customLabels[key] = label
return nil
}
func (ds *DisplaySettings) RemoveCustomLabel(key string) {
delete(ds.customLabels, key)
}
func (ds *DisplaySettings) SetMetadata(key string, value interface{}) error {
if key == "" {
return errors.ErrInvalidMetadata
}
ds.metadata[key] = value
return nil
}
func (ds *DisplaySettings) RemoveMetadata(key string) {
delete(ds.metadata, key)
}
func (ds *DisplaySettings) GetCustomColor(key string) (string, bool) {
color, exists := ds.customColors[key]
return color, exists
}
func (ds *DisplaySettings) GetCustomLabel(key string) (string, bool) {
label, exists := ds.customLabels[key]
return label, exists
}
func (ds *DisplaySettings) IsMinimalView() bool {
return ds.format == DisplayFormatMinimal
}
func (ds *DisplaySettings) IsChartView() bool {
return ds.format == DisplayFormatChart
}
func (ds *DisplaySettings) ShouldShowElement(element string) bool {
switch element {
case "names":
return ds.showPlayerNames
case "scores":
return ds.showScores
case "ranks":
return ds.showRanks
case "avatars":
return ds.showAvatars
case "badges":
return ds.showBadges
case "statistics":
return ds.showStatistics
case "timestamps":
return ds.showTimestamps
default:
return false
}
}
func (ds *DisplaySettings) String() string {
return fmt.Sprintf("DisplaySettings{Format: %s, SortOrder: %s, RankDisplay: %s, EntriesPerPage: %d}",
ds.format, ds.sortOrder, ds.rankDisplay, ds.entriesPerPage)
}
type DisplaySettingsSnapshot struct {
Format DisplayFormat `json:"format"`
SortOrder SortOrder `json:"sort_order"`
RankDisplay RankDisplay `json:"rank_display"`
ShowPlayerNames bool `json:"show_player_names"`
ShowScores bool `json:"show_scores"`
ShowRanks bool `json:"show_ranks"`
ShowAvatars bool `json:"show_avatars"`
ShowBadges bool `json:"show_badges"`
ShowStatistics bool `json:"show_statistics"`
ShowTimestamps bool `json:"show_timestamps"`
EntriesPerPage int32 `json:"entries_per_page"`
AnimateChanges bool `json:"animate_changes"`
HighlightTop int32 `json:"highlight_top"`
CustomColors map[string]string `json:"custom_colors"`
CustomLabels map[string]string `json:"custom_labels"`
Metadata map[string]interface{} `json:"metadata"`
}
func (ds *DisplaySettings) ToSnapshot() DisplaySettingsSnapshot {
return DisplaySettingsSnapshot{
Format: ds.format,
SortOrder: ds.sortOrder,
RankDisplay: ds.rankDisplay,
ShowPlayerNames: ds.showPlayerNames,
ShowScores: ds.showScores,
ShowRanks: ds.showRanks,
ShowAvatars: ds.showAvatars,
ShowBadges: ds.showBadges,
ShowStatistics: ds.showStatistics,
ShowTimestamps: ds.showTimestamps,
EntriesPerPage: ds.entriesPerPage,
AnimateChanges: ds.animateChanges,
HighlightTop: ds.highlightTop,
CustomColors: ds.CustomColors(),
CustomLabels: ds.CustomLabels(),
Metadata: ds.Metadata(),
}
}
func (ds *DisplaySettings) FromSnapshot(snapshot DisplaySettingsSnapshot) error {
if !snapshot.Format.IsValid() {
return errors.ErrInvalidDisplayFormat
}
if !snapshot.SortOrder.IsValid() {
return errors.ErrInvalidSortOrder
}
if !snapshot.RankDisplay.IsValid() {
return errors.ErrInvalidRankDisplay
}
ds.format = snapshot.Format
ds.sortOrder = snapshot.SortOrder
ds.rankDisplay = snapshot.RankDisplay
ds.showPlayerNames = snapshot.ShowPlayerNames
ds.showScores = snapshot.ShowScores
ds.showRanks = snapshot.ShowRanks
ds.showAvatars = snapshot.ShowAvatars
ds.showBadges = snapshot.ShowBadges
ds.showStatistics = snapshot.ShowStatistics
ds.showTimestamps = snapshot.ShowTimestamps
ds.entriesPerPage = snapshot.EntriesPerPage
ds.animateChanges = snapshot.AnimateChanges
ds.highlightTop = snapshot.HighlightTop
ds.customColors = make(map[string]string)
for k, v := range snapshot.CustomColors {
ds.customColors[k] = v
}
ds.customLabels = make(map[string]string)
for k, v := range snapshot.CustomLabels {
ds.customLabels[k] = v
}
ds.metadata = make(map[string]interface{})
for k, v := range snapshot.Metadata {
ds.metadata[k] = v
}
return nil
}