From 927b2652a7545bec77fa83fac6941a11fc9cef6f Mon Sep 17 00:00:00 2001 From: zqj <582132116@qq.com> Date: Mon, 20 Jun 2022 10:55:49 +0800 Subject: [PATCH] feat: EmailNewNty, 新邮件通知 --- cmd/gameserver/gmaction/GmAction.go | 16 ++++------------ cmd/gameserver/service/agent.go | 6 +----- cmd/test/action/protocode.go | 1 + common/components/conn.go | 22 ++++++++++++++++++++++ common/components/icompontents.go | 1 + models/email.go | 15 --------------- models/role.go | 10 ++-------- models/rolePlugin.go | 43 +++++++++++++++++++++++++++++-------------- pb/game.pb.go | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------ pb/protocode.pb.go | 12 ++++++++---- 10 files changed, 157 insertions(+), 82 deletions(-) diff --git a/cmd/gameserver/gmaction/GmAction.go b/cmd/gameserver/gmaction/GmAction.go index 91260ff..035dbaf 100644 --- a/cmd/gameserver/gmaction/GmAction.go +++ b/cmd/gameserver/gmaction/GmAction.go @@ -2,7 +2,6 @@ package gmaction import ( - "pro2d/common" "pro2d/common/logger" "pro2d/models" "pro2d/pb" @@ -114,24 +113,17 @@ func (gm *GmAction) GMEmail(role *models.RoleModel, params GMParams) { if !ok { attachments = "" } - id, err := common.GetNextEmail() - if err != nil { - logger.Error(err) - return - } + email := &pb.Email{ - Id: id, RoleId: role.Data.Id, Title: title, Stitle: stitle, Content: content, Attachments: attachments, - Status: 0, - CreateTime: common.Timex(), } - m := models.NewEmailModelPB(email) - err = m.Create() + err := role.InsertEmail(email) if err != nil { - logger.Error(err) + logger.Error(err.Error()) + return } } diff --git a/cmd/gameserver/service/agent.go b/cmd/gameserver/service/agent.go index 10f06ef..9e9b35a 100644 --- a/cmd/gameserver/service/agent.go +++ b/cmd/gameserver/service/agent.go @@ -100,11 +100,7 @@ func (c *Agent) SendMsg(errCode int32, cmd uint32, msg interface{}, preserve uin if msg == nil || errCode != 0 { return c.Send(errCode, cmd, nil, preserve) } - rsp, err := proto.Marshal(msg.(proto.Message)) - if err != nil { - return c.Send(-100, cmd, nil, preserve) - } - return c.Send(errCode, cmd, rsp, preserve) + return c.SendPB(errCode, cmd, msg.(proto.Message), preserve) } func (c *Agent) OnMessage(msg components.IMessage) error { diff --git a/cmd/test/action/protocode.go b/cmd/test/action/protocode.go index 2b637a9..6c2e73b 100644 --- a/cmd/test/action/protocode.go +++ b/cmd/test/action/protocode.go @@ -23,6 +23,7 @@ func GetTestActionMap() map[interface{}]interface{} { am[uint32(pb.ProtoCode_RoleUpdatePropertyNty)] = RoleUpdatePropertyNty am[uint32(pb.ProtoCode_RoleUpdateItemsNty)] = RoleUpdateItemsNty am[uint32(pb.ProtoCode_EquipmentAddNty)] = EquipmentAddNty + am[uint32(pb.ProtoCode_EmailNewNty)] = EmailNewNty return am } diff --git a/common/components/conn.go b/common/components/conn.go index 38c8c9e..eb19e0d 100644 --- a/common/components/conn.go +++ b/common/components/conn.go @@ -3,6 +3,7 @@ package components import ( "bufio" "fmt" + "github.com/golang/protobuf/proto" "net" "pro2d/common" "pro2d/common/logger" @@ -140,6 +141,27 @@ func (c *Connection) Send(errCode int32, cmd uint32, data []byte, preserve uint3 } } +func (c *Connection) SendPB(errCode int32, cmd uint32, data proto.Message, preserve uint32) error { + pbData, err := proto.Marshal(data) + if err != nil { + return err + } + buf, err := c.splitter.Pack(cmd, pbData, errCode, preserve) + if err != nil { + return err + } + + sendTimeout := time.NewTimer(5 * time.Millisecond) + defer sendTimeout.Stop() + // 发送超时 + select { + case <-sendTimeout.C: + return fmt.Errorf("send buff msg timeout") + case c.WBuffer <- buf: + return nil + } +} + func (c *Connection) SendSuccess(cmd uint32, data []byte, preserve uint32) error { buf, err := c.splitter.Pack(cmd, data, 0, preserve) if err != nil { diff --git a/common/components/icompontents.go b/common/components/icompontents.go index 5cc6aa9..9ebfebc 100644 --- a/common/components/icompontents.go +++ b/common/components/icompontents.go @@ -53,6 +53,7 @@ type ( Start() Stop() Send(errCode int32, cmd uint32, b []byte, preserve uint32) error + SendPB(errCode int32, cmd uint32, b proto.Message, preserve uint32) error SendSuccess(cmd uint32, b []byte, preserve uint32) error CustomChan() chan<- func() diff --git a/models/email.go b/models/email.go index 03fdbba..09bb525 100644 --- a/models/email.go +++ b/models/email.go @@ -1,7 +1,6 @@ package models import ( - "pro2d/common" "pro2d/common/components" "pro2d/pb" ) @@ -30,20 +29,6 @@ func NewEmailModelPB(email *pb.Email) *EmailModel { return m } -func InsertEmail(email *pb.Email) bool { - data := &EmailModel{ - ISchema: NewSchema(email.Id, email), - Data: email, - } - - data.SetProperty("createtime", common.Timex()) - err := data.Create() - if err != nil { - return false - } - return true -} - func (m *EmailModel) Log(role *RoleModel, action int32) { //{desc = "onMail", int1 = self:getProperty("id"), int2 = self:getProperty("status"), cint1 = self:getProperty("emailId"), // short1 = action, key1=self:getProperty("title"), key2=self:getProperty("attachments")}) diff --git a/models/role.go b/models/role.go index f0da807..afa2911 100644 --- a/models/role.go +++ b/models/role.go @@ -2,7 +2,6 @@ package models import ( "fmt" - "github.com/golang/protobuf/proto" "pro2d/common" "pro2d/common/components" "pro2d/common/db/mongoproxy" @@ -208,13 +207,8 @@ func (m *RoleModel) UpdateProperties(property map[string]interface{}, notify boo Id: ids, Role: role, } - if rsp, err := proto.Marshal(update); err != nil { - logger.Error("id %s, err:", m.Data.Id, err) - return - } else { - if m.GetConn() != nil && notify { - m.GetConn().Send(0, uint32(pb.ProtoCode_RoleUpdatePropertyNty), rsp, 0) - } + if m.GetConn() != nil && notify { + m.GetConn().SendPB(0, uint32(pb.ProtoCode_RoleUpdatePropertyNty), update, 0) } } diff --git a/models/rolePlugin.go b/models/rolePlugin.go index 25ada29..f3694f6 100644 --- a/models/rolePlugin.go +++ b/models/rolePlugin.go @@ -64,15 +64,10 @@ func (m *RoleModel) AddItem(key string, count int32, param *pb.LogConf) bool { m.MyLog("in_item", param) } - rsp, err := proto.Marshal(&pb.RoleUpdateItemsNty{Items: fmt.Sprintf("%s=%d", key, num)}) - if err != nil { - logger.Error(err.Error()) - return true - } - if m.GetConn() != nil { - m.GetConn().Send(0, uint32(pb.ProtoCode_RoleUpdateItemsNty), rsp, 0) + m.GetConn().SendPB(0, uint32(pb.ProtoCode_RoleUpdateItemsNty), &pb.RoleUpdateItemsNty{Items: fmt.Sprintf("%s=%d", key, num)}, 0) } + return true } @@ -103,14 +98,8 @@ func (m *RoleModel) AddItems(params common.IMapStringNum, logconf *pb.LogConf) b m.SetProperty("items", common.MapNumToString(m.Items)) - rsp, err := proto.Marshal(&pb.RoleUpdateItemsNty{Items: common.MapToString(tmp)}) - if err != nil { - logger.Error(err.Error()) - return true - } - if m.GetConn() != nil { - m.GetConn().Send(0, uint32(pb.ProtoCode_RoleUpdateItemsNty), rsp, 0) + m.GetConn().SendPB(0, uint32(pb.ProtoCode_RoleUpdateItemsNty), &pb.RoleUpdateItemsNty{Items: common.MapToString(tmp)}, 0) } return true @@ -375,3 +364,29 @@ func (m *RoleModel) LoadEmails() []*pb.Email { } return emails } + +func (m *RoleModel) InsertEmail(email *pb.Email) error { + id, err := common.GetNextEmail() + if err != nil { + return err + } + + email.Id = id + email.CreateTime = common.Timex() + email.Status = 0 + + data := &EmailModel{ + ISchema: NewSchema(email.Id, email), + Data: email, + } + err = data.Create() + if err != nil { + return err + } + + nty := &pb.EmailNewNty{Emails: email} + if m.GetConn() != nil { + m.GetConn().SendPB(0, uint32(pb.ProtoCode_EmailNewNty), nty, 0) + } + return nil +} diff --git a/pb/game.pb.go b/pb/game.pb.go index 2759161..9855082 100644 --- a/pb/game.pb.go +++ b/pb/game.pb.go @@ -1413,6 +1413,53 @@ func (x *EmailDelRsp) GetIds() []string { return nil } +type EmailNewNty struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Emails *Email `protobuf:"bytes,1,opt,name=emails,proto3" json:"emails,omitempty"` +} + +func (x *EmailNewNty) Reset() { + *x = EmailNewNty{} + if protoimpl.UnsafeEnabled { + mi := &file_game_proto_msgTypes[27] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *EmailNewNty) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*EmailNewNty) ProtoMessage() {} + +func (x *EmailNewNty) ProtoReflect() protoreflect.Message { + mi := &file_game_proto_msgTypes[27] + 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 EmailNewNty.ProtoReflect.Descriptor instead. +func (*EmailNewNty) Descriptor() ([]byte, []int) { + return file_game_proto_rawDescGZIP(), []int{27} +} + +func (x *EmailNewNty) GetEmails() *Email { + if x != nil { + return x.Emails + } + return nil +} + var File_game_proto protoreflect.FileDescriptor var file_game_proto_rawDesc = []byte{ @@ -1519,8 +1566,12 @@ var file_game_proto_rawDesc = []byte{ 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0x0d, 0x0a, 0x0b, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x71, 0x22, 0x1f, 0x0a, 0x0b, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x44, 0x65, 0x6c, 0x52, 0x73, 0x70, 0x12, 0x10, 0x0a, 0x03, 0x69, 0x64, 0x73, 0x18, 0x01, - 0x20, 0x03, 0x28, 0x09, 0x52, 0x03, 0x69, 0x64, 0x73, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, - 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x20, 0x03, 0x28, 0x09, 0x52, 0x03, 0x69, 0x64, 0x73, 0x22, 0x34, 0x0a, 0x0b, 0x45, 0x6d, 0x61, + 0x69, 0x6c, 0x4e, 0x65, 0x77, 0x4e, 0x74, 0x79, 0x12, 0x25, 0x0a, 0x06, 0x65, 0x6d, 0x61, 0x69, + 0x6c, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, + 0x73, 0x2e, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x52, 0x06, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x73, 0x42, + 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, } var ( @@ -1535,7 +1586,7 @@ func file_game_proto_rawDescGZIP() []byte { return file_game_proto_rawDescData } -var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 27) +var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 28) var file_game_proto_goTypes = []interface{}{ (*HeartReq)(nil), // 0: game.HeartReq (*HeartRsp)(nil), // 1: game.HeartRsp @@ -1564,29 +1615,31 @@ var file_game_proto_goTypes = []interface{}{ (*EmailCheckRar)(nil), // 24: game.EmailCheckRar (*EmailDelReq)(nil), // 25: game.EmailDelReq (*EmailDelRsp)(nil), // 26: game.EmailDelRsp - (*Role)(nil), // 27: models.Role - (*Hero)(nil), // 28: models.Hero - (*Team)(nil), // 29: models.Team - (*Equipment)(nil), // 30: models.Equipment - (*Email)(nil), // 31: models.Email + (*EmailNewNty)(nil), // 27: game.EmailNewNty + (*Role)(nil), // 28: models.Role + (*Hero)(nil), // 29: models.Hero + (*Team)(nil), // 30: models.Team + (*Equipment)(nil), // 31: models.Equipment + (*Email)(nil), // 32: models.Email } var file_game_proto_depIdxs = []int32{ - 27, // 0: game.LoginRsp.role:type_name -> models.Role - 28, // 1: game.LoginRsp.hero:type_name -> models.Hero - 29, // 2: game.LoginRsp.team:type_name -> models.Team - 30, // 3: game.LoginRsp.equipments:type_name -> models.Equipment - 29, // 4: game.ChangeTeamRar.team:type_name -> models.Team + 28, // 0: game.LoginRsp.role:type_name -> models.Role + 29, // 1: game.LoginRsp.hero:type_name -> models.Hero + 30, // 2: game.LoginRsp.team:type_name -> models.Team + 31, // 3: game.LoginRsp.equipments:type_name -> models.Equipment + 30, // 4: game.ChangeTeamRar.team:type_name -> models.Team 7, // 5: game.HeroEquipReferRar.equipIds:type_name -> game.EquipInfo - 27, // 6: game.RoleUpdatePropertyNty.role:type_name -> models.Role - 28, // 7: game.RoleEndBattleRsp.hero:type_name -> models.Hero - 30, // 8: game.EquipmentAddNty.equip:type_name -> models.Equipment - 28, // 9: game.HeroUpLevelRsp.hero:type_name -> models.Hero - 31, // 10: game.EmailListRsp.emails:type_name -> models.Email - 11, // [11:11] is the sub-list for method output_type - 11, // [11:11] is the sub-list for method input_type - 11, // [11:11] is the sub-list for extension type_name - 11, // [11:11] is the sub-list for extension extendee - 0, // [0:11] is the sub-list for field type_name + 28, // 6: game.RoleUpdatePropertyNty.role:type_name -> models.Role + 29, // 7: game.RoleEndBattleRsp.hero:type_name -> models.Hero + 31, // 8: game.EquipmentAddNty.equip:type_name -> models.Equipment + 29, // 9: game.HeroUpLevelRsp.hero:type_name -> models.Hero + 32, // 10: game.EmailListRsp.emails:type_name -> models.Email + 32, // 11: game.EmailNewNty.emails:type_name -> models.Email + 12, // [12:12] is the sub-list for method output_type + 12, // [12:12] is the sub-list for method input_type + 12, // [12:12] is the sub-list for extension type_name + 12, // [12:12] is the sub-list for extension extendee + 0, // [0:12] is the sub-list for field type_name } func init() { file_game_proto_init() } @@ -1920,6 +1973,18 @@ func file_game_proto_init() { return nil } } + file_game_proto_msgTypes[27].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*EmailNewNty); 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{ @@ -1927,7 +1992,7 @@ func file_game_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_game_proto_rawDesc, NumEnums: 0, - NumMessages: 27, + NumMessages: 28, NumExtensions: 0, NumServices: 0, }, diff --git a/pb/protocode.pb.go b/pb/protocode.pb.go index 36323d6..c60a6da 100644 --- a/pb/protocode.pb.go +++ b/pb/protocode.pb.go @@ -42,6 +42,7 @@ const ( ProtoCode_RoleUpdatePropertyNty ProtoCode = 1002 ProtoCode_RoleUpdateItemsNty ProtoCode = 1003 ProtoCode_EquipmentAddNty ProtoCode = 1004 + ProtoCode_EmailNewNty ProtoCode = 1005 ) // Enum value maps for ProtoCode. @@ -66,6 +67,7 @@ var ( 1002: "RoleUpdatePropertyNty", 1003: "RoleUpdateItemsNty", 1004: "EquipmentAddNty", + 1005: "EmailNewNty", } ProtoCode_value = map[string]int32{ "UNKNOWN": 0, @@ -87,6 +89,7 @@ var ( "RoleUpdatePropertyNty": 1002, "RoleUpdateItemsNty": 1003, "EquipmentAddNty": 1004, + "EmailNewNty": 1005, } ) @@ -121,7 +124,7 @@ var File_protocode_proto protoreflect.FileDescriptor var file_protocode_proto_rawDesc = []byte{ 0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0x90, 0x03, 0x0a, + 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0xa2, 0x03, 0x0a, 0x09, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x0d, 0x0a, 0x08, 0x48, 0x65, 0x61, 0x72, 0x74, 0x52, 0x70, 0x63, 0x10, 0xf5, 0x03, 0x12, 0x0d, 0x0a, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, @@ -146,9 +149,10 @@ var file_protocode_proto_rawDesc = []byte{ 0x74, 0x65, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x4e, 0x74, 0x79, 0x10, 0xea, 0x07, 0x12, 0x17, 0x0a, 0x12, 0x52, 0x6f, 0x6c, 0x65, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x4e, 0x74, 0x79, 0x10, 0xeb, 0x07, 0x12, 0x14, 0x0a, 0x0f, 0x45, 0x71, 0x75, - 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x41, 0x64, 0x64, 0x4e, 0x74, 0x79, 0x10, 0xec, 0x07, 0x42, - 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x33, + 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x41, 0x64, 0x64, 0x4e, 0x74, 0x79, 0x10, 0xec, 0x07, 0x12, + 0x10, 0x0a, 0x0b, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x4e, 0x65, 0x77, 0x4e, 0x74, 0x79, 0x10, 0xed, + 0x07, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( -- libgit2 0.21.2