diff --git a/server/src/game/getQuotesInfoByUser.go b/server/src/game/getQuotesInfoByUser.go index 297549a..19302ed 100644 --- a/server/src/game/getQuotesInfoByUser.go +++ b/server/src/game/getQuotesInfoByUser.go @@ -1,6 +1,8 @@ package game import ( + "sort" + "sirlab.de/go/knowyt/user" ) @@ -9,6 +11,12 @@ func (gm *Game) GetQuotesInfoByUser(usr *user.User) *QuotesInfo { return &QuotesInfo{Quotes: gm.getQuotesInfoByUserId(usrId)} } +type ByCreated []Quote + +func (quotes ByCreated) Len() int { return len(quotes) } +func (quotes ByCreated) Less(a, b int) bool { return quotes[a].Created < quotes[b].Created } +func (quotes ByCreated) Swap(a, b int) { quotes[a], quotes[b] = quotes[b], quotes[a] } + func (gm *Game) getQuotesInfoByUserId(usrId string) []Quote { gm.mu.Lock() defer gm.mu.Unlock() @@ -17,11 +25,13 @@ func (gm *Game) getQuotesInfoByUserId(usrId string) []Quote { for _, quote := range gm.quotes { if quote.GetSourceId() == usrId { quotes = append(quotes, Quote{ - Id: quote.GetId(), - Quote: quote.GetQuote(), + Id: quote.GetId(), + Quote: quote.GetQuote(), + Created: quote.GetCreated(), }) } } + sort.Sort(ByCreated(quotes)) return quotes } diff --git a/server/src/game/struct.go b/server/src/game/struct.go index 163d3cb..9c11fb5 100644 --- a/server/src/game/struct.go +++ b/server/src/game/struct.go @@ -1,9 +1,10 @@ package game import ( + "sync" + "sirlab.de/go/knowyt/engine" "sirlab.de/go/knowyt/quote" - "sync" ) const ( @@ -77,8 +78,9 @@ type GameJson struct { } type Quote struct { - Id string `json:"id"` - Quote string `json:"quote"` + Id string `json:"id"` + Quote string `json:"quote"` + Created int64 `json:"created"` } type QuotesInfo struct { diff --git a/server/src/quote/quote.go b/server/src/quote/quote.go index a897ee9..43992e2 100644 --- a/server/src/quote/quote.go +++ b/server/src/quote/quote.go @@ -6,6 +6,7 @@ import ( "os" "path" "strings" + "time" ) func NewQuoteFromFile(fileName string) (*Quote, error) { @@ -16,7 +17,7 @@ func NewQuoteFromFile(fileName string) (*Quote, error) { var quJson QuoteJson if err := json.Unmarshal(jsonBytes, &quJson); err != nil { - return nil, fmt.Errorf("%s: %v\n", fileName, err) + return nil, fmt.Errorf("%s: %v", fileName, err) } else { _, fileNameShort := path.Split(fileName) id := strings.TrimSuffix(fileNameShort, ".json") @@ -24,6 +25,7 @@ func NewQuoteFromFile(fileName string) (*Quote, error) { id: id, sourceId: quJson.SourceId, quote: quJson.Quote, + created: quJson.Created, isPlayed: false, } @@ -36,6 +38,7 @@ func NewQuote(id, sourceId, quoteText string) *Quote { id: id, sourceId: sourceId, quote: quoteText, + created: time.Now().Unix(), isPlayed: false, } } @@ -44,6 +47,7 @@ func (qu *Quote) Save(fileName string) error { quJson := QuoteJson{ Quote: qu.quote, SourceId: qu.sourceId, + Created: qu.created, } quoteJson, err := json.MarshalIndent(quJson, "", "\t") @@ -51,7 +55,7 @@ func (qu *Quote) Save(fileName string) error { return err } - return os.WriteFile(fileName, quoteJson, 0777) + return os.WriteFile(fileName, quoteJson, 0666) } func (qu *Quote) GetId() string { @@ -77,3 +81,7 @@ func (qu *Quote) IsPlayed() bool { func (qu *Quote) SetIsPlayed() { qu.isPlayed = true } + +func (qu *Quote) GetCreated() int64 { + return qu.created +} diff --git a/server/src/quote/struct.go b/server/src/quote/struct.go index 3f5ccaa..15d7a9c 100644 --- a/server/src/quote/struct.go +++ b/server/src/quote/struct.go @@ -3,6 +3,7 @@ package quote type Quote struct { id string sourceId string + created int64 quote string isPlayed bool } @@ -10,4 +11,5 @@ type Quote struct { type QuoteJson struct { Quote string `json:"quote"` SourceId string `json:"source"` + Created int64 `json:"created"` }