mail_update.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package model
  2. import (
  3. "rocommon/rpc"
  4. "rocommon/service"
  5. "rocommon/util"
  6. "roserver/baseserver/model"
  7. dbmodel "roserver/db/model"
  8. "roserver/serverproto"
  9. "sort"
  10. "strconv"
  11. "sync"
  12. "time"
  13. )
  14. type WebMailItemInfo struct {
  15. Id int32
  16. Title string
  17. Content string
  18. RewardList []*serverproto.KeyValueType
  19. SendDate time.Time
  20. }
  21. type MailUpdateMag struct {
  22. addMutex sync.RWMutex
  23. sendMailList map[int32]*serverproto.WebUpdateMailItem
  24. bInit bool
  25. mailMaxNum int32
  26. }
  27. func newMailUpdateMag() *MailUpdateMag {
  28. mag := &MailUpdateMag{
  29. sendMailList: map[int32]*serverproto.WebUpdateMailItem{},
  30. }
  31. return mag
  32. }
  33. func (this *MailUpdateMag) AttachMail2Update(id int, uidList []uint64, title, content string, sendTime uint64, rewardList []*serverproto.KeyValueType) {
  34. addMail := &serverproto.WebUpdateMailItem{
  35. Id: int32(id),
  36. UidList: uidList,
  37. Title: title,
  38. Content: content,
  39. SendDate: sendTime,
  40. RewardList: rewardList,
  41. }
  42. if len(addMail.UidList) > 0 {
  43. //gm个人邮件
  44. addMail.MailType = int32(serverproto.MailType_MailType_GM_Self)
  45. } else {
  46. //gm全局邮件
  47. addMail.MailType = int32(serverproto.MailType_MailType_GM)
  48. }
  49. this.addMutex.Lock()
  50. //if id != 0 {
  51. // this.sendMailList[addMail.Id] = addMail
  52. //} else {
  53. // this.mailMaxNum++
  54. // addMail.Id = this.mailMaxNum
  55. // this.sendMailList[addMail.Id] = addMail
  56. //}
  57. this.mailMaxNum++
  58. addMail.Id = this.mailMaxNum
  59. this.sendMailList[addMail.Id] = addMail
  60. this.addMail2Redis(addMail)
  61. this.addMutex.Unlock()
  62. }
  63. func (this *MailUpdateMag) DelMailItem(id int) {
  64. this.addMutex.Lock()
  65. delete(this.sendMailList, int32(id))
  66. this.addMutex.Unlock()
  67. }
  68. func (this *MailUpdateMag) GetMailList() []WebMailItemInfo {
  69. var mailList []WebMailItemInfo
  70. this.addMutex.Lock()
  71. for _, val := range this.sendMailList {
  72. tmpMail := WebMailItemInfo{
  73. Id: val.Id,
  74. Title: val.Title,
  75. Content: val.Content,
  76. RewardList: val.RewardList,
  77. SendDate: util.GetTimeByUint64(val.SendDate),
  78. }
  79. mailList = append(mailList, tmpMail)
  80. }
  81. this.addMutex.Unlock()
  82. sort.Slice(mailList, func(i, j int) bool {
  83. return mailList[i].Id < mailList[j].Id
  84. })
  85. return mailList
  86. }
  87. func (this *MailUpdateMag) Update(ms uint64) {
  88. if !this.bInit {
  89. if !this.loadMailFromRedis() {
  90. return
  91. }
  92. this.bInit = true
  93. }
  94. var delMailList []int32
  95. this.addMutex.RLock()
  96. nowTime := util.GetTimeMilliseconds()
  97. for _, item := range this.sendMailList {
  98. //发送邮件
  99. if item.SendDate == 0 || item.SendDate <= nowTime {
  100. ssMsg := &serverproto.SSWebGMAddMailNtf{
  101. MailInfo: item,
  102. }
  103. //发送给全体玩家
  104. if len(item.UidList) <= 0 {
  105. this.addMail2RedisForUser(item)
  106. }
  107. //发送给social,给在线玩家发送邮件,不在线玩家添加到db
  108. SendSocial(ssMsg)
  109. delete(this.sendMailList, item.Id)
  110. delMailList = append(delMailList, item.Id)
  111. }
  112. }
  113. this.addMutex.RUnlock()
  114. this.delMail2Redis(delMailList)
  115. }
  116. const WEB_MAILP_REFIX = "webmail_"
  117. const WEB_MAIL_MAXID_FIELD_PREFIX = "webmaxid"
  118. func (this *MailUpdateMag) loadMailFromRedis() bool {
  119. retList, err := service.GetRedis().HGetAll(WEB_MAILP_REFIX).Result()
  120. if err != nil && err != service.NIL {
  121. return false
  122. }
  123. for key, val := range retList {
  124. if key == WEB_MAIL_MAXID_FIELD_PREFIX {
  125. tmpId, _ := model.Str2Num(val)
  126. if tmpId > 0 {
  127. this.mailMaxNum = int32(tmpId)
  128. }
  129. } else {
  130. //mailInfo := &serverproto.WebUpdateMailItem{}
  131. //err := model.GetDecodeMessage(mailInfo, val)
  132. //if err == nil {
  133. // this.sendMailList[mailInfo.Id] = mailInfo
  134. //}
  135. }
  136. }
  137. globalMaiList, err := service.GetRedis().LRange(dbmodel.RoleGlobalMailListPrefix, 0, -1).Result()
  138. if err != nil {
  139. return true
  140. }
  141. for idx := 0; idx < len(globalMaiList); idx++ {
  142. gmId, _ := model.Str2Num(globalMaiList[idx])
  143. if gmId <= 0 {
  144. continue
  145. }
  146. if gmId > int(this.mailMaxNum) {
  147. this.mailMaxNum = int32(gmId)
  148. }
  149. }
  150. return true
  151. }
  152. func (this *MailUpdateMag) addMail2Redis(mailInfo *serverproto.WebUpdateMailItem) {
  153. if mailInfo.Id <= 0 {
  154. return
  155. }
  156. value := strconv.Itoa(int(mailInfo.Id))
  157. //strconv.Atoi()
  158. err, msgStr := model.GetEncodeMessage(mailInfo)
  159. if err != nil {
  160. return
  161. }
  162. service.GetRedis().HSet(WEB_MAILP_REFIX, WEB_MAIL_MAXID_FIELD_PREFIX, this.mailMaxNum)
  163. _, err = service.GetRedis().HSet(WEB_MAILP_REFIX, value, msgStr).Result()
  164. if err != nil {
  165. return
  166. }
  167. }
  168. func (this *MailUpdateMag) delMail2Redis(mailIdList []int32) {
  169. for idx := 0; idx < len(mailIdList); idx++ {
  170. value := strconv.Itoa(int(mailIdList[idx]))
  171. service.GetRedis().HDel(WEB_MAILP_REFIX, value)
  172. }
  173. }
  174. func (this *MailUpdateMag) addMail2RedisForUser(mailInfo *serverproto.WebUpdateMailItem) {
  175. //放到redis中,玩家上线时获取比玩家记录的id大的所有邮件
  176. value := strconv.Itoa(int(mailInfo.Id))
  177. err, msgStr := model.GetEncodeMessage(mailInfo)
  178. if err != nil {
  179. return
  180. }
  181. _, err1 := service.GetRedis().LPush(dbmodel.RoleGlobalMailListPrefix, value).Result()
  182. if err1 != nil {
  183. util.ErrorF("addMail2RedisForUser err1=%v", err1)
  184. return
  185. }
  186. mailKeyStr := dbmodel.RoleGlobalMailPrefix + value
  187. //添加过期时间
  188. _, err2 := service.GetRedis().Set(mailKeyStr, msgStr, time.Hour*24*30).Result()
  189. //_, err2 := service.GetRedis().Set(mailKeyStr, msgStr, time.Second*10).Result()
  190. if err2 != nil {
  191. util.ErrorF("addMail2RedisForUser err2=%v", err2)
  192. }
  193. }
  194. //////////////////////
  195. func SendSocial(msg interface{}) {
  196. data, meta, err := rpc.EncodeMessage(msg)
  197. if err != nil {
  198. util.InfoF("SendSocial EncodeMessage err=%v,msg=%v", err, msg)
  199. return
  200. }
  201. socialNode := model.SelectServiceNode(model.SERVICE_NODE_TYPE_SOCIAL_STR, 0)
  202. socialSess := model.GetServiceNode(socialNode)
  203. if socialSess == nil {
  204. util.InfoF("SendSocial social session not exist socialnode=%v", socialSess)
  205. } else {
  206. socialSess.Send(&serverproto.ServiceTransmitAck{
  207. MsgId: uint32(meta.ID),
  208. MsgData: data,
  209. })
  210. }
  211. }