You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.6 KiB
Go

package live_logic
import (
"dcg/app/user_center/usercenter"
"dcg/config"
"dcg/game/manager"
pbCommon "dcg/game/pb/common"
pbMq "dcg/game/pb/mq"
"dcg/game/svc"
"dcg/pkg/cmd"
"git.noahlan.cn/northlan/ntools-go/logger"
"strconv"
)
type commonGameLogic struct {
svcCtx *svc.ServiceContext
*LiveGameLogic
}
func NewCommonLiveGameLogic(svcCtx *svc.ServiceContext) *LiveGameLogic {
resp := &commonGameLogic{
svcCtx: svcCtx,
LiveGameLogic: NewLiveGameLogic(-1, cmd.NewCMDParser(true, config.Config.Game.Common.Commands...)),
}
// 通用指令处理器
resp.RegisterCMDHandler(resp.handleQuery, "q", "查询")
resp.RegisterCMDHandler(resp.handleCheckIn, "打卡", "签到")
// 通用礼物处理器
resp.RegisterGiftHandler(resp.handleGift)
resp.RegisterNobilityHandler(resp.handleNobility)
return resp.LiveGameLogic
}
func (h *commonGameLogic) handleCheckIn(roomId int64, _ string, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoomId(roomId)
if err != nil {
return
}
respMsg := &pbCommon.CheckInMsg{User: user}
// RPC - 签到
resp, err := h.svcCtx.UserCenterRpc.UserCheckIn(h.svcCtx.Ctx, &usercenter.UserIdReq{UserId: user.UId})
if err != nil {
respMsg.Msg = "打卡失败UP主有罪。"
} else {
user.Integral = resp.Integral
respMsg.Code = resp.Code
respMsg.Msg = resp.Msg
respMsg.IntegralChange = resp.IntegralChange
respMsg.IsCritical = resp.IsCritical
}
room.Broadcast("user.checkIn", respMsg)
}
func (h *commonGameLogic) handleQuery(roomId int64, _ string, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoomId(roomId)
if err != nil {
return
}
rank := make([]*pbCommon.UserQueryMsg_RankItem, 0)
if rpcRank, err := h.svcCtx.UserCenterRpc.UserRankPvp(h.svcCtx.Ctx, &usercenter.UserRankReq{
UserId: user.UId,
Username: user.Uname,
AllRankType: true,
}); err == nil {
for _, item := range rpcRank.Items {
rank = append(rank, &pbCommon.UserQueryMsg_RankItem{
RankType: item.RankType,
Score: item.Score,
Rank: item.Pos,
})
}
}
room.Broadcast("user.query", &pbCommon.UserQueryMsg{
User: user,
Rank: rank,
TitleIds: []int64{},
})
}
func (h *commonGameLogic) handleGift(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) {
room, err := manager.GameManager.RoomByLiveRoomId(roomId)
if err != nil {
return
}
// 1. 发送通用礼物消息
room.Broadcast("live.gift.common", &pbCommon.GiftMsg{
User: user,
GiftId: gift.GiftId,
Num: gift.Num,
GiftName: gift.GiftName,
Price: gift.Price,
IsPaid: gift.IsPaid,
})
// 2. RPC 记录送礼,积分变动
req := &usercenter.UserSendGiftReq{
Platform: gift.Platform,
UserId: user.UId,
PUid: strconv.FormatInt(gift.Uid, 10),
RoomId: strconv.FormatInt(roomId, 10),
GiftId: gift.GiftId,
GiftName: gift.GiftName,
Num: gift.Num,
Price: gift.Price,
IsPaid: gift.IsPaid,
BattleId: manager.GameManager.BattleIdByLiveRoomId(roomId),
}
giftResp, err := h.svcCtx.UserCenterRpc.UserSendGift(h.svcCtx.Ctx, req)
if err != nil {
logger.SLog.Info("rpc 用户送礼记录失败,积分变动不通知了...")
return
}
user.Integral = giftResp.Integral.Integral // 更新最新积分
room.Broadcast("user.integral.change", &pbCommon.UserIntegralChanged{
User: user,
Change: giftResp.Integral.Change,
Integral: giftResp.Integral.Integral,
})
}
func (h *commonGameLogic) handleNobility(roomId int64, user *pbCommon.PbUser, msg *pbMq.MqGuardBuy) {
room, err := manager.GameManager.RoomByLiveRoomId(roomId)
if err != nil {
return
}
// 1. 发送通用礼物消息
room.Broadcast("live.gift.nobility", &pbCommon.GiftMsg{
User: user,
GiftId: msg.GiftId,
Num: int64(msg.Num),
GiftName: msg.GiftName,
Price: msg.Price,
IsPaid: true,
})
// 2. rpc
nobilityResp, err := h.svcCtx.UserCenterRpc.UserBuyNobility(h.svcCtx.Ctx, &usercenter.UserBuyNobilityReq{
Platform: msg.Platform,
UserId: user.UId,
PUid: strconv.FormatInt(msg.Uid, 10),
BattleId: manager.GameManager.BattleIdByLiveRoomId(roomId),
RoomId: strconv.FormatInt(roomId, 10),
GiftId: msg.GiftId,
GiftName: msg.GiftName,
Num: int64(msg.Num),
Price: msg.Price,
Level: int64(msg.GuardLevel),
StartTime: msg.StartTime,
EndTime: msg.EndTime,
})
if err != nil {
logger.SLog.Info("rpc 购买舰长记录失败,积分变动不通知了...")
return
}
user.Integral = nobilityResp.Integral.Integral // 更新最新积分
room.Broadcast("user.integral.change", &pbCommon.UserIntegralChanged{
User: user,
Change: nobilityResp.Integral.Change,
Integral: nobilityResp.Integral.Integral,
})
}