forked from archway-network/valuter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winners.go
133 lines (101 loc) · 2.45 KB
/
winners.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package winners
import (
"fmt"
)
/*------------------*/
func (w *WinnersList) GetItem(index int) Winner {
return w.list[index]
}
/*------------------*/
func (w *WinnersList) GetItems() []Winner {
return w.list
}
/*------------------*/
// This function merges two lists of winners if a winner already exist, it is not added
func (w *WinnersList) Merge(newList WinnersList) {
for i := range newList.list {
w.Append(newList.list[i])
}
}
/*------------------*/
// This function merges two lists of winners and aggregates the rewards
// If a winner already exist, her/his rewards will be aggregated
func (w *WinnersList) MergeWithAggregateRewards(newList WinnersList) {
for i := range newList.list {
w.AppendWithAggregateRewards(newList.list[i])
}
}
/*------------------*/
// This function cuts the end of a list
func (w WinnersList) Trim(length int) WinnersList {
if w.Length() == length {
return w
}
var trimmedList WinnersList
for i := range w.list {
trimmedList.Append(w.list[i])
if i >= length-1 {
break
}
}
return trimmedList
}
/*------------------*/
func (w WinnersList) Length() int {
return len(w.list)
}
/*------------------*/
// Return result:
// -1 : Not found
// >-1: The item index
func (w WinnersList) FindByAddress(address string) int {
if index, ok := w.hashMap[address]; ok {
return index
}
return -1
}
/*------------------*/
func (w *WinnersList) Append(item Winner) {
if index := w.FindByAddress(item.Address); index == -1 {
newIndex := w.Length()
w.list = append(w.list, item)
if w.hashMap == nil {
w.hashMap = make(hashMapType)
}
w.hashMap[item.Address] = newIndex
}
}
/*------------------*/
func (w *WinnersList) AppendWithAggregateRewards(item Winner) {
if index := w.FindByAddress(item.Address); index != -1 {
w.list[index].Rewards += item.Rewards
} else {
newIndex := w.Length()
w.list = append(w.list, item)
if w.hashMap == nil {
w.hashMap = make(hashMapType)
}
w.hashMap[item.Address] = newIndex
}
}
/*------------------*/
func (w WinnersList) Print() {
for i := range w.list {
fmt.Printf("%d \t%s\tRewards: %d\n",
i+1,
w.list[i].Address,
w.list[i].Rewards,
)
}
}
/*------------------*/
func (w *WinnersList) GetVerifiedOnly() WinnersList {
var output WinnersList
for i := range w.list {
// If the winner has participant data, that means it is verified
if w.list[i].ParticipantData.AccountAddress != "" {
output.Append(w.list[i])
}
}
return output
}