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.
dcg/game/mq/nobility_transfer.go

85 lines
2.5 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package mq
import (
"dcg/app/user_center/usercenter"
"dcg/game/live_logic"
pbCommon "dcg/game/pb/common"
pbMq "dcg/game/pb/mq"
"dcg/game/svc"
kfk "dcg/pkg/kafka"
"git.noahlan.cn/northlan/ntools-go/kafka"
"git.noahlan.cn/northlan/ntools-go/logger"
"github.com/Shopify/sarama"
"google.golang.org/protobuf/proto"
"strconv"
)
type NobilityTransferHandler struct {
svcCtx *svc.ServiceContext
msgHandle map[string]kafkaMsgHandlerFunc
ConsumerGroup *kafka.ConsumerGroup
}
func (h *NobilityTransferHandler) Init(svcCtx *svc.ServiceContext) {
h.svcCtx = svcCtx
cfg := svcCtx.Config.Kafka.GuardBuy
h.msgHandle = make(map[string]kafkaMsgHandlerFunc)
h.msgHandle[cfg.Topic] = h.handleTopic
var err error
h.ConsumerGroup, err = kafka.NewConsumerGroup(&kafka.ConsumerGroupConfig{
KafkaVersion: sarama.V3_1_0_0,
OffsetsInitial: sarama.OffsetNewest,
IsReturnErr: false,
UnMarshaler: kfk.ProtobufMarshaler,
}, cfg.Addr, []string{cfg.Topic}, cfg.ConsumerGroup)
if err != nil {
logger.SLog.Error(err)
}
}
func (h *NobilityTransferHandler) handleTopic(data []byte, _ string) {
// msg proto
var msgFromMq pbMq.MqNobilityBuy
err := proto.Unmarshal(data, &msgFromMq)
if err != nil {
logger.SLog.Error("unmarshal msg err", err)
return
}
// rpc创建或获取用户数据
rpcUser, err := h.svcCtx.UserCenterRpc.RetrievePlatformUser(h.svcCtx.Ctx, &usercenter.PlatformUserReq{
Platform: msgFromMq.Platform,
PUid: strconv.FormatInt(msgFromMq.Uid, 10),
PUname: msgFromMq.Uname,
PAvatar: msgFromMq.Avatar,
})
if err != nil {
logger.SLog.Info("rpc获取用户信息失败本条消息丢弃...")
return
}
pbUser := &pbCommon.PbUser{
UserId: rpcUser.Id,
Username: rpcUser.PUname,
Avatar: rpcUser.PAvatar,
Platform: rpcUser.Platform,
}
logger.SLog.Debugf("队列舰长消息: %s 送出 舰长|总督|提督", msgFromMq.Uname)
// 游戏逻辑处理
live_logic.LiveManager.HandleNobility(pbUser, &msgFromMq)
}
func (NobilityTransferHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil }
func (NobilityTransferHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
func (h *NobilityTransferHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for msg := range claim.Messages() {
//logger.SLog.Infow("kafka get info to mysql", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "msg", string(msg.Value))
if hFunc, ok := h.msgHandle[msg.Topic]; ok {
hFunc(msg.Value, string(msg.Key))
}
sess.MarkMessage(msg, "")
}
return nil
}