draft/pack.go

90 lines
2 KiB
Go
Raw Permalink Normal View History

2019-06-12 13:40:46 +00:00
package draft
2019-05-30 23:16:15 +00:00
import (
"math/rand"
"strings"
)
2019-05-31 08:14:13 +00:00
// Pack is a collection of cards from a booster pack
2019-05-30 23:16:15 +00:00
type Pack []Card
// Card is a single card
type Card struct {
ID string
}
// CardProvider is a function that returns as many cards of a certain types as needed
type CardProvider func(int) []Card
// PackSchema is all that's needed to generate a certain type of pack
type PackSchema struct {
Slots []PackSlot
}
// PackSlot is part of how packs are made, one or more providers provide
// cards for X cards of the whole pack
type PackSlot struct {
Amount int
Provider CardProvider
Alternate []AlternateProvider
}
// AlternateProvider are Card providers that can replace one or more slots
// with special cards (foils, ultra rares)
type AlternateProvider struct {
Probability float32
Provider CardProvider
}
2019-05-31 08:14:13 +00:00
// MakePack makes a booster pack from a given set
2019-05-31 09:12:16 +00:00
// It's a shortcut to `MakePackWithSchema(set.PackSchema())`
func MakePack(set Set) Pack {
schema := set.PackSchema()
2019-05-31 09:12:16 +00:00
return MakePackWithSchema(schema)
}
// MakePackWithSchema makes a booster pack from a given schema
func MakePackWithSchema(schema PackSchema) Pack {
2019-05-30 23:16:15 +00:00
pack := make(Pack, 0)
for _, slot := range schema.Slots {
// Default provider
provider := slot.Provider
// Check for random alternates
if slot.Alternate != nil {
var currentProb float32
var chosenProb = rand.Float32()
for _, alt := range slot.Alternate {
currentProb += alt.Probability
2019-05-30 23:29:22 +00:00
if currentProb > chosenProb {
2019-05-30 23:16:15 +00:00
provider = alt.Provider
break
}
}
}
// Extract cards from provider and add them to the pack
cards := provider(slot.Amount)
pack = append(pack, cards...)
}
return pack
}
// String encodes a pack to a list of space-separated IDs
func (p Pack) String() (str string) {
for _, card := range p {
str += " " + card.ID
}
str = strings.TrimSpace(str)
return
}
2019-06-28 09:17:51 +00:00
// IDs unwraps all the IDs from the cards in the pack
func (p Pack) IDs() []string {
out := make([]string, len(p))
for i, card := range p {
out[i] = card.ID
}
return out
}