1package arena23import"gno.land/p/g1t2kg2vtr3fukg43eujkn6x53gfdyakhngt4sfd/gnofly/arena/payout/v0"45// Earnings are what players won, added up round after round. Like the6// results they are a record for readers: no payment is ever computed from7// them.
102 to := r.search(score - 1) // after every line with this score or more
103if from < 0 && to >= size {
104return r, false
105 }
106if from >= 0 && to > from {
107 to = from // cannot happen: a score only goes up
108 }
109 next = ranking{
110 addrs: spliceAddrs(r.addrs, from, to, addr, size),
111 scores: spliceInts(r.scores, from, to, score, size),
112 }
113return next, true
114}
115
116// search returns the index of the first line with a score of limit or less,
117// the number of lines when there is none. The list is sorted, so this is a
118// binary search.
119func (r ranking) search(limit int64) int {
120 lo, hi := 0, len(r.scores)
121for lo < hi {
122 mid := (lo + hi) / 2
123if r.scores[mid] <= limit {
124 hi = mid
125 } else {
126 lo = mid + 1
127 }
128 }
129return lo
130}
131
132// lineOf returns the index of a player's line, -1 when they have none. old
133// is the player's score before the change being applied.
134//
135// A line always carries the current score of its player, because every
136// change of a score goes through raise. So the line can only be among those
137// with exactly old. A player whose score was zero has never been through
138// raise and has no line.
139func (r ranking) lineOf(addr address, old int64) int {
140if old <= 0 {
141return -1
142 }
143for i := r.search(old); i < len(r.scores) && r.scores[i] == old; i++ {
144if r.addrs[i] == addr {
145return i
146 }
147 }
148return -1
149}
150
151// spliceAddrs returns a copy of a ranking column with v inserted at index
152// to. With from >= 0 the element at that index, the player's previous line,
153// is left out; to must not be past it. With from < 0 the result is kept to
154// size elements by dropping the tail; to must be below size.
155//
156// The copy has exactly the length of its content, so that nothing else stays
157// in storage.
158func spliceAddrs(list []address, from, to int, v address, size int) []address {
159if from < 0 {
160if len(list) >= size {
161 list = list[:size-1]
162 }
163 out := make([]address, 0, len(list)+1)
164 out = append(out, list[:to]...)
165 out = append(out, v)
166return append(out, list[to:]...)
167 }
168 out := make([]address, 0, len(list))
169 out = append(out, list[:to]...)
170 out = append(out, v)
171 out = append(out, list[to:from]...)
172return append(out, list[from+1:]...)
173}
174
175// spliceInts is spliceAddrs for a column of numbers.
176func spliceInts(list []int64, from, to int, v int64, size int) []int64 {
177if from < 0 {
178if len(list) >= size {
179 list = list[:size-1]
180 }
181 out := make([]int64, 0, len(list)+1)
182 out = append(out, list[:to]...)
183 out = append(out, v)
184return append(out, list[to:]...)
185 }
186 out := make([]int64, 0, len(list))
187 out = append(out, list[:to]...)
188 out = append(out, v)
189 out = append(out, list[to:from]...)
190return append(out, list[from+1:]...)
191}
192
vm/qrender output, sanitized (docs/render-security.md) and displayed in an empty-sandbox iframe — scripts, forms and popups cannot run. Links stay inert in-preview; right-click to open.