From 84d1cb02887b9f3d4d94319d249053585fd49c75 Mon Sep 17 00:00:00 2001 From: NorthLan <6995syu@163.com> Date: Fri, 22 Apr 2022 20:50:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yml | 3 +- config/Config.go | 16 +- game/msg_transfer/danmaku/msg_to_db.go | 3 +- game/msg_transfer/danmaku/msg_to_push.go | 8 +- game/msg_transfer/gift/msg.go | 3 + game/msg_transfer/gift/msg_to_push.go | 69 ++++++ game/msg_transfer/init.go | 4 + game/pb/danmaku.pb.go | 179 -------------- game/pb/danmaku.proto | 13 - game/pb/mq/gen.bat | 1 + game/pb/mq/mq.pb.go | 299 +++++++++++++++++++++++ game/pb/mq/mq.proto | 23 ++ main.go | 7 + pkg/logger/logger.go | 39 --- 14 files changed, 419 insertions(+), 248 deletions(-) create mode 100644 game/msg_transfer/gift/msg.go create mode 100644 game/msg_transfer/gift/msg_to_push.go delete mode 100644 game/pb/danmaku.pb.go delete mode 100644 game/pb/danmaku.proto create mode 100644 game/pb/mq/gen.bat create mode 100644 game/pb/mq/mq.pb.go create mode 100644 game/pb/mq/mq.proto diff --git a/config.yml b/config.yml index 5299bb0..a5beb07 100644 --- a/config.yml +++ b/config.yml @@ -1,6 +1,6 @@ Server: Debug: false - Listen: 0.0.0.0:8888 + Listen: 0.0.0.0:8889 Command: Regex: "([jJ])|([sS])|([wW])|([cC]\\d)|([mM]\\d)" Log: @@ -25,5 +25,6 @@ Kafka: Addr: [ "127.0.0.1:9093" ] Topic: "gift" ConsumerGroupId: + GiftToPush: "giftToPush" MsgToPush: "msgToPush" MsgToDb: "msgToDb" \ No newline at end of file diff --git a/config/Config.go b/config/Config.go index 6da2677..0ee734d 100644 --- a/config/Config.go +++ b/config/Config.go @@ -5,13 +5,6 @@ import ( "fmt" c "github.com/gookit/config/v2" "github.com/gookit/config/v2/yaml" - "path/filepath" - "runtime" -) - -var ( - _, b, _, _ = runtime.Caller(0) - Root = filepath.Join(filepath.Dir(b), "../") ) var Config config @@ -37,8 +30,9 @@ type ( Gift Kafka } ConsumerGroupId struct { - MsgToPush string - MsgToDb string + GiftToPush string + MsgToPush string + MsgToDb string } Command struct { Regex string @@ -46,11 +40,11 @@ type ( } ) -func init() { +func Init(filepath string) { var err error c.AddDriver(yaml.Driver) - err = c.LoadFiles(Root + "/config.yml") + err = c.LoadFiles(filepath) if err != nil { panic(err) } diff --git a/game/msg_transfer/danmaku/msg_to_db.go b/game/msg_transfer/danmaku/msg_to_db.go index 048ad7c..964e008 100644 --- a/game/msg_transfer/danmaku/msg_to_db.go +++ b/game/msg_transfer/danmaku/msg_to_db.go @@ -4,6 +4,7 @@ import ( "dcg/config" "dcg/game/command" "dcg/game/pb" + pbMq "dcg/game/pb/mq" "dcg/game/svc" "dcg/pkg/kafka" "dcg/pkg/logger" @@ -57,7 +58,7 @@ func (h *MsgToDbHandler) handleAllCmd(_ string, user *pb.User) { func (h *MsgToDbHandler) handleDanmaku(data []byte, msgKey string) { // danmaku msg proto - var msgFromMq pb.Danmaku + var msgFromMq pbMq.MqDanmaku err := proto.Unmarshal(data, &msgFromMq) if err != nil { logger.SLog.Error("unmarshal msg err", err) diff --git a/game/msg_transfer/danmaku/msg_to_push.go b/game/msg_transfer/danmaku/msg_to_push.go index 621db0c..4538a55 100644 --- a/game/msg_transfer/danmaku/msg_to_push.go +++ b/game/msg_transfer/danmaku/msg_to_push.go @@ -4,12 +4,12 @@ import ( "dcg/config" "dcg/game/command" "dcg/game/pb" + pbMq "dcg/game/pb/mq" "dcg/game/svc" "dcg/pkg/kafka" "dcg/pkg/logger" "github.com/Shopify/sarama" "github.com/golang/protobuf/proto" - "github.com/spf13/cast" "regexp" ) @@ -67,7 +67,7 @@ func (h *MsgToPushHandler) handleCreateUnit(cmd string, user *pb.User) { unit := cmd[1] h.ctx.RoomManager.Broadcast("game.createUnit", &pb.CreateUnit{ User: user, - Unit: cast.ToString(unit), + Unit: string(unit), }) } @@ -78,7 +78,7 @@ func (h *MsgToPushHandler) handleMove(cmd string, user *pb.User) { line := cmd[1] h.ctx.RoomManager.Broadcast("game.move", &pb.Move{ User: user, - Line: cast.ToString(line), + Line: string(line), }) } @@ -88,7 +88,7 @@ func (h *MsgToPushHandler) handleWai(cmd string, user *pb.User) { func (h *MsgToPushHandler) handleDanmaku(data []byte, msgKey string) { // danmaku msg proto - var msgFromMq pb.Danmaku + var msgFromMq pbMq.MqDanmaku err := proto.Unmarshal(data, &msgFromMq) if err != nil { logger.SLog.Error("unmarshal msg err", err) diff --git a/game/msg_transfer/gift/msg.go b/game/msg_transfer/gift/msg.go new file mode 100644 index 0000000..e0d031e --- /dev/null +++ b/game/msg_transfer/gift/msg.go @@ -0,0 +1,3 @@ +package gift + +type msgHandlerFunc func(data []byte, msgKey string) diff --git a/game/msg_transfer/gift/msg_to_push.go b/game/msg_transfer/gift/msg_to_push.go new file mode 100644 index 0000000..749d094 --- /dev/null +++ b/game/msg_transfer/gift/msg_to_push.go @@ -0,0 +1,69 @@ +package gift + +import ( + "dcg/config" + "dcg/game/pb" + pbMq "dcg/game/pb/mq" + "dcg/game/svc" + "dcg/pkg/kafka" + "dcg/pkg/logger" + "github.com/Shopify/sarama" + "github.com/golang/protobuf/proto" +) + +type MsgToPushHandler struct { + ctx *svc.ServiceContext + msgHandle map[string]msgHandlerFunc + ConsumerGroup *kafka.ConsumerGroup +} + +func (h *MsgToPushHandler) Init(ctx *svc.ServiceContext) { + h.ctx = ctx + + cfg := config.Config.Kafka.Gift + h.msgHandle = make(map[string]msgHandlerFunc) + h.msgHandle["gift"] = h.handleGift + + var err error + h.ConsumerGroup, err = kafka.NewConsumerGroup(&kafka.ConsumerGroupConfig{ + KafkaVersion: sarama.V3_1_0_0, + OffsetsInitial: sarama.OffsetNewest, + IsReturnErr: false, + }, cfg.Addr, []string{cfg.Topic}, config.Config.ConsumerGroupId.GiftToPush) + + if err != nil { + logger.SLog.Error(err) + } +} + +func (h *MsgToPushHandler) handleGift(data []byte, msgKey string) { + // msg proto + var msgFromMq pbMq.MqGift + err := proto.Unmarshal(data, &msgFromMq) + if err != nil { + logger.SLog.Error("unmarshal msg err", err) + return + } + h.ctx.RoomManager.Broadcast("game.gift", &pb.Gift{ + User: &pb.User{ + UId: msgFromMq.Uid, + Uname: msgFromMq.Uname, + }, + GiftId: msgFromMq.GiftId, + TotalCoin: msgFromMq.TotalCoin, + }) + logger.SLog.Debugf("队列礼物消息: %s 投喂 %s 价值 %d", msgFromMq.Uname, msgFromMq.GiftName, msgFromMq.TotalCoin) +} + +func (MsgToPushHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil } +func (MsgToPushHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil } +func (h *MsgToPushHandler) 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 +} diff --git a/game/msg_transfer/init.go b/game/msg_transfer/init.go index d56714d..82ea3fa 100644 --- a/game/msg_transfer/init.go +++ b/game/msg_transfer/init.go @@ -2,17 +2,21 @@ package msg_transfer import ( "dcg/game/msg_transfer/danmaku" + "dcg/game/msg_transfer/gift" "dcg/game/svc" ) var ( danmakuMsgToPush danmaku.MsgToPushHandler + giftMsgToPush gift.MsgToPushHandler ) func Init(ctx *svc.ServiceContext) { danmakuMsgToPush.Init(ctx) + giftMsgToPush.Init(ctx) } func Run() { go danmakuMsgToPush.ConsumerGroup.RegisterHandlerAndConsumer(&danmakuMsgToPush) + go giftMsgToPush.ConsumerGroup.RegisterHandlerAndConsumer(&giftMsgToPush) } diff --git a/game/pb/danmaku.pb.go b/game/pb/danmaku.pb.go deleted file mode 100644 index d8887c4..0000000 --- a/game/pb/danmaku.pb.go +++ /dev/null @@ -1,179 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.28.0 -// protoc v3.19.4 -// source: danmaku.proto - -package pb - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -type Danmaku struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Platform string `protobuf:"bytes,1,opt,name=platform,proto3" json:"platform,omitempty"` - Uid int64 `protobuf:"varint,2,opt,name=uid,proto3" json:"uid,omitempty"` - Uname string `protobuf:"bytes,3,opt,name=uname,proto3" json:"uname,omitempty"` - Content string `protobuf:"bytes,4,opt,name=content,proto3" json:"content,omitempty"` - SendTime int64 `protobuf:"varint,5,opt,name=sendTime,proto3" json:"sendTime,omitempty"` -} - -func (x *Danmaku) Reset() { - *x = Danmaku{} - if protoimpl.UnsafeEnabled { - mi := &file_danmaku_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *Danmaku) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Danmaku) ProtoMessage() {} - -func (x *Danmaku) ProtoReflect() protoreflect.Message { - mi := &file_danmaku_proto_msgTypes[0] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use Danmaku.ProtoReflect.Descriptor instead. -func (*Danmaku) Descriptor() ([]byte, []int) { - return file_danmaku_proto_rawDescGZIP(), []int{0} -} - -func (x *Danmaku) GetPlatform() string { - if x != nil { - return x.Platform - } - return "" -} - -func (x *Danmaku) GetUid() int64 { - if x != nil { - return x.Uid - } - return 0 -} - -func (x *Danmaku) GetUname() string { - if x != nil { - return x.Uname - } - return "" -} - -func (x *Danmaku) GetContent() string { - if x != nil { - return x.Content - } - return "" -} - -func (x *Danmaku) GetSendTime() int64 { - if x != nil { - return x.SendTime - } - return 0 -} - -var File_danmaku_proto protoreflect.FileDescriptor - -var file_danmaku_proto_rawDesc = []byte{ - 0x0a, 0x0d, 0x64, 0x61, 0x6e, 0x6d, 0x61, 0x6b, 0x75, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, - 0x02, 0x70, 0x62, 0x22, 0x83, 0x01, 0x0a, 0x07, 0x44, 0x61, 0x6e, 0x6d, 0x61, 0x6b, 0x75, 0x12, - 0x1a, 0x0a, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x12, 0x10, 0x0a, 0x03, 0x75, - 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x14, 0x0a, - 0x05, 0x75, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x75, 0x6e, - 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x04, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x1a, 0x0a, - 0x08, 0x73, 0x65, 0x6e, 0x64, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, - 0x08, 0x73, 0x65, 0x6e, 0x64, 0x54, 0x69, 0x6d, 0x65, 0x42, 0x05, 0x5a, 0x03, 0x2f, 0x70, 0x62, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, -} - -var ( - file_danmaku_proto_rawDescOnce sync.Once - file_danmaku_proto_rawDescData = file_danmaku_proto_rawDesc -) - -func file_danmaku_proto_rawDescGZIP() []byte { - file_danmaku_proto_rawDescOnce.Do(func() { - file_danmaku_proto_rawDescData = protoimpl.X.CompressGZIP(file_danmaku_proto_rawDescData) - }) - return file_danmaku_proto_rawDescData -} - -var file_danmaku_proto_msgTypes = make([]protoimpl.MessageInfo, 1) -var file_danmaku_proto_goTypes = []interface{}{ - (*Danmaku)(nil), // 0: pb.Danmaku -} -var file_danmaku_proto_depIdxs = []int32{ - 0, // [0:0] is the sub-list for method output_type - 0, // [0:0] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name -} - -func init() { file_danmaku_proto_init() } -func file_danmaku_proto_init() { - if File_danmaku_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_danmaku_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Danmaku); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_danmaku_proto_rawDesc, - NumEnums: 0, - NumMessages: 1, - NumExtensions: 0, - NumServices: 0, - }, - GoTypes: file_danmaku_proto_goTypes, - DependencyIndexes: file_danmaku_proto_depIdxs, - MessageInfos: file_danmaku_proto_msgTypes, - }.Build() - File_danmaku_proto = out.File - file_danmaku_proto_rawDesc = nil - file_danmaku_proto_goTypes = nil - file_danmaku_proto_depIdxs = nil -} diff --git a/game/pb/danmaku.proto b/game/pb/danmaku.proto deleted file mode 100644 index 0ad4ec5..0000000 --- a/game/pb/danmaku.proto +++ /dev/null @@ -1,13 +0,0 @@ -syntax = "proto3"; - -package pb; - -option go_package = "/pb"; - -message Danmaku { - string platform = 1; - int64 uid = 2; - string uname = 3; - string content = 4; - int64 sendTime = 5; -} \ No newline at end of file diff --git a/game/pb/mq/gen.bat b/game/pb/mq/gen.bat new file mode 100644 index 0000000..a9eb2ba --- /dev/null +++ b/game/pb/mq/gen.bat @@ -0,0 +1 @@ +protoc --go_opt=paths=source_relative --go-grpc_opt=paths=source_relative --go-grpc_opt=require_unimplemented_servers=false --go_out=. --go-grpc_out=. --proto_path=. *.proto \ No newline at end of file diff --git a/game/pb/mq/mq.pb.go b/game/pb/mq/mq.pb.go new file mode 100644 index 0000000..20332ed --- /dev/null +++ b/game/pb/mq/mq.pb.go @@ -0,0 +1,299 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.0 +// protoc v3.19.4 +// source: mq.proto + +package pbMq + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type MqDanmaku struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Platform string `protobuf:"bytes,1,opt,name=platform,proto3" json:"platform,omitempty"` + Uid int64 `protobuf:"varint,2,opt,name=uid,proto3" json:"uid,omitempty"` + Uname string `protobuf:"bytes,3,opt,name=uname,proto3" json:"uname,omitempty"` + Content string `protobuf:"bytes,4,opt,name=content,proto3" json:"content,omitempty"` + SendTime int64 `protobuf:"varint,5,opt,name=sendTime,proto3" json:"sendTime,omitempty"` +} + +func (x *MqDanmaku) Reset() { + *x = MqDanmaku{} + if protoimpl.UnsafeEnabled { + mi := &file_mq_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *MqDanmaku) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*MqDanmaku) ProtoMessage() {} + +func (x *MqDanmaku) ProtoReflect() protoreflect.Message { + mi := &file_mq_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use MqDanmaku.ProtoReflect.Descriptor instead. +func (*MqDanmaku) Descriptor() ([]byte, []int) { + return file_mq_proto_rawDescGZIP(), []int{0} +} + +func (x *MqDanmaku) GetPlatform() string { + if x != nil { + return x.Platform + } + return "" +} + +func (x *MqDanmaku) GetUid() int64 { + if x != nil { + return x.Uid + } + return 0 +} + +func (x *MqDanmaku) GetUname() string { + if x != nil { + return x.Uname + } + return "" +} + +func (x *MqDanmaku) GetContent() string { + if x != nil { + return x.Content + } + return "" +} + +func (x *MqDanmaku) GetSendTime() int64 { + if x != nil { + return x.SendTime + } + return 0 +} + +type MqGift struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Platform string `protobuf:"bytes,1,opt,name=platform,proto3" json:"platform,omitempty"` + Uid int64 `protobuf:"varint,2,opt,name=uid,proto3" json:"uid,omitempty"` + Uname string `protobuf:"bytes,3,opt,name=uname,proto3" json:"uname,omitempty"` + GiftId int32 `protobuf:"varint,4,opt,name=giftId,proto3" json:"giftId,omitempty"` + GiftName string `protobuf:"bytes,5,opt,name=giftName,proto3" json:"giftName,omitempty"` + TotalCoin int64 `protobuf:"varint,6,opt,name=totalCoin,proto3" json:"totalCoin,omitempty"` + SendTime int64 `protobuf:"varint,7,opt,name=sendTime,proto3" json:"sendTime,omitempty"` +} + +func (x *MqGift) Reset() { + *x = MqGift{} + if protoimpl.UnsafeEnabled { + mi := &file_mq_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *MqGift) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*MqGift) ProtoMessage() {} + +func (x *MqGift) ProtoReflect() protoreflect.Message { + mi := &file_mq_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use MqGift.ProtoReflect.Descriptor instead. +func (*MqGift) Descriptor() ([]byte, []int) { + return file_mq_proto_rawDescGZIP(), []int{1} +} + +func (x *MqGift) GetPlatform() string { + if x != nil { + return x.Platform + } + return "" +} + +func (x *MqGift) GetUid() int64 { + if x != nil { + return x.Uid + } + return 0 +} + +func (x *MqGift) GetUname() string { + if x != nil { + return x.Uname + } + return "" +} + +func (x *MqGift) GetGiftId() int32 { + if x != nil { + return x.GiftId + } + return 0 +} + +func (x *MqGift) GetGiftName() string { + if x != nil { + return x.GiftName + } + return "" +} + +func (x *MqGift) GetTotalCoin() int64 { + if x != nil { + return x.TotalCoin + } + return 0 +} + +func (x *MqGift) GetSendTime() int64 { + if x != nil { + return x.SendTime + } + return 0 +} + +var File_mq_proto protoreflect.FileDescriptor + +var file_mq_proto_rawDesc = []byte{ + 0x0a, 0x08, 0x6d, 0x71, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, 0x70, 0x62, 0x22, 0x85, + 0x01, 0x0a, 0x09, 0x4d, 0x71, 0x44, 0x61, 0x6e, 0x6d, 0x61, 0x6b, 0x75, 0x12, 0x1a, 0x0a, 0x08, + 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, + 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x75, 0x6e, + 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x75, 0x6e, 0x61, 0x6d, 0x65, + 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x73, 0x65, + 0x6e, 0x64, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x73, 0x65, + 0x6e, 0x64, 0x54, 0x69, 0x6d, 0x65, 0x22, 0xba, 0x01, 0x0a, 0x06, 0x4d, 0x71, 0x47, 0x69, 0x66, + 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x12, 0x10, 0x0a, + 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, + 0x14, 0x0a, 0x05, 0x75, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, + 0x75, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x67, 0x69, 0x66, 0x74, 0x49, 0x64, 0x18, + 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x67, 0x69, 0x66, 0x74, 0x49, 0x64, 0x12, 0x1a, 0x0a, + 0x08, 0x67, 0x69, 0x66, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x08, 0x67, 0x69, 0x66, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x6f, 0x74, + 0x61, 0x6c, 0x43, 0x6f, 0x69, 0x6e, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74, 0x6f, + 0x74, 0x61, 0x6c, 0x43, 0x6f, 0x69, 0x6e, 0x12, 0x1a, 0x0a, 0x08, 0x73, 0x65, 0x6e, 0x64, 0x54, + 0x69, 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x73, 0x65, 0x6e, 0x64, 0x54, + 0x69, 0x6d, 0x65, 0x42, 0x07, 0x5a, 0x05, 0x2f, 0x70, 0x62, 0x4d, 0x71, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_mq_proto_rawDescOnce sync.Once + file_mq_proto_rawDescData = file_mq_proto_rawDesc +) + +func file_mq_proto_rawDescGZIP() []byte { + file_mq_proto_rawDescOnce.Do(func() { + file_mq_proto_rawDescData = protoimpl.X.CompressGZIP(file_mq_proto_rawDescData) + }) + return file_mq_proto_rawDescData +} + +var file_mq_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_mq_proto_goTypes = []interface{}{ + (*MqDanmaku)(nil), // 0: pb.MqDanmaku + (*MqGift)(nil), // 1: pb.MqGift +} +var file_mq_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_mq_proto_init() } +func file_mq_proto_init() { + if File_mq_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_mq_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*MqDanmaku); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_mq_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*MqGift); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_mq_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_mq_proto_goTypes, + DependencyIndexes: file_mq_proto_depIdxs, + MessageInfos: file_mq_proto_msgTypes, + }.Build() + File_mq_proto = out.File + file_mq_proto_rawDesc = nil + file_mq_proto_goTypes = nil + file_mq_proto_depIdxs = nil +} diff --git a/game/pb/mq/mq.proto b/game/pb/mq/mq.proto new file mode 100644 index 0000000..769c0b6 --- /dev/null +++ b/game/pb/mq/mq.proto @@ -0,0 +1,23 @@ +syntax = "proto3"; + +package pb; + +option go_package = "/pbMq"; + +message MqDanmaku { + string platform = 1; + int64 uid = 2; + string uname = 3; + string content = 4; + int64 sendTime = 5; +} + +message MqGift { + string platform = 1; + int64 uid = 2; + string uname = 3; + int32 giftId = 4; + string giftName = 5; + int64 totalCoin = 6; + int64 sendTime = 7; +} \ No newline at end of file diff --git a/main.go b/main.go index ecefd5e..83a7d0e 100644 --- a/main.go +++ b/main.go @@ -5,11 +5,18 @@ import ( "dcg/game/msg_transfer" "dcg/game/svc" "dcg/pkg/logger" + "flag" "git.noahlan.cn/northlan/ngs" "git.noahlan.cn/northlan/ngs/serialize/protobuf" ) +var configFile = flag.String("f", "./config.yml", "the config file") + func main() { + flag.Parse() + + config.Init(*configFile) + _ = logger.InitLogger(&config.Config.Log.File, &config.Config.Log.Console) defer logger.Sync() diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 5988bd3..6314624 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -5,10 +5,7 @@ import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" "os" - "path" "path/filepath" - "runtime" - "strings" ) var Log *zap.Logger @@ -142,39 +139,3 @@ func isExist(path string) bool { _, err := os.Stat(path) return err == nil || os.IsExist(err) } - -// 最终方案-全兼容 -func getCurrentAbPath() string { - dir := getCurrentAbPathByExecutable() - if strings.Contains(dir, getTmpDir()) { - return getCurrentAbPathByCaller() - } - return dir -} - -// 获取系统临时目录,兼容go run -func getTmpDir() string { - dir := os.Getenv("TEMP") - if dir == "" { - dir = os.Getenv("TMP") - } - res, _ := filepath.EvalSymlinks(dir) - return res -} - -// 获取当前执行文件绝对路径 -func getCurrentAbPathByExecutable() string { - exePath, _ := os.Executable() - res, _ := filepath.EvalSymlinks(filepath.Dir(exePath)) - return res -} - -// 获取当前执行文件绝对路径(go run) -func getCurrentAbPathByCaller() string { - var abPath string - _, filename, _, ok := runtime.Caller(0) - if ok { - abPath = path.Dir(filename) - } - return abPath -}