Commit c2791869a590be1194c76ea3640d5f3dcdaeb815
1 parent
e448498a
fix: gm增加删除装备删除物品接口
Showing
6 changed files
with
150 additions
and
53 deletions
Show diff stats
cmd/gameserver/action/GmAction.go
1 | 1 | package action |
2 | 2 | |
3 | 3 | import ( |
4 | + "github.com/golang/protobuf/proto" | |
5 | + "pro2d/common/db/mongoproxy" | |
4 | 6 | "pro2d/common/logger" |
5 | 7 | "pro2d/models" |
6 | 8 | "pro2d/pb" |
... | ... | @@ -10,28 +12,51 @@ import ( |
10 | 12 | type GmAction struct { |
11 | 13 | } |
12 | 14 | |
13 | -func (gm *GmAction) AddExp(role *models.RoleModel, properties map[string]interface{}) int { | |
14 | - logger.Debug(properties) | |
15 | - exp, _ := strconv.Atoi(properties["exp"].(string)) | |
16 | - role.IncrPropertyChan("exp", int64(exp), true) | |
17 | - return 0 | |
15 | +func (gm *GmAction) AddExp(role *models.RoleModel, params ...interface{}) { | |
16 | + logger.Debug(params) | |
17 | + expIncrease, _ := strconv.Atoi(params[0].(string)) | |
18 | + exp := role.IncrProperty("exp", int64(expIncrease)) | |
19 | + role.UpdateProperty("exp", exp, true) | |
18 | 20 | } |
19 | 21 | |
20 | -func (gm *GmAction) AddEquip(role *models.RoleModel, properties map[string]interface{}) int { | |
21 | - logger.Debug(properties) | |
22 | +func (gm *GmAction) AddEquip(role *models.RoleModel, params ...interface{}) { | |
23 | + logger.Debug(params) | |
24 | + //TODO 验证装备是否存在 | |
25 | + | |
22 | 26 | equip := &pb.Equipment{ |
23 | - Id: role.IncreEquipByKey(1), | |
24 | - RoleId: role.Role.Id, | |
25 | - Type: properties["id"].(string), | |
27 | + Id: role.IncreEquipByKey(1), | |
28 | + RoleId: role.Role.Id, | |
29 | + Type: params[0].(string), | |
30 | + Quality: 1, | |
26 | 31 | } |
27 | - //TODO 验证装备是否存在 | |
32 | + role.AddEquip(equip) | |
33 | +} | |
28 | 34 | |
29 | - if role.GetConn() != nil { | |
30 | - role.GetConn().CustomChan() <- func() { | |
31 | - role.AddEquip(equip) | |
32 | - } | |
35 | +func (gm *GmAction) DelEquip(role *models.RoleModel, params ...interface{}) { | |
36 | + logger.Debug(params) | |
37 | + id := params[0].(string) | |
38 | + if err := mongoproxy.DelOne("equip", "id", id); err != nil { | |
39 | + logger.Error(err.Error()) | |
40 | + return | |
41 | + } | |
42 | + delete(role.Equipments, id) | |
43 | + update := &pb.EquipmentDelRsp{Id: id} | |
44 | + if rsp, err := proto.Marshal(update); err != nil { | |
45 | + logger.Error(" err:", err.Error()) | |
46 | + return | |
33 | 47 | } else { |
34 | - role.AddEquip(equip) | |
48 | + if role.GetConn() != nil { | |
49 | + role.GetConn().SendSuccess(uint32(pb.ProtoCode_EquipmentDelRsp), rsp) | |
50 | + } | |
51 | + } | |
52 | +} | |
53 | + | |
54 | +func (gm *GmAction) DelItem(role *models.RoleModel, params ...interface{}) { | |
55 | + logger.Debug(params) | |
56 | + id := params[0].(string) | |
57 | + count := params[1].(int32) | |
58 | + | |
59 | + if !role.CostItem(id, count) { | |
60 | + logger.Error("item cost error: ", id) | |
35 | 61 | } |
36 | - return 0 | |
37 | 62 | } | ... | ... |
cmd/gameserver/service/gm.go
... | ... | @@ -25,19 +25,19 @@ func (s *GmServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { |
25 | 25 | return func(c *gin.Context) { |
26 | 26 | var roleId string |
27 | 27 | var ok bool |
28 | - properties := make(map[string]interface{}) | |
28 | + properties := make([]interface{}, 2) | |
29 | 29 | //请求类型,以及 format 参数 |
30 | 30 | if c.Request.Method == "POST" { |
31 | 31 | c.Request.ParseForm() |
32 | - for k, v := range c.Request.PostForm { | |
33 | - properties[k] = v[0] | |
32 | + for _, v := range c.Request.PostForm { | |
33 | + properties = append(properties, v[0]) | |
34 | 34 | } |
35 | 35 | roleId, ok = c.GetPostForm("role_id") |
36 | 36 | |
37 | 37 | } else if c.Request.Method == "GET" { |
38 | 38 | roleId, ok = c.GetQuery("role_id") |
39 | - for k, v := range c.Request.URL.Query() { | |
40 | - properties[k] = v[0] | |
39 | + for _, v := range c.Request.URL.Query() { | |
40 | + properties = append(properties, v[0]) | |
41 | 41 | } |
42 | 42 | } else { |
43 | 43 | 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 { |
52 | 52 | //role start |
53 | 53 | conn := s.Server.GetConnManage().GetConnByRID(roleId) |
54 | 54 | var role *models.RoleModel |
55 | + callback := func() { | |
56 | + tvl.Call([]reflect.Value{obj, reflect.ValueOf(role), reflect.ValueOf(properties)}) | |
57 | + role.SaveRoleData(0) | |
58 | + } | |
55 | 59 | if conn != nil { |
56 | 60 | //在线 |
57 | 61 | role = conn.(*Agent).Role |
62 | + conn.CustomChan() <- callback | |
58 | 63 | } else { |
59 | 64 | //离线 |
60 | 65 | role = models.NewRole(roleId) |
... | ... | @@ -63,17 +68,9 @@ func (s *GmServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { |
63 | 68 | return |
64 | 69 | } |
65 | 70 | role.LoadAll() |
71 | + callback() | |
66 | 72 | } |
67 | - | |
68 | - //func start | |
69 | - v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(role), reflect.ValueOf(properties)}) | |
70 | - role.SaveRoleDataChan(0) | |
71 | - | |
72 | - if len(v) != 1 { | |
73 | - c.JSON(http.StatusNotFound, gin.H{"code": -100, "message": "request param len is error"}) | |
74 | - return | |
75 | - } | |
76 | - c.JSON(http.StatusOK, gin.H{"code": v[0].Interface()}) | |
73 | + c.JSON(http.StatusOK, gin.H{"code": 0}) | |
77 | 74 | } |
78 | 75 | } |
79 | 76 | ... | ... |
common/db/mongoproxy/mongoplugin.go
... | ... | @@ -79,6 +79,12 @@ func FindMany(coll string, key string, val interface{}, schema interface{}) erro |
79 | 79 | return r.All(context.TODO(), schema) |
80 | 80 | } |
81 | 81 | |
82 | +func DelOne(coll string, key string, value interface{}) error { | |
83 | + filter := bson.D{{key, value}} | |
84 | + _, err := mongoDatabase.Collection(coll).DeleteOne(context.TODO(), filter, nil) | |
85 | + return err | |
86 | +} | |
87 | + | |
82 | 88 | func GetBsonD(key string, value interface{}) interface{} { |
83 | 89 | return bson.D{{key, value}} |
84 | 90 | } | ... | ... |
models/rolePlugin.go
... | ... | @@ -51,7 +51,10 @@ func (m *RoleModel) AddItem(key string, count int32) bool { |
51 | 51 | logger.Error(err.Error()) |
52 | 52 | return true |
53 | 53 | } |
54 | - m.GetConn().Send(0, uint32(pb.ProtoCode_RoleUpdateItemsRsp), rsp) | |
54 | + | |
55 | + if m.GetConn() != nil { | |
56 | + m.GetConn().Send(0, uint32(pb.ProtoCode_RoleUpdateItemsRsp), rsp) | |
57 | + } | |
55 | 58 | return true |
56 | 59 | } |
57 | 60 | ... | ... |
pb/game.pb.go
... | ... | @@ -562,6 +562,53 @@ func (x *EquipmentAddRsp) GetEquip() *Equipment { |
562 | 562 | return nil |
563 | 563 | } |
564 | 564 | |
565 | +type EquipmentDelRsp struct { | |
566 | + state protoimpl.MessageState | |
567 | + sizeCache protoimpl.SizeCache | |
568 | + unknownFields protoimpl.UnknownFields | |
569 | + | |
570 | + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` | |
571 | +} | |
572 | + | |
573 | +func (x *EquipmentDelRsp) Reset() { | |
574 | + *x = EquipmentDelRsp{} | |
575 | + if protoimpl.UnsafeEnabled { | |
576 | + mi := &file_game_proto_msgTypes[10] | |
577 | + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) | |
578 | + ms.StoreMessageInfo(mi) | |
579 | + } | |
580 | +} | |
581 | + | |
582 | +func (x *EquipmentDelRsp) String() string { | |
583 | + return protoimpl.X.MessageStringOf(x) | |
584 | +} | |
585 | + | |
586 | +func (*EquipmentDelRsp) ProtoMessage() {} | |
587 | + | |
588 | +func (x *EquipmentDelRsp) ProtoReflect() protoreflect.Message { | |
589 | + mi := &file_game_proto_msgTypes[10] | |
590 | + if protoimpl.UnsafeEnabled && x != nil { | |
591 | + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) | |
592 | + if ms.LoadMessageInfo() == nil { | |
593 | + ms.StoreMessageInfo(mi) | |
594 | + } | |
595 | + return ms | |
596 | + } | |
597 | + return mi.MessageOf(x) | |
598 | +} | |
599 | + | |
600 | +// Deprecated: Use EquipmentDelRsp.ProtoReflect.Descriptor instead. | |
601 | +func (*EquipmentDelRsp) Descriptor() ([]byte, []int) { | |
602 | + return file_game_proto_rawDescGZIP(), []int{10} | |
603 | +} | |
604 | + | |
605 | +func (x *EquipmentDelRsp) GetId() string { | |
606 | + if x != nil { | |
607 | + return x.Id | |
608 | + } | |
609 | + return "" | |
610 | +} | |
611 | + | |
565 | 612 | var File_game_proto protoreflect.FileDescriptor |
566 | 613 | |
567 | 614 | var file_game_proto_rawDesc = []byte{ |
... | ... | @@ -608,9 +655,11 @@ var file_game_proto_rawDesc = []byte{ |
608 | 655 | 0x6d, 0x73, 0x22, 0x3a, 0x0a, 0x0f, 0x45, 0x71, 0x75, 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x41, |
609 | 656 | 0x64, 0x64, 0x52, 0x73, 0x70, 0x12, 0x27, 0x0a, 0x05, 0x65, 0x71, 0x75, 0x69, 0x70, 0x18, 0x01, |
610 | 657 | 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x45, 0x71, |
611 | - 0x75, 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x05, 0x65, 0x71, 0x75, 0x69, 0x70, 0x42, 0x0a, | |
612 | - 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, | |
613 | - 0x6f, 0x33, | |
658 | + 0x75, 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x05, 0x65, 0x71, 0x75, 0x69, 0x70, 0x22, 0x21, | |
659 | + 0x0a, 0x0f, 0x45, 0x71, 0x75, 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x44, 0x65, 0x6c, 0x52, 0x73, | |
660 | + 0x70, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, | |
661 | + 0x64, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, | |
662 | + 0x72, 0x6f, 0x74, 0x6f, 0x33, | |
614 | 663 | } |
615 | 664 | |
616 | 665 | var ( |
... | ... | @@ -625,7 +674,7 @@ func file_game_proto_rawDescGZIP() []byte { |
625 | 674 | return file_game_proto_rawDescData |
626 | 675 | } |
627 | 676 | |
628 | -var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 10) | |
677 | +var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 11) | |
629 | 678 | var file_game_proto_goTypes = []interface{}{ |
630 | 679 | (*HeartReq)(nil), // 0: game.HeartReq |
631 | 680 | (*HeartRsp)(nil), // 1: game.HeartRsp |
... | ... | @@ -637,19 +686,20 @@ var file_game_proto_goTypes = []interface{}{ |
637 | 686 | (*RoleUpdatePropertyRsp)(nil), // 7: game.RoleUpdatePropertyRsp |
638 | 687 | (*RoleUpdateItemsRsp)(nil), // 8: game.RoleUpdateItemsRsp |
639 | 688 | (*EquipmentAddRsp)(nil), // 9: game.EquipmentAddRsp |
640 | - (*Team)(nil), // 10: models.Team | |
641 | - (*Role)(nil), // 11: models.Role | |
642 | - (*Hero)(nil), // 12: models.Hero | |
643 | - (*Equipment)(nil), // 13: models.Equipment | |
689 | + (*EquipmentDelRsp)(nil), // 10: game.EquipmentDelRsp | |
690 | + (*Team)(nil), // 11: models.Team | |
691 | + (*Role)(nil), // 12: models.Role | |
692 | + (*Hero)(nil), // 13: models.Hero | |
693 | + (*Equipment)(nil), // 14: models.Equipment | |
644 | 694 | } |
645 | 695 | var file_game_proto_depIdxs = []int32{ |
646 | - 10, // 0: game.ChangeTeamReq.team:type_name -> models.Team | |
647 | - 11, // 1: game.RoleRsp.role:type_name -> models.Role | |
648 | - 12, // 2: game.RoleRsp.hero:type_name -> models.Hero | |
649 | - 10, // 3: game.RoleRsp.team:type_name -> models.Team | |
650 | - 13, // 4: game.RoleRsp.equipments:type_name -> models.Equipment | |
651 | - 11, // 5: game.RoleUpdatePropertyRsp.role:type_name -> models.Role | |
652 | - 13, // 6: game.EquipmentAddRsp.equip:type_name -> models.Equipment | |
696 | + 11, // 0: game.ChangeTeamReq.team:type_name -> models.Team | |
697 | + 12, // 1: game.RoleRsp.role:type_name -> models.Role | |
698 | + 13, // 2: game.RoleRsp.hero:type_name -> models.Hero | |
699 | + 11, // 3: game.RoleRsp.team:type_name -> models.Team | |
700 | + 14, // 4: game.RoleRsp.equipments:type_name -> models.Equipment | |
701 | + 12, // 5: game.RoleUpdatePropertyRsp.role:type_name -> models.Role | |
702 | + 14, // 6: game.EquipmentAddRsp.equip:type_name -> models.Equipment | |
653 | 703 | 7, // [7:7] is the sub-list for method output_type |
654 | 704 | 7, // [7:7] is the sub-list for method input_type |
655 | 705 | 7, // [7:7] is the sub-list for extension type_name |
... | ... | @@ -784,6 +834,18 @@ func file_game_proto_init() { |
784 | 834 | return nil |
785 | 835 | } |
786 | 836 | } |
837 | + file_game_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} { | |
838 | + switch v := v.(*EquipmentDelRsp); i { | |
839 | + case 0: | |
840 | + return &v.state | |
841 | + case 1: | |
842 | + return &v.sizeCache | |
843 | + case 2: | |
844 | + return &v.unknownFields | |
845 | + default: | |
846 | + return nil | |
847 | + } | |
848 | + } | |
787 | 849 | } |
788 | 850 | type x struct{} |
789 | 851 | out := protoimpl.TypeBuilder{ |
... | ... | @@ -791,7 +853,7 @@ func file_game_proto_init() { |
791 | 853 | GoPackagePath: reflect.TypeOf(x{}).PkgPath(), |
792 | 854 | RawDescriptor: file_game_proto_rawDesc, |
793 | 855 | NumEnums: 0, |
794 | - NumMessages: 10, | |
856 | + NumMessages: 11, | |
795 | 857 | NumExtensions: 0, |
796 | 858 | NumServices: 0, |
797 | 859 | }, | ... | ... |
pb/protocode.pb.go
... | ... | @@ -35,6 +35,7 @@ const ( |
35 | 35 | ProtoCode_RoleUpdatePropertyRsp ProtoCode = 9 |
36 | 36 | ProtoCode_RoleUpdateItemsRsp ProtoCode = 10 |
37 | 37 | ProtoCode_EquipmentAddRsp ProtoCode = 11 |
38 | + ProtoCode_EquipmentDelRsp ProtoCode = 12 | |
38 | 39 | ) |
39 | 40 | |
40 | 41 | // Enum value maps for ProtoCode. |
... | ... | @@ -52,6 +53,7 @@ var ( |
52 | 53 | 9: "RoleUpdatePropertyRsp", |
53 | 54 | 10: "RoleUpdateItemsRsp", |
54 | 55 | 11: "EquipmentAddRsp", |
56 | + 12: "EquipmentDelRsp", | |
55 | 57 | } |
56 | 58 | ProtoCode_value = map[string]int32{ |
57 | 59 | "UNKNOWN": 0, |
... | ... | @@ -66,6 +68,7 @@ var ( |
66 | 68 | "RoleUpdatePropertyRsp": 9, |
67 | 69 | "RoleUpdateItemsRsp": 10, |
68 | 70 | "EquipmentAddRsp": 11, |
71 | + "EquipmentDelRsp": 12, | |
69 | 72 | } |
70 | 73 | ) |
71 | 74 | |
... | ... | @@ -100,7 +103,7 @@ var File_protocode_proto protoreflect.FileDescriptor |
100 | 103 | |
101 | 104 | var file_protocode_proto_rawDesc = []byte{ |
102 | 105 | 0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, |
103 | - 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0xde, 0x01, 0x0a, | |
106 | + 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0xf3, 0x01, 0x0a, | |
104 | 107 | 0x09, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, |
105 | 108 | 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e, |
106 | 109 | 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{ |
114 | 117 | 0x61, 0x74, 0x65, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x52, 0x73, 0x70, 0x10, 0x09, |
115 | 118 | 0x12, 0x16, 0x0a, 0x12, 0x52, 0x6f, 0x6c, 0x65, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x49, 0x74, |
116 | 119 | 0x65, 0x6d, 0x73, 0x52, 0x73, 0x70, 0x10, 0x0a, 0x12, 0x13, 0x0a, 0x0f, 0x45, 0x71, 0x75, 0x69, |
117 | - 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x41, 0x64, 0x64, 0x52, 0x73, 0x70, 0x10, 0x0b, 0x42, 0x0a, 0x5a, | |
118 | - 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, | |
119 | - 0x33, | |
120 | + 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x41, 0x64, 0x64, 0x52, 0x73, 0x70, 0x10, 0x0b, 0x12, 0x13, 0x0a, | |
121 | + 0x0f, 0x45, 0x71, 0x75, 0x69, 0x70, 0x6d, 0x65, 0x6e, 0x74, 0x44, 0x65, 0x6c, 0x52, 0x73, 0x70, | |
122 | + 0x10, 0x0c, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, | |
123 | + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, | |
120 | 124 | } |
121 | 125 | |
122 | 126 | var ( | ... | ... |