package logic import ( "encoding/json" . "gls/lib/game/dbo" "os" "sync" "time" ) type Slot struct { dbo *Dbo //筹码 Balance *DboFloat //下注额 Bet *DboFloat //剩余触发免费 FreeSpCount *DboInt //真钱 RealBalance *DboFloat //代理商 Ops *DboString //代理商账号 Opid *DboString //是否加测试钱 IsAddBalance *DboString //总投注 SpinBalance *DboFloat //总投注次数 SpinCount *DboInt //总赢钱 WinBalance *DboFloat //总赢钱次数 WinCount *DboInt //运营商token OpToken *DboString //登录token Token *DboString //客户端存图 Pic *DboString // 免费剩余次数 FgCount *DboInt // 选择模式的索引 Select *DboInt // 独立逻辑服务器数据 Data *DboString // 免费游戏状态 Status *DboInt // 订单号 OrderID *DboString // 投注模式 0 普通模式 1 免费模式 2 购买模式 3 购买触发的免费模式 SpinType *DboInt // 数据中心随到Idx Idx *DboInt // 免费模式数据中心数据 DCFree *DboString // 玩家rtp Rtp *DboFloat mu sync.Mutex } var slotMap sync.Map var slotMapExp sync.Map var slotMapExpTemp sync.Map func New(gameType string, uid string) *Slot { slot := &Slot{} slot.dbo = &Dbo{} slot.dbo.GameType = gameType slot.dbo.Uid = uid slot.LoadData() return slot } func (s *Slot) LoadData() { s.Lock() defer s.Unlock() if HasSlot(s.dbo.GameType, s.dbo.Uid) { return } //全局用户数据 s.Ops = s.dbo.NewString("ops", "", true) s.Opid = s.dbo.NewString("opid", "", true) s.RealBalance = s.dbo.NewFloat("realBalance", 0, true) s.SpinBalance = s.dbo.NewFloat("spinBalance", 0, true) s.SpinCount = s.dbo.NewInt("spinCount", 0, true) s.WinBalance = s.dbo.NewFloat("winBalance", 0, true) s.WinCount = s.dbo.NewInt("winCount", 0, true) s.OpToken = s.dbo.NewString("optoken", "", true) s.Token = s.dbo.NewString("token", "", true) s.Rtp = s.dbo.NewFloat("Rtp", 0, true) if s.Ops.Get() != "" { s.Balance = s.dbo.NewFloat("balance", 0, false) } else { s.Balance = s.dbo.NewFloat("balance", 0, false) // 测试加钱 s.IsAddBalance = s.dbo.NewString("isAddBalance", "", false) if s.IsAddBalance.Get() == "" { bal := 100000.0 path := "../../../server/sconf/" + s.dbo.GameType + ".json" file, err := os.Open(path) if err == nil { defer file.Close() var jsonData = map[string]interface{}{} decoder := json.NewDecoder(file) if err := decoder.Decode(&jsonData); err == nil { bal = jsonData["balance"].(float64) } } s.Balance.Add(bal) s.IsAddBalance.Set("1") } } //每个游戏单独用户数据 s.Bet = s.dbo.NewFloat("bet", 2, false) s.FreeSpCount = s.dbo.NewInt("freeSpCount", 0, false) s.Pic = s.dbo.NewString("pic", "", false) s.FgCount = s.dbo.NewInt("FgCount", 0, false) s.Select = s.dbo.NewInt("Select", 0, false) s.Data = s.dbo.NewString("Data", "", false) s.Status = s.dbo.NewInt("Status", 0, false) s.OrderID = s.dbo.NewString("OrderID", "", false) s.SpinType = s.dbo.NewInt("SpinType", 0, false) s.Idx = s.dbo.NewInt("Idx", 0, false) s.DCFree = s.dbo.NewString("DCFree", "", false) } func HasSlot(gameType string, uid string) bool { key := gameType + uid _, ok := slotMap.Load(key) return ok } func GetSlot(gameType string, uid string) *Slot { key := gameType + uid slotMapExp.Store(key, time.Now().Unix()) go expireUser() value, ok := slotMap.Load(key) if !ok { slot := New(gameType, uid) slotMap.Store(key, slot) return slot } else { return value.(*Slot) } } var isExpiring = false func expireUser() { if isExpiring { return } isExpiring = true for { now := time.Now().Unix() ks := []string{} slotMapExp.Range(func(k, v any) bool { if now-v.(int64) > 60*5 { ks = append(ks, k.(string)) slotMapExpTemp.Store(k, v) slotMap.Delete(k) slotMapExp.Delete(k) } return true }) time.Sleep(time.Second * 60) for _, key := range ks { slotMapExpTemp.Delete(key) } time.Sleep(time.Second * 60 * 4) } } func GetNewSlot(gameType string, uid string) *Slot { slot := GetSlot(gameType, uid) return slot } func ResetSlot(gameType string, uid string) { key := gameType + uid slotMap.Delete(key) } func (s *Slot) Lock() { s.mu.Lock() } func (s *Slot) Unlock() { s.mu.Unlock() } func (s *Slot) SaveUserData() string { s.Lock() defer s.Unlock() return "" } func (s *Slot) Heart() int64 { s.Lock() defer s.Unlock() var code int64 code = 0 return code } func (s *Slot) GetBalance() float64 { return s.Balance.Get() + s.RealBalance.Get() } func (s *Slot) GetGameType() string { return s.dbo.GameType }