battle

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGameNotFound       = errors.New("game is not found")
	ErrBattleNotFound     = errors.New("battle is not found")
	ErrInvalidBattleState = errors.New("invalid battle state")
)

Functions

This section is empty.

Types

type BattleStorage

type BattleStorage interface {
	// GetBattle returns battle instance for given gameID from storage. Returns nil
	// when battle instance is not found.
	GetBattle(ctx context.Context, gameID string) (*entity.Battle, error)

	// SaveBattle is used for saving given battle instance into storage. If battle
	// instance is already exists in the storage, it will be overwritten.
	SaveBattle(ctx context.Context, b entity.Battle) error
}

type GameStorage

type GameStorage interface {
	// GetGame returns game instance for given gameID from storage. Returns nil
	// when given gameID is not found in database.
	GetGame(ctx context.Context, gameID string) (*entity.Game, error)

	// Save is used for saving game instance in storage.
	SaveGame(ctx context.Context, game entity.Game) error
}

type MonsterStorage

type MonsterStorage interface {
	// GetPossibleEnemies returns all possible enemies in the game. Returns nil
	// when there is no possible enemies.
	GetPossibleEnemies(ctx context.Context) ([]entity.Monster, error)
}

type Service

type Service interface {
	// StartBattle is used for starting new battle for given game id. Battle could only
	// be started when there is no previous battle or it is already ended.
	StartBattle(ctx context.Context, gameID string) (*entity.Battle, error)

	// GetBattle returns ongoing battle for given game id.
	GetBattle(ctx context.Context, gameID string) (*entity.Battle, error)

	// DecideTurn is used for deciding turn for the battle. There are 3 possible outcome
	// battle states from this action:
	//
	// - DECIDE_TURN => partner has been attacked by enemy but still not lose, so the state
	// 					returned back to DECIDE_TURN
	// - LOSE => partner has been attacked by enemy and already lose
	// - PARTNER_TURN => it is partner turn, client may commence attack or surrender
	DecideTurn(ctx context.Context, gameID string) (*entity.Battle, error)

	// Attack is used for executing attack to enemy. The possible battle state outcome
	// is DECIDE_TURN or WIN.
	Attack(ctx context.Context, gameID string) (*entity.Battle, error)

	// Surrender is used for executing surrender action. Battle will immediately ended
	// with player losing the battle.
	Surrender(ctx context.Context, gameID string) (*entity.Battle, error)
}

func NewService

func NewService(cfg ServiceConfig) (Service, error)

NewService returns new instance of service.

type ServiceConfig

type ServiceConfig struct {
	GameStorage    GameStorage    `validate:"nonnil"`
	BattleStorage  BattleStorage  `validate:"nonnil"`
	MonsterStorage MonsterStorage `validate:"nonnil"`
}

func (ServiceConfig) Validate

func (c ServiceConfig) Validate() error

Jump to

Keyboard shortcuts

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