Commit 5dc1ce3a authored by hujiebin's avatar hujiebin

check cp 麦位置

parent c0aa4097
...@@ -193,7 +193,7 @@ const micUpdateScript = "local flag = redis.call('EXISTS', '{prefixGroupUserMic} ...@@ -193,7 +193,7 @@ const micUpdateScript = "local flag = redis.call('EXISTS', '{prefixGroupUserMic}
//上麦(自己), //上麦(自己),
//规则:1:加锁了不能上麦 2:麦上有人,不能上麦 //规则:1:加锁了不能上麦 2:麦上有人,不能上麦
//cpUserId如果有 //cpUserId如果有
func (mic *Mic) In(userId uint64, externalId string, cpUserId uint64) error { func (mic *Mic) In(userId uint64, externalId string) error {
// 群是否被封禁, 呃,,,呃,,,呃,,, // 群是否被封禁, 呃,,,呃,,,呃,,,
banned := GroupBanned{ImGroupId: mic.GroupUuid} banned := GroupBanned{ImGroupId: mic.GroupUuid}
if err := banned.Get(mic.model); err != gorm.ErrRecordNotFound { if err := banned.Get(mic.model); err != gorm.ErrRecordNotFound {
...@@ -218,7 +218,6 @@ func (mic *Mic) In(userId uint64, externalId string, cpUserId uint64) error { ...@@ -218,7 +218,6 @@ func (mic *Mic) In(userId uint64, externalId string, cpUserId uint64) error {
I: mic.I, I: mic.I,
ExternalId: externalId, ExternalId: externalId,
UserId: userId, UserId: userId,
CpUserId: cpUserId,
Forbid: false, Forbid: false,
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
}) })
......
...@@ -126,51 +126,14 @@ func redisLock(key string, sign string, expiration time.Duration, callBack func( ...@@ -126,51 +126,14 @@ func redisLock(key string, sign string, expiration time.Duration, callBack func(
//加入麦位,锁两秒 //加入麦位,锁两秒
func (s *GroupMicService) GroupMicIn(groupUuid string, i int, userId uint64, externalId string) error { func (s *GroupMicService) GroupMicIn(groupUuid string, i int, userId uint64, externalId string) error {
model := domain.CreateModelContext(s.svc.MyContext) return redisLock(redis_key.GetPrefixGroupMicUserInLock(userId), uuid.NewV4().String(), time.Second*2, func() error {
// 填充cp麦位
var cpUserId uint64
var cpI int
if cpRelation, _ := rpc.GetUserCpRelation(model, userId); cpRelation != nil {
micNumType, err := group_m.GetMicNumType(model, groupUuid)
if err != nil {
return err
}
_, micUsers, err := group_m.GetAllMic(groupUuid, micNumType)
if err != nil {
return err
}
for _, u := range micUsers {
if u.UserId == cpRelation.CpUserId {
cpUserId = u.UserId
cpI = u.I
break
}
}
}
err := redisLock(redis_key.GetPrefixGroupMicUserInLock(userId), uuid.NewV4().String(), time.Second*2, func() error {
model := domain.CreateModel(s.svc.CtxAndDb) model := domain.CreateModel(s.svc.CtxAndDb)
mic, err := group_m.GetMic(model, groupUuid, i) mic, err := group_m.GetMic(model, groupUuid, i)
if err != nil { if err != nil {
return err return err
} }
return mic.In(userId, externalId, cpUserId) return mic.In(userId, externalId)
}) })
if err != nil {
return err
}
// 更新麦上cp的信息
if cpUserId > 0 {
user, _ := user_m.GetUser(model, cpUserId)
return redisLock(redis_key.GetPrefixGroupMicUserInLock(cpUserId), uuid.NewV4().String(), time.Second*2, func() error {
model := domain.CreateModel(s.svc.CtxAndDb)
mic, err := group_m.GetMic(model, groupUuid, cpI)
if err != nil {
return err
}
return mic.Update(cpUserId, user.ExternalId, userId)
})
}
return nil
} }
//离开麦位 //离开麦位
...@@ -367,3 +330,55 @@ func (s *GroupMicService) IncrGroupPowerOnMicExpAndTime(groupId string, userId u ...@@ -367,3 +330,55 @@ func (s *GroupMicService) IncrGroupPowerOnMicExpAndTime(groupId string, userId u
} }
return nil return nil
} }
func (s *GroupMicService) CheckCpOnMic(groupUuid string) {
model := domain.CreateModelContext(s.svc.MyContext)
// 填充cp麦位
micNumType, err := group_m.GetMicNumType(model, groupUuid)
if err != nil {
return
}
_, micUsers, err := group_m.GetAllMic(groupUuid, micNumType)
if err != nil {
return
}
// userId->micIndex
var userMicIndex = make(map[uint64]int)
var userIds []uint64
for _, u := range micUsers {
userMicIndex[u.UserId] = u.I
userIds = append(userIds, u.UserId)
}
cpPairs, err := rpc.MGetUserCpPairs(model, userIds)
// 更新麦上cp的信息
if err != nil {
model.Log.Errorf("MGetUserCpPairs fail:%v", err)
return
}
users, err := user_m.GetUserMapByIds(model, userIds)
if err != nil {
model.Log.Errorf("GetUserMapByIds fail:%v", err)
return
}
for _, pairs := range cpPairs {
userId := pairs[0]
cpUserId := pairs[1]
redisLock(redis_key.GetPrefixGroupMicUserInLock(userId), uuid.NewV4().String(), time.Second*2, func() error {
model := domain.CreateModel(s.svc.CtxAndDb)
mic, err := group_m.GetMic(model, groupUuid, userMicIndex[userId])
if err != nil {
return err
}
return mic.Update(userId, users[userId].ExternalId, cpUserId)
})
redisLock(redis_key.GetPrefixGroupMicUserInLock(cpUserId), uuid.NewV4().String(), time.Second*2, func() error {
model := domain.CreateModel(s.svc.CtxAndDb)
mic, err := group_m.GetMic(model, groupUuid, userMicIndex[cpUserId])
if err != nil {
return err
}
return mic.Update(cpUserId, users[cpUserId].ExternalId, userId)
})
}
return
}
...@@ -600,6 +600,8 @@ func GroupMicIn(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -600,6 +600,8 @@ func GroupMicIn(c *gin.Context) (*mycontext.MyContext, error) {
} else { } else {
resp.ResponseOk(c, nil) resp.ResponseOk(c, nil)
} }
// check cp 麦位置
go group_mic_s.NewGroupPowerService(myContext).CheckCpOnMic(groupUuid)
return myContext, nil return myContext, nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment