/
leaderboard.go
77 lines (62 loc) · 1.66 KB
/
leaderboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package server
import (
"net/http"
"github.com/labstack/echo/v4"
db "github.com/reliablestaking/zombie-fight-club-server/db"
"github.com/sirupsen/logrus"
)
type (
Leaders struct {
MostWins []Nft `json:"mostWins"`
MostLosses []Nft `json:"mostLoses"`
BestPercent []Nft `json:"bestPercent"`
}
)
//GetLeaders get all leaders
func (s Server) GetLeaders(c echo.Context) (err error) {
log := logrus.WithContext(c.Request().Context())
leaderCache, found := s.LeaderCache.Get("leaders")
if found {
return c.JSON(http.StatusOK, leaderCache)
}
wins, err := s.Store.GetNftMostWins(10)
if err != nil {
log.WithError(err).Error("Error getting nfts")
return s.RenderError("Error getting nfts", c)
}
loses, err := s.Store.GetNftMostLoses(10)
if err != nil {
log.WithError(err).Error("Error getting nfts")
return s.RenderError("Error getting nfts", c)
}
percent, err := s.Store.GetNftHighestPercentMinimum(10, 10)
if err != nil {
log.WithError(err).Error("Error getting nfts")
return s.RenderError("Error getting nfts", c)
}
leaders := Leaders{
MostWins: s.convertDbNftToNftDto(wins),
MostLosses: s.convertDbNftToNftDto(loses),
BestPercent: s.convertDbNftToNftDto(percent),
}
c.Set("leaders", leaders)
return c.JSON(http.StatusOK, leaders)
}
func (s Server) convertDbNftToNftDto(nfts []db.Nft) []Nft {
dto := make([]Nft, 0)
for _, nft := range nfts {
zc := Nft{
Name: nft.NftName,
Wins: nft.Wins,
Loses: nft.Loses,
}
zc.Type = nft.NftType
if zc.Type == "Zombie" {
zc.IPFS = s.ZombieMeta[nft.NftName]
} else if zc.Type == "Hunter" {
zc.IPFS = s.HunterMeta[nft.NftName]
}
dto = append(dto, zc)
}
return dto
}