query.gno
4.51 Kb · 177 lines
1package standings
2
3import (
4 "gno.land/p/nt/avl/v0"
5 "gno.land/r/sys/users"
6)
7
8const maxPage = 50
9
10// Standing is one player's place on the ladder. Rank is 1 for the highest rating and
11// equal ratings share it; Rank is 0 and Rating 1000 for a player with no rated match.
12// Name is the player's gno.land name, "" without one. MainClan is "" until their
13// first rated match with a counted clan.
14type Standing struct {
15 Rank int
16 Player, Name, MainClan string
17 Rating int
18 Wins, Losses, Draws int
19}
20
21// ClanStanding is one clan's record across rated cross-clan matches.
22type ClanStanding struct {
23 Rank int
24 Clan string
25 Rating int
26 Wins, Losses, Draws int
27}
28
29// Result is one logged result. Seq is its place in the log, 1 first; Writer is the path
30// of the realm that reported it. Result is "a" when the challenger won, "b" when the
31// acceptor did, and "draw"; Time is the block's Unix seconds.
32type Result struct {
33 Seq int
34 Writer string
35 Challenger, Acceptor string
36 ChallengerClan, AcceptorClan string
37 Result string
38 Time int64
39}
40
41// Leaderboard lists rated players, highest rating first: limit of them after skipping offset.
42func Leaderboard(offset, limit int) []Standing {
43 checkPage(offset, limit)
44 list := []Standing{}
45 ladder.TraverseByOffset(offset, limit, true, true, func(n *avl.Node) bool {
46 s := standingOf(address(n.Key()[2:]))
47 switch {
48 case len(list) == 0:
49 s.Rank = rankOf(s.Rating)
50 case list[len(list)-1].Rating == s.Rating:
51 s.Rank = list[len(list)-1].Rank
52 default:
53 s.Rank = offset + len(list) + 1
54 }
55 list = append(list, s)
56 return false
57 })
58 return list
59}
60
61// StandingOf returns player's standing, rated or not.
62func StandingOf(player address) Standing {
63 s := standingOf(player)
64 if s.Wins+s.Losses+s.Draws > 0 {
65 s.Rank = rankOf(s.Rating)
66 }
67 return s
68}
69
70// RatedPlayers counts the players on the ladder.
71func RatedPlayers() int {
72 return ladder.Size()
73}
74
75// ClanStandings lists every clan with a rated cross-clan match, highest rating first;
76// equal ratings keep first-seen order and share a rank. Clans with no such match are left
77// out: they stand at 1000 and 0-0-0.
78func ClanStandings() []ClanStanding {
79 list := []ClanStanding{}
80 for _, c := range clans {
81 if c.wins+c.losses+c.draws == 0 {
82 continue
83 }
84 list = append(list, ClanStanding{Clan: c.name, Rating: c.rating, Wins: c.wins, Losses: c.losses, Draws: c.draws})
85 }
86 for i := 1; i < len(list); i++ {
87 for j := i; j > 0 && list[j].Rating > list[j-1].Rating; j-- {
88 list[j], list[j-1] = list[j-1], list[j]
89 }
90 }
91 for i := range list {
92 if i > 0 && list[i].Rating == list[i-1].Rating {
93 list[i].Rank = list[i-1].Rank
94 } else {
95 list[i].Rank = i + 1
96 }
97 }
98 return list
99}
100
101// Results lists logged results, oldest first: limit of them after skipping offset.
102func Results(offset, limit int) []Result {
103 checkPage(offset, limit)
104 list := []Result{}
105 n := resultCount()
106 if offset >= n {
107 return list
108 }
109 end := offset + limit
110 if end > n {
111 end = n
112 }
113 for seq := offset + 1; seq <= end; seq++ {
114 e := resultAt(seq)
115 list = append(list, Result{
116 Seq: seq,
117 Writer: writerPaths[e.writer-1],
118 Challenger: e.challenger.String(),
119 Acceptor: e.acceptor.String(),
120 ChallengerClan: clanName(e.clans[0]),
121 AcceptorClan: clanName(e.clans[1]),
122 Result: resultName(e.result),
123 Time: e.time,
124 })
125 }
126 return list
127}
128
129// ResultCount counts the logged results.
130func ResultCount() int {
131 return resultCount()
132}
133
134// standingOf is player's standing without its rank.
135func standingOf(player address) Standing {
136 s := Standing{Player: player.String(), Name: nameOf(player), Rating: startRating}
137 st := loadStanding(player)
138 if st == nil {
139 return s
140 }
141 s.MainClan = clanName(st.mainClan())
142 s.Rating, s.Wins, s.Losses, s.Draws = st.rating, st.wins, st.losses, st.draws
143 return s
144}
145
146// nameOf is player's gno.land name, "" when they have none.
147func nameOf(player address) string {
148 u := users.ResolveAddress(player)
149 if u.IsDeleted() {
150 return ""
151 }
152 return u.Name()
153}
154
155// clanName is the name of clan number n, "" for 0, no clan.
156func clanName(n int) string {
157 if n == 0 {
158 return ""
159 }
160 return clans[n-1].name
161}
162
163func resultName(b byte) string {
164 if b == 'd' {
165 return "draw"
166 }
167 return string(b)
168}
169
170func checkPage(offset, limit int) {
171 if offset < 0 {
172 panic("offset must not be negative")
173 }
174 if limit < 1 || limit > maxPage {
175 panic("limit must be 1 to 50")
176 }
177}