diff --git a/src/game/game.go b/src/game/game.go new file mode 100644 index 0000000..24f90a0 --- /dev/null +++ b/src/game/game.go @@ -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) +} diff --git a/src/game/master.go b/src/game/master.go index a5685b6..8bda307 100644 --- a/src/game/master.go +++ b/src/game/master.go @@ -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) diff --git a/src/game/storage.go b/src/game/storage.go new file mode 100644 index 0000000..238ffa1 --- /dev/null +++ b/src/game/storage.go @@ -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 +} diff --git a/src/game/master_test.go b/src/game/storage_test.go similarity index 100% rename from src/game/master_test.go rename to src/game/storage_test.go