2022-04-13 23:09:58 +00:00
|
|
|
package card
|
2022-04-13 07:48:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.tuxpa.in/a/card_id/common/data"
|
|
|
|
"lukechampine.com/frand"
|
|
|
|
)
|
|
|
|
|
|
|
|
// a table is a collection of results
|
|
|
|
|
2022-04-13 23:09:58 +00:00
|
|
|
type Deck struct {
|
|
|
|
Cards []Card
|
2022-04-13 07:48:22 +00:00
|
|
|
|
|
|
|
TotalProbability float64
|
|
|
|
}
|
|
|
|
|
2022-04-13 23:09:58 +00:00
|
|
|
func NewDeck(r data.Results) *Deck {
|
|
|
|
out := &Deck{}
|
|
|
|
out.Cards = make([]Card, 0, len(r))
|
|
|
|
for _, v := range r {
|
|
|
|
out.Cards = append(out.Cards, new(Card).FromResult(v))
|
2022-04-13 07:48:22 +00:00
|
|
|
out.TotalProbability = out.TotalProbability + v.Rate
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-04-13 23:09:58 +00:00
|
|
|
func (t *Deck) Draw() Card {
|
2022-04-13 07:48:22 +00:00
|
|
|
selector := frand.Float64() * t.TotalProbability
|
|
|
|
sofar := 0.0
|
2022-04-13 23:09:58 +00:00
|
|
|
for _, v := range t.Cards {
|
2022-04-13 07:48:22 +00:00
|
|
|
sofar = sofar + v.Rate
|
|
|
|
if sofar >= selector-0.000000001 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 23:09:58 +00:00
|
|
|
return Card{
|
|
|
|
Id: 0,
|
2022-04-13 07:48:22 +00:00
|
|
|
Name: fmt.Sprintf("please report this bug: %v", selector),
|
|
|
|
Rate: selector,
|
|
|
|
}
|
|
|
|
}
|