148 lines
4.1 KiB
Go
148 lines
4.1 KiB
Go
![]() |
package model
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"samba/pkg/log"
|
||
|
"samba/util/rdbkey"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// 获取玩家所在的俱乐部id集
|
||
|
func getUserInClubs(userId int64) []int {
|
||
|
// 获取该玩家所有俱乐部信息 set集
|
||
|
ctx := context.Background()
|
||
|
strClubIds, err := rdbBaseInfo.SMembers(ctx, rdbkey.UserInClubKey(userId)).Result()
|
||
|
if err != nil {
|
||
|
log.Error(err.Error())
|
||
|
return nil
|
||
|
}
|
||
|
var clubIds []int
|
||
|
for _, s := range strClubIds {
|
||
|
clubId, err := strconv.ParseInt(s, 10, 64)
|
||
|
if err != nil {
|
||
|
log.Error(err.Error())
|
||
|
continue
|
||
|
}
|
||
|
clubIds = append(clubIds, int(clubId))
|
||
|
}
|
||
|
return clubIds
|
||
|
}
|
||
|
|
||
|
// 添加俱乐部成员在线
|
||
|
func AddClubMemberOnline(userId int64) {
|
||
|
ctx := context.Background()
|
||
|
clubIds := getUserInClubs(userId)
|
||
|
for _, clubId := range clubIds {
|
||
|
if _, err := rdbBaseInfo.HSet(ctx, rdbkey.ClubMemberOnlineKey(clubId), userId, time.Now().Unix()).Result(); err != nil {
|
||
|
log.Error(fmt.Sprintf("AddClubMemberOnline.key:%v uid:%v err:%v", rdbkey.ClubMemberOnlineKey(clubId), userId, err.Error()))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 删除俱乐部成员在线
|
||
|
func DelClubMemberOnline(userId int64) {
|
||
|
ctx := context.Background()
|
||
|
clubIds := getUserInClubs(userId)
|
||
|
for _, clubId := range clubIds {
|
||
|
if _, err := rdbBaseInfo.HDel(ctx, rdbkey.ClubMemberOnlineKey(clubId), fmt.Sprintf("%v", userId)).Result(); err != nil {
|
||
|
log.Error(fmt.Sprintf("DelClubMemberOnline.key:%v uid:%v err:%v", rdbkey.ClubMemberOnlineKey(clubId), userId, err.Error()))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 记录玩家在哪个游戏中,以及开始游戏的时间信息
|
||
|
type UserInGameInfo struct {
|
||
|
Games map[int]int64 `json:"games"` // map[roomType]time
|
||
|
}
|
||
|
|
||
|
type ClubMemberInGameInfo struct {
|
||
|
Member map[int64]*UserInGameInfo
|
||
|
}
|
||
|
|
||
|
func GetAllClubMemberPlaying() map[int]ClubMemberInGameInfo {
|
||
|
ctx := context.Background()
|
||
|
clubInfos, err := NewClubInfoOp().LoadAll()
|
||
|
if err != nil {
|
||
|
log.Error(err.Error())
|
||
|
return map[int]ClubMemberInGameInfo{}
|
||
|
}
|
||
|
var allClubMemberInGameInfo = make(map[int]ClubMemberInGameInfo)
|
||
|
var userId int64
|
||
|
for _, club := range clubInfos {
|
||
|
usersInGame, err := rdbBaseInfo.HGetAll(ctx, rdbkey.ClubMemberPlayingKey(club.ID)).Result()
|
||
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
|
clubMembers := ClubMemberInGameInfo{Member: make(map[int64]*UserInGameInfo)}
|
||
|
for sUserId, games := range usersInGame {
|
||
|
userId, err = strconv.ParseInt(sUserId, 10, 64)
|
||
|
if err != nil {
|
||
|
log.Error(err.Error())
|
||
|
continue
|
||
|
}
|
||
|
gameInfo := &UserInGameInfo{}
|
||
|
if err = json.Unmarshal([]byte(games), gameInfo); err != nil {
|
||
|
log.Error(err.Error())
|
||
|
continue
|
||
|
}
|
||
|
clubMembers.Member[userId] = gameInfo
|
||
|
}
|
||
|
allClubMemberInGameInfo[club.ID] = clubMembers
|
||
|
}
|
||
|
return allClubMemberInGameInfo
|
||
|
}
|
||
|
|
||
|
// 添加俱乐部成员在玩情况
|
||
|
func AddClubMemberPlaying(userId int64, clubId int, roomId int) {
|
||
|
ctx := context.Background()
|
||
|
str, _ := rdbBaseInfo.HGet(ctx, rdbkey.ClubMemberPlayingKey(clubId), fmt.Sprintf("%v", userId)).Result()
|
||
|
gameInfo := UserInGameInfo{Games: make(map[int]int64)}
|
||
|
_ = json.Unmarshal([]byte(str), &gameInfo)
|
||
|
gameInfo.Games[roomId] = time.Now().Unix()
|
||
|
byteData, err := json.Marshal(gameInfo)
|
||
|
if err != nil {
|
||
|
log.Error(err.Error())
|
||
|
return
|
||
|
}
|
||
|
err = rdbBaseInfo.HSet(ctx, rdbkey.ClubMemberPlayingKey(clubId), fmt.Sprintf("%v", userId), string(byteData)).Err()
|
||
|
if err != nil {
|
||
|
log.Error(err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func DelClubMemberPlaying(userId int64, clubId int, roomType int) {
|
||
|
ctx := context.Background()
|
||
|
str, err := rdbBaseInfo.HGet(ctx, rdbkey.ClubMemberPlayingKey(clubId), fmt.Sprintf("%v", userId)).Result()
|
||
|
if str == "" {
|
||
|
return
|
||
|
}
|
||
|
var gameInfo UserInGameInfo
|
||
|
err = json.Unmarshal([]byte(str), &gameInfo)
|
||
|
if err != nil {
|
||
|
log.Error(err.Error())
|
||
|
return
|
||
|
}
|
||
|
delete(gameInfo.Games, roomType)
|
||
|
if len(gameInfo.Games) == 0 {
|
||
|
err = rdbBaseInfo.HDel(ctx, rdbkey.ClubMemberPlayingKey(clubId), fmt.Sprintf("%v", userId)).Err()
|
||
|
if err != nil {
|
||
|
log.Error(err.Error())
|
||
|
return
|
||
|
}
|
||
|
} else {
|
||
|
var byteData []byte
|
||
|
byteData, err = json.Marshal(gameInfo)
|
||
|
if err != nil {
|
||
|
log.Error(err.Error())
|
||
|
return
|
||
|
}
|
||
|
err = rdbBaseInfo.HSet(ctx, rdbkey.ClubMemberPlayingKey(clubId), fmt.Sprintf("%v", userId), string(byteData)).Err()
|
||
|
if err != nil {
|
||
|
log.Error(err.Error())
|
||
|
}
|
||
|
}
|
||
|
}
|