game_sync/gamesrv/base/slots.go

86 lines
1.5 KiB
Go

package base
import (
"sync"
"sync/atomic"
"time"
)
type SlotsSession struct {
sync.RWMutex
uid uint64 // binding user id
coin int64
data map[string]interface{} // session data store
lastConnectTime time.Time
connectNum uint64
}
func NewSession(uid uint64, coin int64) *SlotsSession {
return &SlotsSession{
uid: uid,
coin: coin,
data: make(map[string]interface{}),
}
}
func (s *SlotsSession) SetLastConnectTime() bool {
if time.Now().Sub(s.lastConnectTime) > time.Second {
s.connectNum = 1
s.lastConnectTime = time.Now()
} else {
s.connectNum++
if s.connectNum > 5 {
return false
}
}
return true
}
func (s *SlotsSession) GetLastConnectTime() time.Time {
return s.lastConnectTime
}
func (s *SlotsSession) UID() uint64 {
return atomic.LoadUint64(&s.uid)
}
func (s *SlotsSession) Coin() int64 {
return atomic.LoadInt64(&s.coin)
}
func (s *SlotsSession) SetCoin(coin int64) {
s.coin = coin
}
func (s *SlotsSession) Set(key string, value interface{}) {
s.Lock()
defer s.Unlock()
if s.data == nil {
s.data = make(map[string]interface{})
}
s.data[key] = value
}
func (s *SlotsSession) Remove(key string) {
s.Lock()
defer s.Unlock()
delete(s.data, key)
}
func (s *SlotsSession) Value(key string) interface{} {
s.RLock()
defer s.RUnlock()
return s.data[key]
}
func (s *SlotsSession) Bool(key string) bool {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return false
}
value, ok := v.(bool)
if !ok {
return false
}
return value
}