card_id/common/game/game.go

73 lines
1.6 KiB
Go
Raw Normal View History

2022-04-13 07:48:22 +00:00
package game
import (
"log"
"path"
"strconv"
"git.tuxpa.in/a/card_id/common/data"
"git.tuxpa.in/a/card_id/common/game/level"
"git.tuxpa.in/a/card_id/common/game/table"
"lukechampine.com/frand"
)
type Game struct {
Levels map[string]*level.Level
TotalProbability float64
}
2022-04-13 07:51:52 +00:00
func New() *Game {
return &Game{
Levels: make(map[string]*level.Level, 12),
TotalProbability: 0,
2022-04-13 07:48:22 +00:00
}
2022-04-13 07:51:52 +00:00
}
func (g *Game) ReadFromRoot(datadir string) (*Game, error) {
2022-04-13 07:48:22 +00:00
if datadir == "" {
datadir = "./data"
}
grades, err := data.NewGradesFromFile(path.Join(datadir, "GradeInfo.yaml"))
if err != nil {
return nil, err
}
for _, v := range grades {
lvl := &level.Level{}
lvl.Name = strconv.Itoa(v.Grade)
2022-04-13 07:51:52 +00:00
g.Levels[lvl.Name] = lvl
2022-04-13 07:48:22 +00:00
lvl.SelectionWeight = v.Rate
2022-04-13 07:51:52 +00:00
g.TotalProbability = g.TotalProbability + v.Rate
2022-04-13 07:48:22 +00:00
rres, err := data.NewResultsFromFile(path.Join(datadir, "ResultTable", lvl.Name+".yaml"))
if err != nil {
return nil, err
}
lvl.RewardTable = table.New(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.SpecialTable = table.New(sres)
}
}
2022-04-13 07:51:52 +00:00
return g, nil
2022-04-13 07:48:22 +00:00
}
func (g *Game) RandomLevel() *level.Level {
selector := frand.Float64() * g.TotalProbability
sofar := 0.0
if len(g.Levels) == 0 {
return nil
}
for _, v := range g.Levels {
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.RandomLevel()
}