deck

package
v0.0.0-...-b6c9256 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrInvalidCardCode is an error that indicates that the card code
	// provided to the system does not conform to the expected format.
	ErrInvalidCardCode = errors.New("invalid card code")

	// ErrInvalidSuitCode is an error that signifies a non-matching
	// suit code was provided during card creation or processing.
	ErrInvalidSuitCode = errors.New("invalid suit code")

	// ErrInvalidValueCode is an error indicating that the value code portion
	// of a card code does not match any of the standard card values.
	ErrInvalidValueCode = errors.New("invalid value code")
)
View Source
var (
	// ErrDeckNotFound is an error that indicates that
	// the deck ID was not found in the database.
	ErrDeckNotFound = errors.New("deck not found")

	// ErrNotEnoughCards is an error that indicates that we are
	// about to draw more cards than available in the deck.
	ErrNotEnoughCards = errors.New("not enough cards in the deck")
)

Functions

func ShuffleCards

func ShuffleCards(cards []Card) error

ShuffleCards takes a slice of cards and shuffles it in place using cryptographic randomness.

Types

type Card

type Card interface{}

Card is the interface that all card types need to implement.

func GenerateStandardFrenchCardsForDeck

func GenerateStandardFrenchCardsForDeck() []Card

GenerateStandardFrenchCardsForDeck creates a complete deck of French playing cards in a sequential order: A-spades, 2-spades, 3-spades... Followed by diamonds, clubs, then hearts.

type CreateDeckRequest

type CreateDeckRequest struct {
	Shuffled bool         `json:"shuffled"`
	DeckType string       `json:"deck_type"`
	Cards    []FrenchCard `json:"cards"`
}

CreateDeckRequest represents the parameters required to create a new deck of cards.

type CreateDeckResponse

type CreateDeckResponse struct {
	DeckID    string `json:"deck_id"`
	Shuffled  bool   `json:"shuffled"`
	Remaining int    `json:"remaining"`
}

CreateDeckResponse represents the response returned after creating a new deck of cards.

type Deck

type Deck struct {
	ID        uuid.UUID `json:"deck_id"`
	Shuffled  bool      `json:"shuffled"`
	Remaining int       `json:"remaining"`
	Cards     []Card    `json:"-"`
}

Deck represents a card deck.

func NewWithFrenchCards

func NewWithFrenchCards(deckType string, shuffled bool, customCards []FrenchCard) (*Deck, error)

NewWithFrenchCards creates a new deck of cards based on the specified deck type and shuffle option.

func (*Deck) MarshalJSON

func (d *Deck) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Deck) UnmarshalJSON

func (d *Deck) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type DrawCardsRequest

type DrawCardsRequest struct {
	Cards int `json:"cards"`
}

DrawCardsRequest represents the parameters required to draw cards from a deck.

type DrawCardsResponse

type DrawCardsResponse struct {
	Cards []FrenchCard `json:"cards"`
}

DrawCardsResponse represents the response returned after drawing cards from a deck.

type ErrorResponse

type ErrorResponse struct {
	Message string `json:"message"`
}

ErrorResponse represents the response message when encountering an error.

type FrenchCard

type FrenchCard struct {
	Value string `json:"value"`
	Suit  string `json:"suit"`
	Code  string `json:"code"`
}

FrenchCard is a type that implements the Card interface.

func (*FrenchCard) FromCode

func (c *FrenchCard) FromCode(cardCode string) error

FromCode uses maps to determine the full value and suit from a card code.

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

Repository allows for interacting with the card decks stored in the database.

func (*Repository) CreateDeck

func (r *Repository) CreateDeck(ctx context.Context, deck *Deck) error

CreateDeck inserts a new card deck into the database using the deck information provided.

func (*Repository) DrawCards

func (r *Repository) DrawCards(ctx context.Context, deckID string, cardsToDraw int) ([]FrenchCard, error)

DrawCards draws N cards from a given card deck in the database.

func (*Repository) OpenDeck

func (r *Repository) OpenDeck(ctx context.Context, deckID string) (*Deck, error)

OpenDeck retrieves a card deck from the database using the deck ID.

type RepositoryOperator

type RepositoryOperator interface {
	CreateDeck(ctx context.Context, deck *Deck) error
	OpenDeck(ctx context.Context, deckID string) (*Deck, error)
	DrawCards(ctx context.Context, deckID string, cardsToDraw int) ([]FrenchCard, error)
}

RepositoryOperator is an interface with allowed deck repository methods.

func NewRepository

func NewRepository(db *sql.DB) RepositoryOperator

NewRepository creates a new Repository with a given database connection.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service for managing card decks.

func NewService

func NewService(log zerolog.Logger, repository RepositoryOperator) *Service

NewService returns a new instance of Service.

func (*Service) CreateDeck

func (s *Service) CreateDeck(w http.ResponseWriter, req *http.Request)

CreateDeck handles the creation of a new card deck.

func (*Service) DrawCards

func (s *Service) DrawCards(w http.ResponseWriter, req *http.Request)

DrawCards draws N cards from a given card deck.

func (*Service) OpenDeck

func (s *Service) OpenDeck(w http.ResponseWriter, req *http.Request)

OpenDeck handles the creation of a new card deck.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL