app

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package app provides business logic.

Package app is a generated GoMock package.

Index

Constants

This section is empty.

Variables

View Source
var Difficulty = map[string]game.Config{
	"test": {
		MaxActiveLicenses: 3,
		Density:           4,
		SizeX:             5,
		SizeY:             5,
		Depth:             10,
		TreasureValue: func() *[]int {
			treasureValues := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
			return &treasureValues
		}(),
		TreasureValueAlg: game.AlgDoubleMax,
	},
	"normal": {
		MaxActiveLicenses: 10,
		Density:           250,
		SizeX:             3500,
		SizeY:             3500,
		Depth:             10,
	},
}

Difficulty contains predefined game difficulty levels.

View Source
var (
	ErrContactExists = errors.New("contact already exists")
)

Errors.

View Source
var (
	Metric def.Metrics // Common metrics used by all packages.

)

Functions

func InitMetrics

func InitMetrics(reg *prometheus.Registry)

InitMetrics must be called once before using this package. It registers and initializes metrics used by this package.

Types

type App

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

App implements interface Appl.

func New

func New(ctx Ctx, repo Repo, cpu CPU, svcLicense LicenseSvc, factory GameFactory, cfg Config) (*App, error)

func (*App) Balance

func (a *App) Balance(ctx Ctx) (balance int, wallet []int, err error)

func (*App) Cash

func (a *App) Cash(ctx Ctx, treasure string) (wallet []int, err error)

func (*App) Dig

func (a *App) Dig(ctx Ctx, licenseID int, pos game.Coord) (treasure string, _ error)

func (*App) ExploreArea

func (a *App) ExploreArea(ctx Ctx, area game.Area) (int, error)

func (*App) HealthCheck added in v0.2.4

func (a *App) HealthCheck(_ Ctx) (interface{}, error)

func (*App) IssueLicense

func (a *App) IssueLicense(ctx Ctx, wallet []int) (game.License, error)

func (*App) Licenses

func (a *App) Licenses(ctx Ctx) ([]game.License, error)

func (*App) Start

func (a *App) Start(t time.Time) (err error)

func (*App) Wait

func (a *App) Wait(ctx Ctx) (err error)

type Appl

type Appl interface {
	// HealthCheck returns error if service is unhealthy or current
	// status otherwise.
	// Errors: none.
	HealthCheck(Ctx) (interface{}, error)
	// Start must be called before any other method to ensure task
	// will be available for cfg.Duration since given time. Second and
	// following calls will have no effect, so it's safe to call Start
	// on every API call.
	// Errors: none.
	Start(time.Time) error
	// Balance returns current balance and up to 1000 issued coins.
	// Errors: none.
	Balance(Ctx) (balance int, wallet []int, err error)
	// Licenses returns all active licenses.
	// Errors: resource.ErrRPCInternal, resource.ErrRPCTimeout.
	Licenses(Ctx) ([]game.License, error)
	// IssueLicense creates and returns a new license with given digAllowed.
	// Errors: game.ErrActiveLicenseLimit, game.ErrBogusCoin,
	// resource.ErrRPCInternal, resource.ErrRPCTimeout.
	IssueLicense(_ Ctx, wallet []int) (game.License, error)
	// ExploreArea returns amount of not-digged-yet treasures in the
	// area at depth.
	// Errors: game.ErrWrongCoord.
	ExploreArea(_ Ctx, area game.Area) (int, error)
	// Dig tries to dig at pos and returns if any treasure was found.
	// The pos depth must be next to current (already digged) one.
	// Also it increment amount of used dig calls in given active license.
	// If amount of used dig calls became equal to amount of allowed dig calls
	// then license will became inactive after the call.
	// Errors: game.ErrNoSuchLicense, game.ErrWrongCoord, game.ErrWrongDepth.
	Dig(_ Ctx, licenseID int, pos game.Coord) (treasure string, _ error)
	// Cash returns coins earned for treasure as given pos.
	// Errors: game.ErrWrongCoord, game.ErrNotDigged, game.ErrAlreadyCached.
	Cash(_ Ctx, treasure string) (wallet []int, err error)
}

Appl provides application features (use cases) service.

type CPU added in v1.0.0

type CPU interface {
	// Consume t resources of this CPU instance.
	// It returns nil if consumed successfully or ctx.Err() if ctx is done
	// earlier than t resources will be consumed.
	Consume(Ctx, time.Duration) error
}

CPU is a resource which can be consumed for up to time.Second per real-time second (i.e. it's a single-core CPU).

type Config

type Config struct {
	StartTimeout       time.Duration
	Duration           time.Duration
	Game               game.Config
	AutosavePeriod     time.Duration
	DigBaseDelay       time.Duration
	DigExtraDelay      time.Duration
	DepthProfitChange  float64
	LicensePercentFail int
}

type Contact

type Contact struct {
	ID   int
	Name string
}

Contact describes record in address book.

type Ctx

type Ctx = context.Context

Ctx is a synonym for convenience.

type GameFactory

type GameFactory interface {
	// New creates and returns new game.
	New(Ctx, game.Config) (game.Game, error)
	// Continue creates and returns new game restored from given reader, which
	// should contain data written by Game.WriteTo.
	Continue(Ctx, io.ReadSeeker) (game.Game, error)
}

GameFactory provides different ways to create a new game.

type LicenseSvc added in v1.0.0

type LicenseSvc interface {
	// Call will use percentFail and percentTimeout to decide call result:
	//   - delay 0.01…0.1 sec without error
	//   - delay 0.01 sec with ErrRPCInternal
	//   - delay 1 sec with ErrRPCTimeout
	Call(ctx Ctx, percentFail int) error
}

LicenseSvc is a virtual resource which pretends to be an RPC client.

type MockAppl

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

MockAppl is a mock of Appl interface

func NewMockAppl

func NewMockAppl(ctrl *gomock.Controller) *MockAppl

NewMockAppl creates a new mock instance

func (*MockAppl) Balance

func (m *MockAppl) Balance(arg0 Ctx) (int, []int, error)

Balance mocks base method

func (*MockAppl) Cash

func (m *MockAppl) Cash(arg0 Ctx, treasure string) ([]int, error)

Cash mocks base method

func (*MockAppl) Dig

func (m *MockAppl) Dig(arg0 Ctx, licenseID int, pos game.Coord) (string, error)

Dig mocks base method

func (*MockAppl) EXPECT

func (m *MockAppl) EXPECT() *MockApplMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockAppl) ExploreArea

func (m *MockAppl) ExploreArea(arg0 Ctx, area game.Area) (int, error)

ExploreArea mocks base method

func (*MockAppl) HealthCheck added in v0.2.4

func (m *MockAppl) HealthCheck(arg0 Ctx) (interface{}, error)

HealthCheck mocks base method

func (*MockAppl) IssueLicense

func (m *MockAppl) IssueLicense(arg0 Ctx, wallet []int) (game.License, error)

IssueLicense mocks base method

func (*MockAppl) Licenses

func (m *MockAppl) Licenses(arg0 Ctx) ([]game.License, error)

Licenses mocks base method

func (*MockAppl) Start

func (m *MockAppl) Start(arg0 time.Time) error

Start mocks base method

type MockApplMockRecorder

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

MockApplMockRecorder is the mock recorder for MockAppl

func (*MockApplMockRecorder) Balance

func (mr *MockApplMockRecorder) Balance(arg0 interface{}) *gomock.Call

Balance indicates an expected call of Balance

func (*MockApplMockRecorder) Cash

func (mr *MockApplMockRecorder) Cash(arg0, treasure interface{}) *gomock.Call

Cash indicates an expected call of Cash

func (*MockApplMockRecorder) Dig

func (mr *MockApplMockRecorder) Dig(arg0, licenseID, pos interface{}) *gomock.Call

Dig indicates an expected call of Dig

func (*MockApplMockRecorder) ExploreArea

func (mr *MockApplMockRecorder) ExploreArea(arg0, area interface{}) *gomock.Call

ExploreArea indicates an expected call of ExploreArea

func (*MockApplMockRecorder) HealthCheck added in v0.2.4

func (mr *MockApplMockRecorder) HealthCheck(arg0 interface{}) *gomock.Call

HealthCheck indicates an expected call of HealthCheck

func (*MockApplMockRecorder) IssueLicense

func (mr *MockApplMockRecorder) IssueLicense(arg0, wallet interface{}) *gomock.Call

IssueLicense indicates an expected call of IssueLicense

func (*MockApplMockRecorder) Licenses

func (mr *MockApplMockRecorder) Licenses(arg0 interface{}) *gomock.Call

Licenses indicates an expected call of Licenses

func (*MockApplMockRecorder) Start

func (mr *MockApplMockRecorder) Start(arg0 interface{}) *gomock.Call

Start indicates an expected call of Start

type MockCPU added in v1.0.0

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

MockCPU is a mock of CPU interface

func NewMockCPU added in v1.0.0

func NewMockCPU(ctrl *gomock.Controller) *MockCPU

NewMockCPU creates a new mock instance

func (*MockCPU) Consume added in v1.0.0

func (m *MockCPU) Consume(arg0 Ctx, arg1 time.Duration) error

Consume mocks base method

func (*MockCPU) EXPECT added in v1.0.0

func (m *MockCPU) EXPECT() *MockCPUMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

type MockCPUMockRecorder added in v1.0.0

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

MockCPUMockRecorder is the mock recorder for MockCPU

func (*MockCPUMockRecorder) Consume added in v1.0.0

func (mr *MockCPUMockRecorder) Consume(arg0, arg1 interface{}) *gomock.Call

Consume indicates an expected call of Consume

type MockGameFactory added in v0.3.0

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

MockGameFactory is a mock of GameFactory interface

func NewMockGameFactory added in v0.3.0

func NewMockGameFactory(ctrl *gomock.Controller) *MockGameFactory

NewMockGameFactory creates a new mock instance

func (*MockGameFactory) Continue added in v0.3.0

func (m *MockGameFactory) Continue(arg0 Ctx, arg1 io.ReadSeeker) (game.Game, error)

Continue mocks base method

func (*MockGameFactory) EXPECT added in v0.3.0

EXPECT returns an object that allows the caller to indicate expected use

func (*MockGameFactory) New added in v0.3.0

func (m *MockGameFactory) New(arg0 Ctx, arg1 game.Config) (game.Game, error)

New mocks base method

type MockGameFactoryMockRecorder added in v0.3.0

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

MockGameFactoryMockRecorder is the mock recorder for MockGameFactory

func (*MockGameFactoryMockRecorder) Continue added in v0.3.0

func (mr *MockGameFactoryMockRecorder) Continue(arg0, arg1 interface{}) *gomock.Call

Continue indicates an expected call of Continue

func (*MockGameFactoryMockRecorder) New added in v0.3.0

func (mr *MockGameFactoryMockRecorder) New(arg0, arg1 interface{}) *gomock.Call

New indicates an expected call of New

type MockLicenseSvc added in v1.0.0

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

MockLicenseSvc is a mock of LicenseSvc interface

func NewMockLicenseSvc added in v1.0.0

func NewMockLicenseSvc(ctrl *gomock.Controller) *MockLicenseSvc

NewMockLicenseSvc creates a new mock instance

func (*MockLicenseSvc) Call added in v1.0.0

func (m *MockLicenseSvc) Call(ctx Ctx, percentFail int) error

Call mocks base method

func (*MockLicenseSvc) EXPECT added in v1.0.0

EXPECT returns an object that allows the caller to indicate expected use

type MockLicenseSvcMockRecorder added in v1.0.0

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

MockLicenseSvcMockRecorder is the mock recorder for MockLicenseSvc

func (*MockLicenseSvcMockRecorder) Call added in v1.0.0

func (mr *MockLicenseSvcMockRecorder) Call(ctx, percentFail interface{}) *gomock.Call

Call indicates an expected call of Call

type MockReadSeekCloser added in v0.3.0

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

MockReadSeekCloser is a mock of ReadSeekCloser interface

func NewMockReadSeekCloser added in v0.3.0

func NewMockReadSeekCloser(ctrl *gomock.Controller) *MockReadSeekCloser

NewMockReadSeekCloser creates a new mock instance

func (*MockReadSeekCloser) Close added in v0.3.0

func (m *MockReadSeekCloser) Close() error

Close mocks base method

func (*MockReadSeekCloser) EXPECT added in v0.3.0

EXPECT returns an object that allows the caller to indicate expected use

func (*MockReadSeekCloser) Read added in v0.3.0

func (m *MockReadSeekCloser) Read(p []byte) (int, error)

Read mocks base method

func (*MockReadSeekCloser) Seek added in v0.3.0

func (m *MockReadSeekCloser) Seek(offset int64, whence int) (int64, error)

Seek mocks base method

type MockReadSeekCloserMockRecorder added in v0.3.0

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

MockReadSeekCloserMockRecorder is the mock recorder for MockReadSeekCloser

func (*MockReadSeekCloserMockRecorder) Close added in v0.3.0

Close indicates an expected call of Close

func (*MockReadSeekCloserMockRecorder) Read added in v0.3.0

func (mr *MockReadSeekCloserMockRecorder) Read(p interface{}) *gomock.Call

Read indicates an expected call of Read

func (*MockReadSeekCloserMockRecorder) Seek added in v0.3.0

func (mr *MockReadSeekCloserMockRecorder) Seek(offset, whence interface{}) *gomock.Call

Seek indicates an expected call of Seek

type MockRepo

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

MockRepo is a mock of Repo interface

func NewMockRepo

func NewMockRepo(ctrl *gomock.Controller) *MockRepo

NewMockRepo creates a new mock instance

