71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
|
package card
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"path"
|
||
|
"strconv"
|
||
|
|
||
|
"git.tuxpa.in/a/card_id/common/data"
|
||
|
"lukechampine.com/frand"
|
||
|
)
|
||
|
|
||
|
type Dealer struct {
|
||
|
Brands map[string]*Brand
|
||
|
TotalProbability float64
|
||
|
}
|
||
|
|
||
|
func NewDealer() *Dealer {
|
||
|
return &Dealer{
|
||
|
Brands: make(map[string]*Brand, 12),
|
||
|
TotalProbability: 0,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (g *Dealer) ReadFromRoot(datadir string) (*Dealer, error) {
|
||
|
if datadir == "" {
|
||
|
datadir = "./data"
|
||
|
}
|
||
|
grades, err := data.NewGradesFromFile(path.Join(datadir, "GradeInfo.yaml"))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
for _, v := range grades {
|
||
|
lvl := &Brand{}
|
||
|
lvl.Name = strconv.Itoa(v.Grade)
|
||
|
g.Brands[lvl.Name] = lvl
|
||
|
lvl.SelectionWeight = v.Rate
|
||
|
g.TotalProbability = g.TotalProbability + v.Rate
|
||
|
|
||
|
rres, err := data.NewResultsFromFile(path.Join(datadir, "ResultTable", lvl.Name+".yaml"))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
lvl.RewardDeck = NewDeck(rres)
|
||
|
if v.SpecialRate > 0 {
|
||
|
lvl.SpecialChance = v.SpecialRate
|
||
|
sres, err := data.NewResultsFromFile(path.Join(datadir, "SpecialTable", lvl.Name+".yaml"))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
lvl.SpecialDeck = NewDeck(sres)
|
||
|
}
|
||
|
}
|
||
|
return g, nil
|
||
|
}
|
||
|
|
||
|
func (g *Dealer) GetBrand() *Brand {
|
||
|
selector := frand.Float64() * g.TotalProbability
|
||
|
sofar := 0.0
|
||
|
if len(g.Brands) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
for _, v := range g.Brands {
|
||
|
sofar = sofar + v.SelectionWeight
|
||
|
if sofar >= selector-0.000000001 {
|
||
|
return v
|
||
|
}
|
||
|
}
|
||
|
log.Println("BAD BAD BAD BAD BAD BAD PLEASE REPORT OH NO OH NO OH FISJAFIJNSAF")
|
||
|
return g.GetBrand()
|
||
|
}
|