Refactor set to be an interface, add generic set and cube

This commit is contained in:
Hamcha 2019-05-31 10:11:54 +02:00
parent b250fc9b33
commit da2a2ad914
Signed by: hamcha
GPG key ID: A40413D21021EAEE
5 changed files with 79 additions and 13 deletions

View file

@ -1,5 +0,0 @@
package draft
type Cube struct {
Cards []Card
}

View file

@ -29,8 +29,8 @@ on set. Specifically, this is the RR ratio for each set:
*/
// BoxSchema returns the pack schema from a booster box for a specific set
func (set *Set) BoxSchema() draft.PackSchema {
// PackSchema returns the pack schema from a booster box for a specific set
func (set *Set) PackSchema() draft.PackSchema {
// Return blank schemas for invalid sets
if set.ID == SetRockNRave || set.ID == SetCelestialSolstice {
return draft.PackSchema{}

5
mlp/cube.go Normal file
View file

@ -0,0 +1,5 @@
package mlp
type Cube struct {
}

View file

@ -1,10 +1,6 @@
package draft // import "git.fromouter.space/mcg/draft"
import (
"math/rand"
)
// Pack is a collection of cards from a booster pack
import "math/rand" // Pack is a collection of cards from a booster pack
type Pack []Card
// Card is a single card
@ -36,7 +32,8 @@ type AlternateProvider struct {
}
// MakePack makes a booster pack from a given schema
func MakePack(schema PackSchema) Pack {
func MakePack(set Set) Pack {
schema := set.PackSchema()
pack := make(Pack, 0)
for _, slot := range schema.Slots {
// Default provider

69
set.go Normal file
View file

@ -0,0 +1,69 @@
package draft
import "math/rand"
// Set is an interface for all game sets/expansions
type Set interface {
PackSchema() PackSchema
}
// GenericSet is an generalized set of a card game
// Treat this as an example implementation or a starting
type GenericSet struct {
Cards []Card
PackSize int
}
// PackSchema returns the pack schema from a booster containing all possible cards
func (g *GenericSet) PackSchema() PackSchema {
return PackSchema{
Slots: []PackSlot{
{Amount: g.PackSize, Provider: g.RandomProvider()},
},
}
}
// RandomProvider returns a provider for random cards from the set
func (g *GenericSet) RandomProvider() CardProvider {
return func(n int) []Card {
out := make([]Card, n)
for n := range out {
idx := rand.Intn(len(g.Cards))
out[n] = g.Cards[idx]
}
return out
}
}
// GenericCube is a "consumable" set, meaning cards get taken out of the pool as they are put in packs
type GenericCube struct {
Cards []Card
PackSize int
}
// PackSchema returns the pack schema from a booster containing all possible cards
func (c *GenericCube) PackSchema() PackSchema {
return PackSchema{
Slots: []PackSlot{
{Amount: c.PackSize, Provider: c.RandomProvider()},
},
}
}
// RandomProvider returns a provider for random cards from the set
func (c *GenericCube) RandomProvider() CardProvider {
return func(n int) (out []Card) {
c.shuffle()
if len(c.Cards) < n {
n = len(c.Cards)
}
out, c.Cards = c.Cards[:n], c.Cards[n:]
return
}
}
func (c *GenericCube) shuffle() {
rand.Shuffle(len(c.Cards), func(i, j int) {
c.Cards[i], c.Cards[j] = c.Cards[j], c.Cards[i]
})
}