game

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2022 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// Note: above is overly optimistic.
	// It seems each cache slot is taking about 750kB.
	// That's in addition to about 200MB base.
	// Reduced cache cap accordingly.
	CacheCap = 400
)
View Source
const (
	MaxRecentGames = 20
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.RWMutex // used for the activeGames cache.
	// 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) CachedCount added in v0.1.3

func (c *Cache) CachedCount(ctx context.Context) int

func (*Cache) Count added in v0.1.3

func (c *Cache) Count(ctx context.Context) (int64, error)

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) Disconnect added in v0.1.2

func (c *Cache) Disconnect()

func (*Cache) Exists

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

func (*Cache) GameEventChan added in v0.1.3

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

func (*Cache) Get

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

Get gets a game from the cache.. it loads it into the cache if it's not there.

func (*Cache) GetHistory added in v0.1.3

func (c *Cache) GetHistory(ctx context.Context, id string) (*macondopb.GameHistory, error)

func (*Cache) GetMetadata added in v0.1.2

func (c *Cache) GetMetadata(ctx context.Context, id string) (*pb.GameInfoResponse, error)

Similar to get but does not unmarshal the stats and timers and does not play the game

func (*Cache) GetRecentGames added in v0.1.2

func (c *Cache) GetRecentGames(ctx context.Context, username string, numGames int, offset int) (*pb.GameInfoResponses, error)

func (*Cache) GetRecentTourneyGames added in v0.1.3

func (c *Cache) GetRecentTourneyGames(ctx context.Context, tourneyID string, numGames int, offset int) (*pb.GameInfoResponses, error)

func (*Cache) GetRematchStreak added in v0.1.2

func (c *Cache) GetRematchStreak(ctx context.Context, originalRequestId string) (*gs.StreakInfoResponse, error)

Just call the DB implementation for now

func (*Cache) ListActive

func (c *Cache) ListActive(ctx context.Context, tourneyID string, bust bool) (*pb.GameInfoResponses, error)

ListActive lists all active games in the given tournament ID (optional) or site-wide if not provided. If `bust` is true, we will always query the backing store.

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) SetReady added in v0.1.3

func (c *Cache) SetReady(ctx context.Context, gid string, pidx int) (int, error)

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) CachedCount added in v0.1.3

func (s *DBStore) CachedCount(ctx context.Context) int

func (*DBStore) Count added in v0.1.3

func (s *DBStore) Count(ctx context.Context) (int64, error)

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) Exists added in v0.1.2

func (s *DBStore) Exists(ctx context.Context, id string) (bool, error)

func (*DBStore) GameEventChan added in v0.1.3

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

GameEventChan returns the game event channel for all games.

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) GetHistory added in v0.1.3

func (s *DBStore) GetHistory(ctx context.Context, id string) (*macondopb.GameHistory, error)

func (*DBStore) GetMetadata added in v0.1.2

func (s *DBStore) GetMetadata(ctx context.Context, id string) (*pb.GameInfoResponse, error)

GetMetadata gets metadata about the game, but does not actually play the game.

func (*DBStore) GetRecentGames added in v0.1.2

func (s *DBStore) GetRecentGames(ctx context.Context, username string, numGames int, offset int) (*pb.GameInfoResponses, error)

func (*DBStore) GetRecentTourneyGames added in v0.1.3

func (s *DBStore) GetRecentTourneyGames(ctx context.Context, tourneyID string, numGames int, offset int) (*pb.GameInfoResponses, error)

func (*DBStore) GetRematchStreak added in v0.1.2

func (s *DBStore) GetRematchStreak(ctx context.Context, originalRequestId string) (*gs.StreakInfoResponse, error)

func (*DBStore) ListActive

func (s *DBStore) ListActive(ctx context.Context, tourneyID string) (*pb.GameInfoResponses, error)

func (*DBStore) ListAllIDs added in v0.1.1

func (s *DBStore) ListAllIDs(ctx context.Context) ([]string, error)

List all game IDs, ordered by date played. Should not be used by anything other than debug or migration code when the db is still small.

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.

func (*DBStore) SetReady added in v0.1.3

func (s *DBStore) SetReady(ctx context.Context, gid string, pidx int) (int, error)

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