Refactor set to be an interface, add generic set and cube
This commit is contained in:
parent
b250fc9b33
commit
da2a2ad914
5 changed files with 79 additions and 13 deletions
5
cube.go
5
cube.go
|
@ -1,5 +0,0 @@
|
||||||
package draft
|
|
||||||
|
|
||||||
type Cube struct {
|
|
||||||
Cards []Card
|
|
||||||
}
|
|
|
@ -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
|
// PackSchema returns the pack schema from a booster box for a specific set
|
||||||
func (set *Set) BoxSchema() draft.PackSchema {
|
func (set *Set) PackSchema() draft.PackSchema {
|
||||||
// Return blank schemas for invalid sets
|
// Return blank schemas for invalid sets
|
||||||
if set.ID == SetRockNRave || set.ID == SetCelestialSolstice {
|
if set.ID == SetRockNRave || set.ID == SetCelestialSolstice {
|
||||||
return draft.PackSchema{}
|
return draft.PackSchema{}
|
||||||
|
|
5
mlp/cube.go
Normal file
5
mlp/cube.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package mlp
|
||||||
|
|
||||||
|
type Cube struct {
|
||||||
|
|
||||||
|
}
|
9
pack.go
9
pack.go
|
@ -1,10 +1,6 @@
|
||||||
package draft // import "git.fromouter.space/mcg/draft"
|
package draft // import "git.fromouter.space/mcg/draft"
|
||||||
|
|
||||||
import (
|
import "math/rand" // Pack is a collection of cards from a booster pack
|
||||||
"math/rand"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Pack is a collection of cards from a booster pack
|
|
||||||
type Pack []Card
|
type Pack []Card
|
||||||
|
|
||||||
// Card is a single card
|
// Card is a single card
|
||||||
|
@ -36,7 +32,8 @@ type AlternateProvider struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakePack makes a booster pack from a given schema
|
// 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)
|
pack := make(Pack, 0)
|
||||||
for _, slot := range schema.Slots {
|
for _, slot := range schema.Slots {
|
||||||
// Default provider
|
// Default provider
|
||||||
|
|
69
set.go
Normal file
69
set.go
Normal 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]
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue