player.gno
3.41 Kb · 156 lines
1package hall
2
3import (
4 "chain"
5 "sort"
6
7 "gno.land/r/sys/users"
8)
9
10// profile is a player as the hall knows them.
11type profile struct {
12 addr address
13 handle string
14 solves []Solve // claim order
15 badges []string
16}
17
18func (p *profile) solved(slug string) bool {
19 for _, s := range p.solves {
20 if s.Slug == slug {
21 return true
22 }
23 }
24 return false
25}
26
27// score sums the player's solves; season 0 means all time.
28func (p *profile) score(season int) (points, solves int, last int64) {
29 for _, s := range p.solves {
30 if season != 0 && s.Season != season {
31 continue
32 }
33 points += solvePoints(s)
34 solves++
35 last = s.Height
36 }
37 return points, solves, last
38}
39
40// profileOf returns the profile of addr, creating it on first use.
41func profileOf(addr address) *profile {
42 if p, ok := findProfile(addr); ok {
43 return p
44 }
45 p := &profile{addr: addr}
46 players.Set(addr.String(), p)
47 return p
48}
49
50func findProfile(addr address) (*profile, bool) {
51 v := players.Get(addr.String())
52 if v == nil {
53 return nil, false
54 }
55 return v.(*profile), true
56}
57
58// SetHandle sets the caller's display name in the hall. Handles are
59// unique; setting a new one releases the old one.
60func SetHandle(cur realm, handle string) {
61 caller := cur.Previous()
62 if !caller.IsUserCall() {
63 panic(ErrNotUser)
64 }
65 if !validHandle(handle) {
66 panic(ErrBadHandle)
67 }
68 addr := caller.Address()
69 if owner := handles.Get(handle); owner != nil && owner.(address) != addr {
70 panic(ErrHandleTaken)
71 }
72 p := profileOf(addr)
73 if p.handle != "" {
74 handles.Remove(p.handle)
75 }
76 p.handle = handle
77 handles.Set(handle, addr)
78 chain.Emit("Handle", "player", addr.String(), "handle", handle)
79}
80
81func validHandle(h string) bool {
82 if len(h) < 3 || len(h) > 20 || h[0] < 'a' || h[0] > 'z' {
83 return false
84 }
85 for _, c := range h {
86 if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '_') {
87 return false
88 }
89 }
90 return true
91}
92
93// DisplayName returns the hall handle of addr, else its gno.land username,
94// else a shortened address.
95func DisplayName(addr address) string {
96 if p, ok := findProfile(addr); ok && p.handle != "" {
97 return p.handle
98 }
99 if u := users.ResolveAddress(addr); u != nil {
100 return u.Name()
101 }
102 s := addr.String()
103 if len(s) < 14 {
104 return s
105 }
106 return s[:8] + "…" + s[len(s)-4:]
107}
108
109// resolvePlayer finds an address from an address or a handle.
110func resolvePlayer(who string) (address, bool) {
111 if addr := address(who); addr.IsValid() {
112 return addr, true
113 }
114 if v := handles.Get(who); v != nil {
115 return v.(address), true
116 }
117 return "", false
118}
119
120// Standing is a row of a leaderboard.
121type Standing struct {
122 Player address
123 Points int
124 Solves int
125 Last int64 // height of the latest counted solve, for tie-breaks
126}
127
128// standings ranks every player with points; season 0 means all time.
129// Ties go to whoever got there first.
130func standings(season int) []Standing {
131 var rows []Standing
132 players.Iterate("", "", func(_ string, v any) bool {
133 p := v.(*profile)
134 pts, n, last := p.score(season)
135 if n > 0 {
136 rows = append(rows, Standing{Player: p.addr, Points: pts, Solves: n, Last: last})
137 }
138 return false
139 })
140 sort.Sort(byRank(rows))
141 return rows
142}
143
144type byRank []Standing
145
146func (r byRank) Len() int { return len(r) }
147func (r byRank) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
148func (r byRank) Less(i, j int) bool {
149 if r[i].Points != r[j].Points {
150 return r[i].Points > r[j].Points
151 }
152 if r[i].Last != r[j].Last {
153 return r[i].Last < r[j].Last
154 }
155 return r[i].Player < r[j].Player
156}