Blame view

cmd/gameserver/action/EmailAction.go 2.92 KB
e172952c   zhangqijia   feat: email 系统搭建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  package action
  
  import (
  	"github.com/golang/protobuf/proto"
  	"go.mongodb.org/mongo-driver/bson"
  	"pro2d/common"
  	"pro2d/common/components"
  	"pro2d/common/db/mongoproxy"
  	"pro2d/common/logger"
  	"pro2d/models"
  	"pro2d/pb"
  )
  
  /*
  EmailListRpc 邮件列表
  */
  func EmailListRpc(role *models.RoleModel, msg components.IMessage) (int32, interface{}) {
  	// del
  	err := role.DelExpireEmail()
  	if err != nil {
  		logger.Error(err.Error())
  	}
  
  	// load
  	emails := role.LoadEmails()
  	return 0, emails
  }
  
  func GetEmailPBAttachMents(email *pb.Email) string {
  	if email.Status == 2 {
  		return ""
  	}
  	return email.Attachments
  }
  
  /*
  EmailDrawRpc 打开所有邮件奖励
   2 邮件不存在
  */
  func EmailDrawRpc(role *models.RoleModel, msg components.IMessage) (int32, interface{}) {
  	req := pb.EmailDrawReq{}
  	if err := proto.Unmarshal(msg.GetData(), &req); err != nil {
  		logger.Error("loginRpc err: %v", err)
  		return 1, nil
  	}
  	var emails []*pb.Email
  
  	if req.Id == "" {
  		// 打开所有邮件
  		emails = role.LoadEmails()
  	} else {
  		// 打开一个邮件
  		email := models.NewEmailModel(req.Id)
  		if err := email.Load(); err != nil {
  			return 2, nil
  		}
  		emails = []*pb.Email{email.Data}
  	}
  
  	var ids []string
  	reward := make(common.IMapStringNum)
  	for _, e := range emails {
  		attachments := GetEmailPBAttachMents(e)
  		if attachments == "" {
  			continue
  		}
  		email := models.NewEmailModelPB(e)
  		email.SetProperty("status", 2)
  		email.Log(role, 2)
  		email.Update()
  		ids = append(ids, e.Id)
  		for k, v := range common.StringToMapNum(email.Data.Attachments) {
  			tmp, ok := reward[k]
  			if !ok {
  				reward[k] = v
  			} else {
  				reward[k] = tmp + v
  			}
  		}
  	}
  	role.Award(reward)
  
  	return 0, &pb.EmailDrawRsp{Ids: ids, Reward: common.MapNumToString(reward)}
  }
  
  func EmailCheckRpc(role *models.RoleModel, msg components.IMessage) (int32, interface{}) {
  	req := pb.EmailCheckRar{}
  	if err := proto.Unmarshal(msg.GetData(), &req); err != nil {
  		logger.Error("loginRpc err: %v", err)
  		return 1, nil
  	}
  	email := models.NewEmailModel(req.Id)
  	if err := email.Load(); err != nil {
  		return 2, nil
  	}
  
  	email.SetProperty("status", 1)
  	email.Log(role, 1)
  	email.Update()
  	return 0, nil
  }
  
  func EmailDelRpc(role *models.RoleModel, msg components.IMessage) (int32, interface{}) {
  	req := pb.EmailDelReq{}
  	if err := proto.Unmarshal(msg.GetData(), &req); err != nil {
  		logger.Error("loginRpc err: %v", err)
  		return 1, nil
  	}
  
  	var result []string
  	emails := role.LoadEmails()
  	for _, email := range emails {
  		attachments := GetEmailPBAttachMents(email)
  		if email.Status == 2 || (attachments == "" && email.Status == 1) {
  			result = append(result, email.Id)
  			models.NewEmailModelPB(email).Log(role, 3)
  
  			role.MyLog("mail_action", &pb.LogConf{
  				Desc: "del_mail",
  				Int1: email.Id,
  			})
  		}
  	}
  
  	filter := bson.D{{"roleid", role.Data.Id}, {"id", bson.D{{"$in", result}}}}
  	if err := mongoproxy.DelMany("email", filter); err != nil {
  		return 2, nil
  	}
  
  	return 0, &pb.EmailDelRsp{Ids: result}
  }