started adding test for shared group and created utility methods for

getting shared balances
This commit is contained in:
Radu Ioan Fericean
2014-01-09 20:06:32 +02:00
parent c8fb090c11
commit 1471c47bda
4 changed files with 178 additions and 25 deletions

View File

@@ -18,6 +18,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"math"
"math/rand"
"time"
)
const (
STRATEGY_LOWEST_FIRST = "*lowest_first"
STRATEGY_HIGHEST_FIRST = "*highest_first"
STRATEGY_RANDOM = "*random"
)
type SharedGroup struct {
Id string
Strategy string
@@ -26,3 +38,46 @@ type SharedGroup struct {
Weight float64
Members []string
}
func (sg *SharedGroup) GetMembersExceptUser(ubId string) []string {
for i, m := range sg.Members {
if m == ubId {
a := make([]string, len(sg.Members))
copy(a, sg.Members)
a[i], a = a[len(a)-1], a[:len(a)-1]
return a
}
}
return sg.Members
}
func (sg *SharedGroup) PopBalanceByStrategy(balanceChain *BalanceChain) (bal *Balance) {
bc := *balanceChain
if len(bc) == 0 {
return
}
index := 0
switch sg.Strategy {
case STRATEGY_RANDOM:
rand.Seed(time.Now().Unix())
index = rand.Intn(len(bc))
case STRATEGY_LOWEST_FIRST:
minVal := math.MaxFloat64
for i, b := range bc {
if b.Value < minVal {
minVal = b.Value
index = i
}
}
case STRATEGY_HIGHEST_FIRST:
maxVal := math.SmallestNonzeroFloat64
for i, b := range bc {
if b.Value > maxVal {
maxVal = b.Value
index = i
}
}
}
bal, bc[index], *balanceChain = bc[index], bc[len(bc)-1], bc[:len(bc)-1]
return
}