2022-10-03 11:53:27 +03:00
|
|
|
package view
|
|
|
|
|
2022-10-06 00:36:57 +03:00
|
|
|
import (
|
|
|
|
"github.com/muety/wakapi/models"
|
2022-12-29 13:54:14 +03:00
|
|
|
"github.com/muety/wakapi/utils"
|
2022-10-06 00:36:57 +03:00
|
|
|
"strings"
|
2022-10-06 16:30:18 +03:00
|
|
|
"time"
|
2022-10-06 00:36:57 +03:00
|
|
|
)
|
2022-10-03 11:53:27 +03:00
|
|
|
|
|
|
|
type LeaderboardViewModel struct {
|
2023-01-02 20:05:28 +03:00
|
|
|
Messages
|
2022-10-05 22:52:10 +03:00
|
|
|
User *models.User
|
|
|
|
By string
|
|
|
|
Key string
|
2022-10-20 09:33:12 +03:00
|
|
|
Items []*models.LeaderboardItemRanked
|
2022-10-05 22:52:10 +03:00
|
|
|
TopKeys []string
|
|
|
|
UserLanguages map[string][]string
|
|
|
|
ApiKey string
|
2022-12-29 13:54:14 +03:00
|
|
|
PageParams *utils.PageParams
|
2022-10-03 11:53:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LeaderboardViewModel) WithSuccess(m string) *LeaderboardViewModel {
|
2023-01-02 20:05:28 +03:00
|
|
|
s.SetSuccess(m)
|
2022-10-03 11:53:27 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LeaderboardViewModel) WithError(m string) *LeaderboardViewModel {
|
2023-01-02 20:05:28 +03:00
|
|
|
s.SetError(m)
|
2022-10-03 11:53:27 +03:00
|
|
|
return s
|
|
|
|
}
|
2022-10-04 00:52:22 +03:00
|
|
|
|
2022-10-20 09:33:12 +03:00
|
|
|
func (s *LeaderboardViewModel) ColorModifier(item *models.LeaderboardItemRanked, principal *models.User) string {
|
2022-10-04 00:52:22 +03:00
|
|
|
if principal != nil && item.UserID == principal.ID {
|
|
|
|
return "self"
|
|
|
|
}
|
|
|
|
if item.Rank == 1 {
|
|
|
|
return "gold"
|
|
|
|
}
|
|
|
|
if item.Rank == 2 {
|
|
|
|
return "silver"
|
|
|
|
}
|
|
|
|
if item.Rank == 3 {
|
|
|
|
return "bronze"
|
|
|
|
}
|
|
|
|
return "default"
|
|
|
|
}
|
2022-10-06 00:36:57 +03:00
|
|
|
|
|
|
|
func (s *LeaderboardViewModel) LangIcon(lang string) string {
|
|
|
|
// https://icon-sets.iconify.design/mdi/
|
|
|
|
langs := map[string]string{
|
2022-11-11 18:13:41 +03:00
|
|
|
"c++": "language-cpp",
|
|
|
|
"cpp": "language-cpp",
|
|
|
|
"go": "language-go",
|
|
|
|
"haskell": "language-haskell",
|
|
|
|
"html": "language-html5",
|
|
|
|
"java": "language-java",
|
|
|
|
"javascript": "language-javascript",
|
|
|
|
"jsx": "language-javascript",
|
|
|
|
"kotlin": "language-kotlin",
|
|
|
|
"lua": "language-lua",
|
|
|
|
"php": "language-php",
|
|
|
|
"python": "language-python",
|
|
|
|
"r": "language-r",
|
|
|
|
"ruby": "language-ruby",
|
|
|
|
"rust": "language-rust",
|
|
|
|
"swift": "language-swift",
|
|
|
|
"typescript": "language-typescript",
|
|
|
|
"tsx": "language-typescript",
|
|
|
|
"markdown": "language-markdown",
|
2022-11-13 22:11:53 +03:00
|
|
|
"vue": "vuejs",
|
|
|
|
"react": "react",
|
2022-11-11 18:13:41 +03:00
|
|
|
"bash": "bash",
|
|
|
|
"json": "code-json",
|
2022-10-06 00:36:57 +03:00
|
|
|
}
|
|
|
|
if match, ok := langs[strings.ToLower(lang)]; ok {
|
2022-11-11 18:13:41 +03:00
|
|
|
return "mdi:" + match
|
2022-10-06 00:36:57 +03:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2022-10-06 16:30:18 +03:00
|
|
|
|
|
|
|
func (s *LeaderboardViewModel) LastUpdate() time.Time {
|
|
|
|
return models.Leaderboard(s.Items).LastUpdate()
|
|
|
|
}
|