func (*MockRepo) EXPECT

func (m *MockRepo) EXPECT() *MockRepoMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockRepo) LoadGame added in v0.3.0

func (m *MockRepo) LoadGame() (ReadSeekCloser, error)

LoadGame mocks base method

func (*MockRepo) LoadStartTime

func (m *MockRepo) LoadStartTime() (*time.Time, error)

LoadStartTime mocks base method

func (*MockRepo) LoadTreasureKey added in v0.3.0

func (m *MockRepo) LoadTreasureKey() ([]byte, error)

LoadTreasureKey mocks base method

func (*MockRepo) SaveError added in v1.3.0

func (m *MockRepo) SaveError(msg string) error

SaveError mocks base method

func (*MockRepo) SaveGame added in v0.3.0

func (m *MockRepo) SaveGame(arg0 io.WriterTo) error

SaveGame mocks base method

func (*MockRepo) SaveResult added in v0.3.0

func (m *MockRepo) SaveResult(arg0 int) error

SaveResult mocks base method

func (*MockRepo) SaveStartTime

func (m *MockRepo) SaveStartTime(t time.Time) error

SaveStartTime mocks base method

func (*MockRepo) SaveTreasureKey added in v0.3.0

func (m *MockRepo) SaveTreasureKey(arg0 []byte) error

SaveTreasureKey mocks base method

type MockRepoMockRecorder

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

MockRepoMockRecorder is the mock recorder for MockRepo

func (*MockRepoMockRecorder) LoadGame added in v0.3.0

func (mr *MockRepoMockRecorder) LoadGame() *gomock.Call

LoadGame indicates an expected call of LoadGame

func (*MockRepoMockRecorder) LoadStartTime

func (mr *MockRepoMockRecorder) LoadStartTime() *gomock.Call

LoadStartTime indicates an expected call of LoadStartTime

func (*MockRepoMockRecorder) LoadTreasureKey added in v0.3.0

func (mr *MockRepoMockRecorder) LoadTreasureKey() *gomock.Call

LoadTreasureKey indicates an expected call of LoadTreasureKey

func (*MockRepoMockRecorder) SaveError added in v1.3.0

func (mr *MockRepoMockRecorder) SaveError(msg interface{}) *gomock.Call

SaveError indicates an expected call of SaveError

func (*MockRepoMockRecorder) SaveGame added in v0.3.0

func (mr *MockRepoMockRecorder) SaveGame(arg0 interface{}) *gomock.Call

SaveGame indicates an expected call of SaveGame

func (*MockRepoMockRecorder) SaveResult added in v0.3.0

func (mr *MockRepoMockRecorder) SaveResult(arg0 interface{}) *gomock.Call

SaveResult indicates an expected call of SaveResult

func (*MockRepoMockRecorder) SaveStartTime

func (mr *MockRepoMockRecorder) SaveStartTime(t interface{}) *gomock.Call

SaveStartTime indicates an expected call of SaveStartTime

func (*MockRepoMockRecorder) SaveTreasureKey added in v0.3.0

func (mr *MockRepoMockRecorder) SaveTreasureKey(arg0 interface{}) *gomock.Call

SaveTreasureKey indicates an expected call of SaveTreasureKey

type ReadSeekCloser added in v0.3.0

type ReadSeekCloser interface {
	io.ReadSeeker
	io.Closer
}

ReadSeekCloser is the interface that groups the basic Read, Seek and Close methods.

type Repo

type Repo interface {
	// LoadStartTime returns start time or zero time if not started.
	// Errors: none.
	LoadStartTime() (*time.Time, error)
	// SaveStartTime stores start time.
	// Errors: none.
	SaveStartTime(t time.Time) error
	// LoadTreasureKey returns treasure key.
	// Errors: none.
	LoadTreasureKey() ([]byte, error)
	// SaveTreasureKey stores treasure key.
	// Errors: none.
	SaveTreasureKey([]byte) error
	// LoadGame returns game state.
	// Errors: none.
	LoadGame() (ReadSeekCloser, error)
	// SaveGame stores game state.
	// Errors: none.
	SaveGame(io.WriterTo) error
	// SaveResult stores final game result.
	// Errors: none.
	SaveResult(int) error
	// SaveError stores final game error.
	// Errors: none.
	SaveError(msg string) error
}

Repo provides data storage.

Directories

Path Synopsis
Package game implements treasure hunting game.
Package game implements treasure hunting game.
Package resource implements virtual resources, which simulate some real-world resources without actually consuming them.
Package resource implements virtual resources, which simulate some real-world resources without actually consuming them.

Jump to

Keyboard shortcuts

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