game

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2020 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromState

func FromState(timers entity.Timers, Started bool,
	GameEndReason int, p0id, p1id uint, WinnerIdx, LoserIdx int, reqBytes, histBytes []byte,
	stats *entity.Stats,
	gameEventChan chan<- *entity.EventWrapper, cfg *config.Config) (*entity.Game, error)

FromState returns an entity.Game from a DB State.

Types

type Cache

type Cache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Cache will reside in-memory, and will be per-node. If we add more nodes we will need to make sure only the right nodes respond to game requests.

func NewCache

func NewCache(backing backingStore) *Cache

func (*Cache) Create

func (c *Cache) Create(ctx context.Context, game *entity.Game) error

Create creates the game in the cache as well as the store.

func (*Cache) Exists

func (c *Cache) Exists(ctx context.Context, id string) bool

Exists lets us know whether the game is in the cache

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, id string) (*entity.Game, error)

Get gets a game from the cache. It doesn't try to get it from the backing store, if it can't find it in the cache.

func (*Cache) ListActive

func (c *Cache) ListActive(ctx context.Context) ([]*pb.GameMeta, error)

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, game *entity.Game) error

Set sets a game in the cache, AND in the backing store. This ensures if the node crashes the game doesn't just vanish.

func (*Cache) SetGameEventChan

func (c *Cache) SetGameEventChan(ch chan<- *entity.EventWrapper)

SetGameEventChan sets the game event channel to the passed in channel.

func (*Cache) Unload

func (c *Cache) Unload(ctx context.Context, id string)

Unload unloads the game from the cache

type DBStore

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

DBStore is a postgres-backed store for games.

func NewDBStore

func NewDBStore(config *config.Config, userStore pkguser.Store) (*DBStore, error)

NewDBStore creates a new DB store for games.

func (*DBStore) Create

func (s *DBStore) Create(ctx context.Context, g *entity.Game) error

Create saves a brand new entity to the database

func (*DBStore) Disconnect

func (s *DBStore) Disconnect()

func (*DBStore) Get

func (s *DBStore) Get(ctx context.Context, id string) (*entity.Game, error)

Get creates an instantiated entity.Game from the database. This function should almost never be called during a live game. The db store should be wrapped with a cache. Only API nodes that have this game in its cache should respond to requests.

func (*DBStore) ListActive

func (s *DBStore) ListActive(ctx context.Context) ([]*pb.GameMeta, error)

func (*DBStore) Set

func (s *DBStore) Set(ctx context.Context, g *entity.Game) error

Set takes in a game entity that _already exists_ in the DB, and writes it to the database.

func (*DBStore) SetGameEventChan

func (s *DBStore) SetGameEventChan(c chan<- *entity.EventWrapper)

SetGameEventChan sets the game event channel to the passed in channel.

type MemoryStore

type MemoryStore struct {
	sync.Mutex
	// contains filtered or unexported fields
}

MemoryStore is a purely in-memory store of a game. In the real final implementation, we will probably use a Postgres-backed memory store. Due to the nature of this app, we will always need to have a persistent in-memory instantiation of a game.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

func (*MemoryStore) Create

func (m *MemoryStore) Create(ctx context.Context, game *entity.Game) error

Create creates a game in the store initially. Make it the same function as Set essentially.

func (*MemoryStore) Get

func (m *MemoryStore) Get(ctx context.Context, id string) (*entity.Game, error)

Get gets the game with the given ID.

func (*MemoryStore) Set

func (m *MemoryStore) Set(ctx context.Context, game *entity.Game) error

Set sets the game in the store.

Jump to

Keyboard shortcuts

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