diff --git a/cmd/gameserver/action/GmAction.go b/cmd/gameserver/action/GmAction.go index 3d681c2..c348f5c 100644 --- a/cmd/gameserver/action/GmAction.go +++ b/cmd/gameserver/action/GmAction.go @@ -1,6 +1,8 @@ package action import ( + "github.com/golang/protobuf/proto" + "pro2d/common/db/mongoproxy" "pro2d/common/logger" "pro2d/models" "pro2d/pb" @@ -10,28 +12,51 @@ import ( type GmAction struct { } -func (gm *GmAction) AddExp(role *models.RoleModel, properties map[string]interface{}) int { - logger.Debug(properties) - exp, _ := strconv.Atoi(properties["exp"].(string)) - role.IncrPropertyChan("exp", int64(exp), true) - return 0 +func (gm *GmAction) AddExp(role *models.RoleModel, params ...interface{}) { + logger.Debug(params) + expIncrease, _ := strconv.Atoi(params[0].(string)) + exp := role.IncrProperty("exp", int64(expIncrease)) + role.UpdateProperty("exp", exp, true) } -func (gm *GmAction) AddEquip(role *models.RoleModel, properties map[string]interface{}) int { - logger.Debug(properties) +func (gm *GmAction) AddEquip(role *models.RoleModel, params ...interface{}) { + logger.Debug(params) + //TODO 验证装备是否存在 + equip := &pb.Equipment{ - Id: role.IncreEquipByKey(1), - RoleId: role.Role.Id, - Type: properties["id"].(string), + Id: role.IncreEquipByKey(1), + RoleId: role.Role.Id, + Type: params[0].(string), + Quality: 1, } - //TODO 验证装备是否存在 + role.AddEquip(equip) +} - if role.GetConn() != nil { - role.GetConn().CustomChan() <- func() { - role.AddEquip(equip) - } +func (gm *GmAction) DelEquip(role *models.RoleModel, params ...interface{}) { + logger.Debug(params) + id := params[0].(string) + if err := mongoproxy.DelOne("equip", "id", id); err != nil { + logger.Error(err.Error()) + return + } + delete(role.Equipments, id) + update := &pb.EquipmentDelRsp{Id: id} + if rsp, err := proto.Marshal(update); err != nil { + logger.Error(" err:", err.Error()) + return } else { - role.AddEquip(equip) + if role.GetConn() != nil { + role.GetConn().SendSuccess(uint32(pb.ProtoCode_EquipmentDelRsp), rsp) + } + } +} + +func (gm *GmAction) DelItem(role *models.RoleModel, params ...interface{}) { + logger.Debug(params) + id := params[0].(string) + count := params[1].(int32) + + if !role.CostItem(id, count) { + logger.Error("item cost error: ", id) } - return 0 } diff --git a/cmd/gameserver/service/gm.go b/cmd/gameserver/service/gm.go index e3519ec..3a92229 100644 --- a/cmd/gameserver/service/gm.go +++ b/cmd/gameserver/service/gm.go @@ -25,19 +25,19 @@ func (s *GmServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { return func(c *gin.Context) { var roleId string var ok bool - properties := make(map[string]interface{}) + properties := make([]interface{}, 2) //请求类型,以及 format 参数 if c.Request.Method == "POST" { c.Request.ParseForm() - for k, v := range c.Request.PostForm { - properties[k] = v[0] + for _, v := range c.Request.PostForm { + properties = append(properties, v[0]) } roleId, ok = c.GetPostForm("role_id") } else if c.Request.Method == "GET" { roleId, ok = c.GetQuery("role_id") - for k, v := range c.Request.URL.Query() { - properties[k] = v[0] + for _, v := range c.Request.URL.Query() { + properties = append(properties, v[0]) } } else { c.JSON(http.StatusOK, gin.H{"code": -101, "message": "not support method"}) @@ -52,9 +52,14 @@ func (s *GmServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { //role start conn := s.Server.GetConnManage().GetConnByRID(roleId) var role *models.RoleModel + callback := func() { + tvl.Call([]reflect.Value{obj, reflect.ValueOf(role), reflect.ValueOf(properties)}) + role.SaveRoleData(0) + } if conn != nil { //在线 role = conn.(*Agent).Role + conn.CustomChan() <- callback } else { //离线 role = models.NewRole(roleId) @@ -63,17 +68,9 @@ func (s *GmServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { return } role.LoadAll() + callback() } - - //func start - v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(role), reflect.ValueOf(properties)}) - role.SaveRoleDataChan(0) - - if len(v) != 1 { - c.JSON(http.StatusNotFound, gin.H{"code": -100, "message": "request param len is error"}) - return - } - c.JSON(http.StatusOK, gin.H{"code": v[0].Interface()}) + c.JSON(http.StatusOK, gin.H{"code": 0}) } } diff --git a/common/db/mongoproxy/mongoplugin.go b/common/db/mongoproxy/mongoplugin.go index 151e574..9b4e639 100644 --- a/common/db/mongoproxy/mongoplugin.go +++ b/common/db/mongoproxy/mongoplugin.go @@ -79,6 +79,12 @@ func FindMany(coll string, key string, val interface{}, schema interface{}) erro return r.All(context.TODO(), schema) } +func DelOne(coll string, key string, value interface{}) error { + filter := bson.D{{key, value}} + _, err := mongoDatabase.Collection(coll).DeleteOne(context.TODO(), filter, nil) + return err +} + func GetBsonD(key string, value interface{}) interface{} { return bson.D{{key, value}} } diff --git a/models/rolePlugin.go b/models/rolePlugin.go index 593ee27..f3aeb64 100644 --- a/models/rolePlugin.go +++ b/models/rolePlugin.go @@ -51,7 +51,10 @@ func (m *RoleModel) AddItem(key string, count int32) bool { logger.Error(err.Error()) return true } - m.GetConn().Send(0, uint32(pb.ProtoCode_RoleUpdateItemsRsp), rsp) + + if m.GetConn() != nil { + m.GetConn().Send(0, uint32(pb.ProtoCode_RoleUpdateItemsRsp), rsp) + } return true } diff --git a/pb/game.pb.go b/pb/game.pb.go index afafe4e..80f5add 100644 --- a/pb/game.pb.go +++ b/pb/game.pb.go @@ -562,6 +562,53 @@ func (x *EquipmentAddRsp) GetEquip() *Equipment { return nil } +type EquipmentDelRsp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` +} + +func (x *EquipmentDelRsp) Reset() { + *x = EquipmentDelRsp{} + if protoimpl.UnsafeEnabled { + mi := &file_game_proto_msgTypes[10] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *EquipmentDelRsp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*EquipmentDelRsp) ProtoMessage() {} + +func (x *EquipmentDelRsp) ProtoReflect() protoreflect.Message { + mi := &file_game_proto_msgTypes[10] + 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 EquipmentDelRsp.ProtoReflect.Descriptor instead. +func (*EquipmentDelRsp) Descriptor() ([]byte, []int) { + return file_game_proto_rawDescGZIP(), []int{10} +} + +func (x *EquipmentDelRsp) GetId() string { + if x != nil { + return x.Id + } + return "" +} + var File_game_proto protoreflect.FileDescriptor var file_game_proto_rawDesc = []byte{ @@ -608,9 +655,11 @@ var file_game_proto_rawDesc = []byte{ 0x6d, 0x73, 0x22, 0x3a, 0x0a, 0x0f, 0x45, 0x71, 0x75, 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x41, 0x64, 0x64, 0x52, 0x73, 0x70, 0x12, 0x27, 0x0a, 0x05, 0x65, 0x71, 0x75, 0x69, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x45, 0x71, - 0x75, 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x05, 0x65, 0x71, 0x75, 0x69, 0x70, 0x42, 0x0a, - 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, + 0x75, 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x05, 0x65, 0x71, 0x75, 0x69, 0x70, 0x22, 0x21, + 0x0a, 0x0f, 0x45, 0x71, 0x75, 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x44, 0x65, 0x6c, 0x52, 0x73, + 0x70, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, + 0x64, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -625,7 +674,7 @@ func file_game_proto_rawDescGZIP() []byte { return file_game_proto_rawDescData } -var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 10) +var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 11) var file_game_proto_goTypes = []interface{}{ (*HeartReq)(nil), // 0: game.HeartReq (*HeartRsp)(nil), // 1: game.HeartRsp @@ -637,19 +686,20 @@ var file_game_proto_goTypes = []interface{}{ (*RoleUpdatePropertyRsp)(nil), // 7: game.RoleUpdatePropertyRsp (*RoleUpdateItemsRsp)(nil), // 8: game.RoleUpdateItemsRsp (*EquipmentAddRsp)(nil), // 9: game.EquipmentAddRsp - (*Team)(nil), // 10: models.Team - (*Role)(nil), // 11: models.Role - (*Hero)(nil), // 12: models.Hero - (*Equipment)(nil), // 13: models.Equipment + (*EquipmentDelRsp)(nil), // 10: game.EquipmentDelRsp + (*Team)(nil), // 11: models.Team + (*Role)(nil), // 12: models.Role + (*Hero)(nil), // 13: models.Hero + (*Equipment)(nil), // 14: models.Equipment } var file_game_proto_depIdxs = []int32{ - 10, // 0: game.ChangeTeamReq.team:type_name -> models.Team - 11, // 1: game.RoleRsp.role:type_name -> models.Role - 12, // 2: game.RoleRsp.hero:type_name -> models.Hero - 10, // 3: game.RoleRsp.team:type_name -> models.Team - 13, // 4: game.RoleRsp.equipments:type_name -> models.Equipment - 11, // 5: game.RoleUpdatePropertyRsp.role:type_name -> models.Role - 13, // 6: game.EquipmentAddRsp.equip:type_name -> models.Equipment + 11, // 0: game.ChangeTeamReq.team:type_name -> models.Team + 12, // 1: game.RoleRsp.role:type_name -> models.Role + 13, // 2: game.RoleRsp.hero:type_name -> models.Hero + 11, // 3: game.RoleRsp.team:type_name -> models.Team + 14, // 4: game.RoleRsp.equipments:type_name -> models.Equipment + 12, // 5: game.RoleUpdatePropertyRsp.role:type_name -> models.Role + 14, // 6: game.EquipmentAddRsp.equip:type_name -> models.Equipment 7, // [7:7] is the sub-list for method output_type 7, // [7:7] is the sub-list for method input_type 7, // [7:7] is the sub-list for extension type_name @@ -784,6 +834,18 @@ func file_game_proto_init() { return nil } } + file_game_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*EquipmentDelRsp); 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{ @@ -791,7 +853,7 @@ func file_game_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_game_proto_rawDesc, NumEnums: 0, - NumMessages: 10, + NumMessages: 11, NumExtensions: 0, NumServices: 0, }, diff --git a/pb/protocode.pb.go b/pb/protocode.pb.go index 64f538e..56c8d30 100644 --- a/pb/protocode.pb.go +++ b/pb/protocode.pb.go @@ -35,6 +35,7 @@ const ( ProtoCode_RoleUpdatePropertyRsp ProtoCode = 9 ProtoCode_RoleUpdateItemsRsp ProtoCode = 10 ProtoCode_EquipmentAddRsp ProtoCode = 11 + ProtoCode_EquipmentDelRsp ProtoCode = 12 ) // Enum value maps for ProtoCode. @@ -52,6 +53,7 @@ var ( 9: "RoleUpdatePropertyRsp", 10: "RoleUpdateItemsRsp", 11: "EquipmentAddRsp", + 12: "EquipmentDelRsp", } ProtoCode_value = map[string]int32{ "UNKNOWN": 0, @@ -66,6 +68,7 @@ var ( "RoleUpdatePropertyRsp": 9, "RoleUpdateItemsRsp": 10, "EquipmentAddRsp": 11, + "EquipmentDelRsp": 12, } ) @@ -100,7 +103,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, 0xde, 0x01, 0x0a, + 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0xf3, 0x01, 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, 0x0c, 0x0a, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x73, 0x70, 0x10, 0x01, 0x12, 0x0c, 0x0a, 0x08, 0x48, 0x65, 0x61, 0x72, 0x74, 0x52, 0x65, @@ -114,9 +117,10 @@ var file_protocode_proto_rawDesc = []byte{ 0x61, 0x74, 0x65, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x52, 0x73, 0x70, 0x10, 0x09, 0x12, 0x16, 0x0a, 0x12, 0x52, 0x6f, 0x6c, 0x65, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x52, 0x73, 0x70, 0x10, 0x0a, 0x12, 0x13, 0x0a, 0x0f, 0x45, 0x71, 0x75, 0x69, - 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x41, 0x64, 0x64, 0x52, 0x73, 0x70, 0x10, 0x0b, 0x42, 0x0a, 0x5a, - 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x33, + 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x41, 0x64, 0x64, 0x52, 0x73, 0x70, 0x10, 0x0b, 0x12, 0x13, 0x0a, + 0x0f, 0x45, 0x71, 0x75, 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x44, 0x65, 0x6c, 0x52, 0x73, 0x70, + 0x10, 0x0c, 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