split game.master
parent
4bf83b3e40
commit
bce32ed6a3
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -2,8 +2,6 @@ package game
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"local/sandbox/cards/src/config"
|
"local/sandbox/cards/src/config"
|
||||||
"local/sandbox/cards/src/consts"
|
|
||||||
"local/sandbox/cards/src/entity"
|
|
||||||
"local/sandbox/cards/src/storage"
|
"local/sandbox/cards/src/storage"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
@ -14,13 +12,6 @@ type Master struct {
|
||||||
locks *storage.RWLockMap
|
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 {
|
func NewMaster(config config.Config, s Storage) *Master {
|
||||||
return &Master{
|
return &Master{
|
||||||
config: config,
|
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 {
|
func NewTestMaster(t *testing.T) *Master {
|
||||||
config := config.NewTestConfig(t)
|
config := config.NewTestConfig(t)
|
||||||
storage := storage.NewStorage(config)
|
storage := storage.NewStorage(config)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue