96 lines
3.0 KiB
Go
96 lines
3.0 KiB
Go
![]() |
package model
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"github.com/go-redis/redis/v8"
|
||
|
"samba/pkg/log"
|
||
|
"samba/util/rdbkey"
|
||
|
)
|
||
|
|
||
|
type ClubPlayingRoom struct {
|
||
|
RoomId int `json:"r"` // 房间id
|
||
|
PlayType int `json:"p"` // 玩法id
|
||
|
RoomType int `json:"ro"` // 房间配置id
|
||
|
Blind int64 `json:"b"` // 底注
|
||
|
CurrPlayerNum int `json:"c"` // 当前玩家数量
|
||
|
MaxPlayerNum int `json:"m"` // 最大玩家数量
|
||
|
MaxGameNum int `json:"g"` // 游戏总局数
|
||
|
Status int `json:"s"` // 房间状态 0:等待 1:准备 其它为正在游戏中
|
||
|
ServiceTime int64 `json:"se"` // 服务启动时间,该时间之前的房间说明是本服务重启之前的房间,需要移除
|
||
|
Top int `json:"t"` // 置顶 0:没有置顶 1:置顶
|
||
|
}
|
||
|
|
||
|
type ClubPlayingRoomOp struct {
|
||
|
rdb *redis.Client
|
||
|
}
|
||
|
|
||
|
func NewClubPlayingRoomOp() *ClubPlayingRoomOp {
|
||
|
return &ClubPlayingRoomOp{rdb: rdbBaseInfo}
|
||
|
}
|
||
|
|
||
|
// 拉取该俱乐部下所有房间
|
||
|
func (op *ClubPlayingRoomOp) LoadAll(clubId int) ([]*ClubPlayingRoom, error) {
|
||
|
// 获取玩法
|
||
|
playInfos, _ := NewClubPlayInfoOp().Load(clubId)
|
||
|
var rooms []*ClubPlayingRoom
|
||
|
var delRooms []*ClubPlayingRoom
|
||
|
var lastTime = int64(0)
|
||
|
maps, err := op.rdb.HGetAll(context.Background(), rdbkey.ClubPlayingRoomKey(clubId)).Result()
|
||
|
if err != nil {
|
||
|
log.Error(fmt.Sprintf("redis key:%v hgetall err:%v", rdbkey.ClubPlayingRoomKey(clubId), err))
|
||
|
return nil, err
|
||
|
}
|
||
|
for sRoomId, sRoomData := range maps {
|
||
|
var clubPlayingRoom ClubPlayingRoom
|
||
|
if err = json.Unmarshal([]byte(sRoomData), &clubPlayingRoom); err != nil {
|
||
|
log.Error(fmt.Sprintf("redis key:%v roomId:%v room data:%v.err:%v", rdbkey.ClubPlayingRoomKey(clubId), sRoomId, sRoomData, err))
|
||
|
return nil, err
|
||
|
}
|
||
|
if lastTime < clubPlayingRoom.ServiceTime {
|
||
|
lastTime = clubPlayingRoom.ServiceTime
|
||
|
delRooms = append(delRooms, rooms...)
|
||
|
rooms = rooms[:0]
|
||
|
}
|
||
|
for _, playInfo := range playInfos {
|
||
|
if playInfo.ID == clubPlayingRoom.RoomType {
|
||
|
clubPlayingRoom.Top = playInfo.Top
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
rooms = append(rooms, &clubPlayingRoom)
|
||
|
}
|
||
|
for _, delRoom := range delRooms {
|
||
|
op.Del(clubId, delRoom.RoomId)
|
||
|
}
|
||
|
return rooms, nil
|
||
|
}
|
||
|
|
||
|
// Update 更新俱乐部房间
|
||
|
func (op *ClubPlayingRoomOp) Update(clubId int, room *ClubPlayingRoom) {
|
||
|
bRoom, err := json.Marshal(room)
|
||
|
if err != nil {
|
||
|
log.Error(fmt.Sprintf("marshal clubPlayingRoom:%+v err:%v", room, err))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = op.rdb.HSet(context.Background(), rdbkey.ClubPlayingRoomKey(clubId), room.RoomId, bRoom).Err()
|
||
|
if err != nil {
|
||
|
log.Error(fmt.Sprintf("redis key:%v hset room:%v err:%v", rdbkey.ClubPlayingRoomKey(clubId), room.RoomId, err))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 删除所有房间
|
||
|
func (op *ClubPlayingRoomOp) Del(clubId int, roomId int) {
|
||
|
err := op.rdb.HDel(context.Background(), rdbkey.ClubPlayingRoomKey(clubId), fmt.Sprintf("%d", roomId)).Err()
|
||
|
if err != nil {
|
||
|
log.Error(fmt.Sprintf("redis key:%v hdel room:%v err:%v", rdbkey.ClubPlayingRoomKey(clubId), roomId, err))
|
||
|
return
|
||
|
}
|
||
|
return
|
||
|
}
|