diff --git a/pack.go b/pack.go index ba63e77..b98af41 100644 --- a/pack.go +++ b/pack.go @@ -1,6 +1,9 @@ package draft // import "git.fromouter.space/mcg/draft" -import "math/rand" +import ( + "math/rand" + "strings" +) // Pack is a collection of cards from a booster pack type Pack []Card @@ -66,3 +69,12 @@ func MakePackWithSchema(schema PackSchema) Pack { } 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 +} diff --git a/set_test.go b/set_test.go index 69cbabb..3a43c52 100644 --- a/set_test.go +++ b/set_test.go @@ -1,21 +1,23 @@ -package draft +package draft_test import ( "fmt" "math/rand" "testing" + + "git.fromouter.space/mcg/draft" ) // TestSetRepeatable makes sure that a set can generate more cards than it contains func TestSetRepeatable(t *testing.T) { const PACKSIZE = 5 - s := &GenericSet{ - Cards: []Card{{ID: "a"}, {ID: "b"}, {ID: "c"}}, + s := &draft.GenericSet{ + Cards: []draft.Card{{ID: "a"}, {ID: "b"}, {ID: "c"}}, PackSize: PACKSIZE, } // Create a pack - pack := MakePack(s) + pack := draft.MakePack(s) if len(pack) < PACKSIZE { t.Errorf("Pack expected to contain %d cards, contains %d", PACKSIZE, len(pack)) @@ -34,17 +36,17 @@ func TestSetRepeatable(t *testing.T) { // TestAlternateProviders tests alternate providers func TestAlternateProviders(t *testing.T) { - customProvider := func(n int) []Card { - out := make([]Card, n) + customProvider := func(n int) []draft.Card { + out := make([]draft.Card, n) for n := range out { - out[n] = Card{ID: "x"} + out[n] = draft.Card{ID: "x"} } return out } - s := &GenericSet{ - Cards: []Card{{ID: "a"}, {ID: "b"}, {ID: "c"}}, + s := &draft.GenericSet{ + Cards: []draft.Card{{ID: "a"}, {ID: "b"}, {ID: "c"}}, PackSize: 1, - Alternates: []AlternateProvider{ + Alternates: []draft.AlternateProvider{ { Probability: 0.3, Provider: customProvider, @@ -56,7 +58,7 @@ func TestAlternateProviders(t *testing.T) { nonalternates, alternates := 0, 0 for i := 0; i < 500000; i++ { - pack := MakePack(s) + pack := draft.MakePack(s) if pack[0].ID == "x" { alternates++ } else { @@ -76,16 +78,16 @@ func TestAlternateProviders(t *testing.T) { // TestCubeOverflow makes sure cubes stop providing cards as they are exhausted instead of going out of bound func TestCubeOverflow(t *testing.T) { const PACKSIZE = 5 - c := &GenericCube{ - Cards: []Card{ + c := &draft.GenericCube{ + Cards: []draft.Card{ {ID: "a"}, {ID: "b"}, {ID: "c"}, {ID: "d"}, {ID: "e"}, {ID: "f"}, {ID: "g"}, {ID: "h"}, {ID: "i"}, }, PackSize: PACKSIZE, } - pack1 := MakePack(c) - pack2 := MakePack(c) + pack1 := draft.MakePack(c) + pack2 := draft.MakePack(c) // Pack 2 can only contain 4 cards, as there are not enough cards to fill it if len(pack2) >= PACKSIZE { @@ -100,11 +102,21 @@ func TestCubeOverflow(t *testing.T) { } } +// TestPackString makes sure packs are serialized correctly +func TestPackString(t *testing.T) { + p := draft.Pack{{ID: "a"}, {ID: "b"}, {ID: "c"}} + expected := "a b c" + + if p.String() != expected { + t.Errorf("Expected \"%s\" but got \"%s\"", expected, p) + } +} + // ExampleGenericSet is an example usage of the Set APIs to make packs func ExampleGenericSet() { // Create a set with some items - s := &GenericSet{ - Cards: []Card{ + s := &draft.GenericSet{ + Cards: []draft.Card{ {ID: "a"}, {ID: "b"}, {ID: "c"}, @@ -113,7 +125,7 @@ func ExampleGenericSet() { } // Create a pack - pack := MakePack(s) + pack := draft.MakePack(s) // Print cards in pack for i, card := range pack { @@ -122,7 +134,7 @@ func ExampleGenericSet() { } // https://gist.github.com/alioygur/16c66b4249cb42715091fe010eec7e33 -func sliceUniq(s []Card) []Card { +func sliceUniq(s []draft.Card) []draft.Card { for i := 0; i < len(s); i++ { for i2 := i + 1; i2 < len(s); i2++ { if s[i].ID == s[i2].ID {