split game.master

master
Bel LaPointe 2021-03-13 16:02:30 -06:00
parent 4bf83b3e40
commit bce32ed6a3
4 changed files with 55 additions and 46 deletions

43
src/game/game.go Normal file
View File

@ -0,0 +1,43 @@
package game
import (
"local/sandbox/cards/src/consts"
"local/sandbox/cards/src/entity"
)
func (gm *Master) ListGames() ([]string, error) {
return gm.storage.ListGames()
}
func (gm *Master) GetGame(id string) (entity.Game, error) {
gm.locks.RLock(id)
defer gm.locks.RUnlock(id)
game, err := gm.storage.GetGame(id)
if game.Players == nil {
game.Players = make(entity.Players, 0)
}
return game, err
}
func (gm *Master) CreateGame(id string) error {
gm.locks.Lock(id)
defer gm.locks.Unlock(id)
if _, err := gm.storage.GetGame(id); err == nil {
return consts.ErrGameExists
}
return gm.storage.CreateGame(id)
}
func (gm *Master) ReplaceGame(id string, game entity.Game) error {
gm.locks.Lock(id)
defer gm.locks.Unlock(id)
if _, err := gm.storage.GetGame(id); err != nil {
return err
}
return gm.storage.ReplaceGame(id, game)
}

View File

@ -2,8 +2,6 @@ package game
import (
"local/sandbox/cards/src/config"
"local/sandbox/cards/src/consts"
"local/sandbox/cards/src/entity"
"local/sandbox/cards/src/storage"
"testing"
)
@ -14,13 +12,6 @@ type Master struct {
locks *storage.RWLockMap
}
type Storage interface {
CreateGame(string) error
GetGame(string) (entity.Game, error)
ListGames() ([]string, error)
ReplaceGame(string, entity.Game) error
}
func NewMaster(config config.Config, s Storage) *Master {
return &Master{
config: config,
@ -29,43 +20,6 @@ func NewMaster(config config.Config, s Storage) *Master {
}
}
func (gm *Master) ListGames() ([]string, error) {
return gm.storage.ListGames()
}
func (gm *Master) GetGame(id string) (entity.Game, error) {
gm.locks.RLock(id)
defer gm.locks.RUnlock(id)
game, err := gm.storage.GetGame(id)
if game.Players == nil {
game.Players = make(entity.Players, 0)
}
return game, err
}
func (gm *Master) CreateGame(id string) error {
gm.locks.Lock(id)
defer gm.locks.Unlock(id)
if _, err := gm.storage.GetGame(id); err == nil {
return consts.ErrGameExists
}
return gm.storage.CreateGame(id)
}
func (gm *Master) ReplaceGame(id string, game entity.Game) error {
gm.locks.Lock(id)
defer gm.locks.Unlock(id)
if _, err := gm.storage.GetGame(id); err != nil {
return err
}
return gm.storage.ReplaceGame(id, game)
}
func NewTestMaster(t *testing.T) *Master {
config := config.NewTestConfig(t)
storage := storage.NewStorage(config)

12
src/game/storage.go Normal file
View File

@ -0,0 +1,12 @@
package game
import (
"local/sandbox/cards/src/entity"
)
type Storage interface {
CreateGame(string) error
GetGame(string) (entity.Game, error)
ListGames() ([]string, error)
ReplaceGame(string, entity.Game) error
}