You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
896 B
Go
51 lines
896 B
Go
package manager
|
|
|
|
import (
|
|
"git.noahlan.cn/northlan/ntools-go/uuid"
|
|
"sync"
|
|
)
|
|
|
|
type GameStatus int32
|
|
|
|
const (
|
|
GameStarted GameStatus = iota + 1 // 游戏已经开始
|
|
GameEnded // 游戏已结束
|
|
)
|
|
|
|
type GameData struct {
|
|
battleId int64 // 战局ID
|
|
current GameStatus // 当前战局状态
|
|
mutex sync.RWMutex // 锁
|
|
}
|
|
|
|
func NewGameData() *GameData {
|
|
return &GameData{
|
|
current: GameEnded,
|
|
}
|
|
}
|
|
|
|
func (m *GameData) SetStatus(status GameStatus) GameStatus {
|
|
m.mutex.Lock()
|
|
defer m.mutex.Unlock()
|
|
m.current = status
|
|
|
|
if status == GameStarted {
|
|
m.battleId = uuid.NextId()
|
|
} else if status == GameEnded {
|
|
m.battleId = 0
|
|
}
|
|
return m.current
|
|
}
|
|
|
|
func (m *GameData) Status() GameStatus {
|
|
m.mutex.RLock()
|
|
defer m.mutex.RUnlock()
|
|
return m.current
|
|
}
|
|
|
|
func (m *GameData) BattleId() int64 {
|
|
m.mutex.RLock()
|
|
defer m.mutex.RUnlock()
|
|
return m.battleId
|
|
}
|