forked from xiaonanln/goworld
/
MailService.go
111 lines (92 loc) · 3.23 KB
/
MailService.go
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
package main
import (
"fmt"
"strconv"
"github.com/xiaonanln/goworld/engine/common"
"github.com/xiaonanln/goworld/engine/entity"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/kvdb"
"github.com/xiaonanln/goworld/engine/kvdb/types"
"github.com/xiaonanln/goworld/engine/netutil"
)
const (
// END_MAIL_ID is the max possible Mail ID
END_MAIL_ID = 9999999999
)
// MailService to handle mail sending & receiving
type MailService struct {
entity.Entity // Entity type should always inherit entity.Entity
mailPacker netutil.MsgPacker
}
func (s *MailService) DescribeEntityType(desc *entity.EntityTypeDesc) {
desc.SetPersistent(true)
desc.DefineAttr("lastMailID", "Persistent")
}
// OnInit is called when initializing MailService
func (s *MailService) OnInit() {
s.mailPacker = netutil.MessagePackMsgPacker{}
}
// OnCreated is called when MailService is created
func (s *MailService) OnCreated() {
gwlog.Infof("Registering MailService ...")
s.Attrs.SetDefaultInt("lastMailID", 0)
}
// SendMail handles send mail requests from avatars
func (s *MailService) SendMail(senderID common.EntityID, senderName string, targetID common.EntityID, data MailData) {
gwlog.Debugf("%s.SendMail: sender=%s,%s, target=%s, mail=%v", s, senderID, senderName, targetID, data)
mailID := s.genMailID()
mailKey := s.getMailKey(mailID, targetID)
mail := map[string]interface{}{
"senderID": senderID,
"senderName": senderName,
"targetID": targetID,
"data": data,
}
mailBytes, err := s.mailPacker.PackMsg(mail, nil)
if err != nil {
gwlog.Panicf("Pack mail failed: %s", err)
s.Call(senderID, "OnSendMail", false)
}
kvdb.Put(mailKey, string(mailBytes), func(err error) {
if err != nil {
gwlog.Panicf("Put mail to kvdb failed: %s", err)
s.Call(senderID, "OnSendMail", false)
}
gwlog.Debugf("Put mail %s to KVDB succeed", mailKey)
s.Call(senderID, "OnSendMail", true)
// tell the target that you have got a mail
s.Call(targetID, "NotifyReceiveMail")
})
}
// GetMails handle get mails requests from avatars
func (s *MailService) GetMails(avatarID common.EntityID, lastMailID int) {
beginMailKey := s.getMailKey(lastMailID+1, avatarID)
endMailKey := s.getMailKey(END_MAIL_ID, avatarID)
kvdb.GetRange(beginMailKey, endMailKey, func(items []kvdbtypes.KVItem, err error) {
s.PanicOnError(err)
var mails []interface{}
for _, item := range items { // Parse the mails
_, mailId := s.parseMailKey(item.Key) // eid should always equal to avatarID
mails = append(mails, []interface{}{
mailId, item.Val, // val is the marshalled mail
})
}
s.Call(avatarID, "OnGetMails", lastMailID, mails)
})
}
func (s *MailService) genMailID() int {
lastMailID := int(s.Attrs.GetInt("lastMailID")) + 1
s.Attrs.SetInt("lastMailID", int64(lastMailID))
return lastMailID
}
func (s *MailService) getMailKey(mailID int, targetID common.EntityID) string {
return fmt.Sprintf("mail$%s$%010d", targetID, mailID)
}
func (s *MailService) parseMailKey(mailKey string) (common.EntityID, int) {
// mail$WVKLioYW8i5wAAD9$0000020969
eid := common.EntityID(mailKey[5 : 5+common.ENTITYID_LENGTH])
mailIdStr := mailKey[5+common.ENTITYID_LENGTH+1:]
mailId, err := strconv.Atoi(mailIdStr)
s.PanicOnError(err)
return eid, mailId
}