role_mail.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. package model
  2. import (
  3. "math"
  4. "rocommon/util"
  5. "roserver/baseserver/set"
  6. "roserver/serverproto"
  7. "sort"
  8. )
  9. const DayMS = DaySec * 1000
  10. const DaySec = 24 * 60 * 60
  11. const MaxMailNum = 150
  12. type RoleMail struct {
  13. SaveObject
  14. updateTimer util.DurationTimer //保存数据定时器
  15. mailMap map[int32]*serverproto.MailContent
  16. //未读取邮件列表
  17. unreadMailList map[int32]*serverproto.MailContent
  18. //拥有附件邮件列表
  19. unRewardMailList map[int32]*serverproto.MailContent
  20. maxMailId int32
  21. curGlobalMailId int32
  22. delMailSet []int32
  23. saveMailSet set.Interface
  24. addMailList set.Interface
  25. }
  26. func newRoleMail(r *Role) *RoleMail {
  27. mail := &RoleMail{
  28. SaveObject: SaveObject{
  29. role: r,
  30. },
  31. mailMap: map[int32]*serverproto.MailContent{},
  32. maxMailId: 0,
  33. saveMailSet: set.New(set.NonThreadSafe),
  34. addMailList: set.New(set.NonThreadSafe),
  35. unreadMailList: map[int32]*serverproto.MailContent{},
  36. unRewardMailList: map[int32]*serverproto.MailContent{},
  37. }
  38. return mail
  39. }
  40. func (this *RoleMail) LoadOther(msg interface{}) bool {
  41. return true
  42. }
  43. func (this *RoleMail) Load(msg interface{}) bool {
  44. proRole := msg.(*serverproto.Role)
  45. mailInfo := proRole.RoleMail
  46. if mailInfo == nil {
  47. return true
  48. }
  49. nowTime := util.GetTimeMilliseconds()
  50. this.maxMailId = mailInfo.MaxMailId
  51. this.curGlobalMailId = mailInfo.CurrGlobalMailId
  52. for _, mail := range mailInfo.MailList {
  53. //记录当前领取到的最大平台发送邮件ID
  54. if mail.Type == int32(serverproto.MailType_MailType_GM) && mail.BeginTime == 0 {
  55. if this.curGlobalMailId < mail.Id {
  56. this.curGlobalMailId = mail.Id
  57. }
  58. this.maxMailId++
  59. mail.Id = this.maxMailId //重新复制邮件ID,之前的ID表示全局邮件ID
  60. this.SetDirty(true)
  61. }
  62. //离线添加的邮件在玩家登陆时进行初始化
  63. if mail.BeginTime <= 0 {
  64. mail.BeginTime = nowTime
  65. //通过gmweb方式添加的邮件
  66. if mail.Type != int32(serverproto.MailType_MailType_GM) {
  67. cfgData, ok := serverproto.MailCfgLoader[mail.ConfigId]
  68. if ok {
  69. mail.ExpireTime = nowTime + uint64(cfgData.Time)*DayMS
  70. }
  71. } else {
  72. mail.ExpireTime = nowTime + 30*DayMS
  73. }
  74. this.SetDirty(true)
  75. this.saveMailSet.Add(mail.Id)
  76. }
  77. //判断过期邮件
  78. if this.isExpired(mail, nowTime) {
  79. this.delMailSet = append(this.delMailSet, mail.Id)
  80. this.SetDirty(true)
  81. continue
  82. }
  83. //邮件内容是否正确,不正确在初始化时删除
  84. bDel := false
  85. for idx := 0; idx < len(mail.RewardList); idx++ {
  86. if GetItemCfg(mail.RewardList[idx].Key) == nil {
  87. bDel = true
  88. break
  89. }
  90. }
  91. if bDel {
  92. this.delMailSet = append(this.delMailSet, mail.Id)
  93. this.SetDirty(true)
  94. continue
  95. }
  96. this.mailMap[mail.Id] = mail
  97. if this.isMailCanRead(mail) {
  98. this.unreadMailList[mail.Id] = mail
  99. }
  100. if this.isMailCanReward(mail) {
  101. this.unRewardMailList[mail.Id] = mail
  102. }
  103. }
  104. util.InfoF("uid=%v maxMailId=%v", this.role.GetUUid(), this.maxMailId)
  105. this.updateTimer.Reset(util.GetTimeMilliseconds(), 60*1000, false)
  106. //邮件数量超过上限处理
  107. delMailIdList := this.refreshMailNum()
  108. for idx := 0; idx < len(delMailIdList); idx++ {
  109. this.addDelMail(delMailIdList[idx])
  110. }
  111. if len(this.unreadMailList) <= 0 {
  112. this.role.GetRoleRed().SetUnRedMail(false, false)
  113. } else {
  114. this.role.GetRoleRed().SetUnRedMail(true, false)
  115. }
  116. return true
  117. }
  118. func (this *RoleMail) Save() {
  119. this.SetDirty(false)
  120. //util.DebugF("uid=%v RoleMail save...", this.role.GetUUid())
  121. saveMsg := &serverproto.SSMailSaveNtf{}
  122. //邮件状态变更
  123. for {
  124. if this.saveMailSet.Size() <= 0 {
  125. break
  126. }
  127. mailId := this.saveMailSet.Pop().(int32)
  128. if this.mailMap[mailId] != nil {
  129. saveMsg.MailStateList = append(saveMsg.MailStateList, this.mailMap[mailId])
  130. }
  131. }
  132. //删除邮件
  133. saveMsg.DelMailList = append(this.delMailSet)
  134. this.delMailSet = this.delMailSet[:0]
  135. //添加新邮件
  136. for {
  137. if this.addMailList.Size() <= 0 {
  138. break
  139. }
  140. mailId := this.addMailList.Pop().(int32)
  141. if this.mailMap[mailId] != nil {
  142. saveMsg.AddMailList = append(saveMsg.AddMailList, this.mailMap[mailId])
  143. }
  144. }
  145. saveMsg.MaxMailId = this.maxMailId
  146. saveMsg.CurGlobalMailId = this.curGlobalMailId
  147. this.role.SendDb(saveMsg)
  148. }
  149. func (this *RoleMail) Update(ms uint64) {
  150. //过期邮件处理
  151. if this.updateTimer.IsStart() && this.updateTimer.IsExpired(ms) {
  152. var ntfMsg *serverproto.SCMailChangeNtf = nil
  153. for _, data := range this.mailMap {
  154. if this.isExpired(data, ms) {
  155. this.addDelMail(data.Id)
  156. if ntfMsg == nil {
  157. ntfMsg = &serverproto.SCMailChangeNtf{}
  158. }
  159. ntfMsg.DelMailList = append(ntfMsg.DelMailList, data.Id)
  160. }
  161. }
  162. if ntfMsg != nil && len(ntfMsg.DelMailList) > 0 {
  163. util.InfoF("uid=%v Update delMailList=%v", this.role.GetUUid(), ntfMsg)
  164. ntfMsg.TotalMailCount = int32(len(this.mailMap))
  165. //小红点数据
  166. if len(this.unreadMailList) > 0 {
  167. this.role.roleRed.SetUnRedMail(true, true)
  168. } else {
  169. this.role.roleRed.SetUnRedMail(false, true)
  170. }
  171. this.role.ReplayGate(ntfMsg, true)
  172. }
  173. }
  174. }
  175. func (this *RoleMail) isExpired(mail *serverproto.MailContent, ms uint64) bool {
  176. if ms >= mail.ExpireTime && mail.ExpireTime != 0 {
  177. return true
  178. }
  179. return false
  180. }
  181. // 0未读取 1已读取 | 00未获取 10已获取
  182. // 邮件状态 第1位标识读取状态,第2位标识附件获取状态
  183. // 邮件是否读取
  184. func (this *RoleMail) isMailCanRead(mail *serverproto.MailContent) bool {
  185. if mail.State&1 > 0 {
  186. return false
  187. }
  188. return true
  189. }
  190. func (this *RoleMail) setMailRead(mail *serverproto.MailContent) {
  191. mail.State |= 1
  192. this.saveMailSet.Add(mail.Id)
  193. delete(this.unreadMailList, mail.Id)
  194. this.SetDirty(true)
  195. }
  196. // 附件是否领取
  197. func (this *RoleMail) isMailCanReward(mail *serverproto.MailContent) bool {
  198. if len(mail.RewardList) <= 0 {
  199. return false
  200. }
  201. if (mail.State>>1)&1 > 0 {
  202. return false
  203. }
  204. this.saveMailSet.Add(mail.Id)
  205. this.SetDirty(true)
  206. return true
  207. }
  208. func (this *RoleMail) setMailReward(mail *serverproto.MailContent) {
  209. mail.State |= 2
  210. delete(this.unRewardMailList, mail.Id)
  211. delete(this.unreadMailList, mail.Id)
  212. //this.saveMailSet.Add(mail.Id)
  213. //this.SetDirty(true)
  214. }
  215. func (this *RoleMail) addDelMail(mailId int32) {
  216. this.delMailSet = append(this.delMailSet, mailId)
  217. this.saveMailSet.Remove(mailId)
  218. this.addMailList.Remove(mailId)
  219. delete(this.unreadMailList, mailId)
  220. delete(this.unRewardMailList, mailId)
  221. delete(this.mailMap, mailId)
  222. this.SetDirty(true)
  223. }
  224. func (this *RoleMail) GetMailList() {
  225. ackMsg := &serverproto.SCMailListAck{}
  226. if len(this.mailMap) <= 0 {
  227. this.role.ReplayGate(ackMsg, true)
  228. return
  229. }
  230. ackMsg.TotalMailCount = int32(len(this.mailMap))
  231. for _, data := range this.mailMap {
  232. ackMsg.MailList = append(ackMsg.MailList, data)
  233. }
  234. this.role.ReplayGate(ackMsg, true)
  235. }
  236. func (this *RoleMail) MailRead(idList []int32) serverproto.ErrorCode {
  237. ntfMsg := &serverproto.SCMailChangeNtf{}
  238. ntfMsg.TotalMailCount = int32(len(this.mailMap))
  239. if len(idList) <= 0 {
  240. if len(this.unreadMailList) <= 0 {
  241. return serverproto.ErrorCode_ERROR_MAIL_NO_READ_MAIL
  242. }
  243. //一键读取
  244. for _, mail := range this.unreadMailList {
  245. if !this.isMailCanRead(mail) {
  246. continue
  247. }
  248. this.setMailRead(mail)
  249. ntfMsg.MailStateList = append(ntfMsg.MailStateList, &serverproto.KeyValueType{
  250. Key: mail.Id,
  251. Value: mail.State,
  252. })
  253. }
  254. } else {
  255. for _, id := range idList {
  256. mail, ok := this.mailMap[id]
  257. if !ok {
  258. continue
  259. }
  260. if !this.isMailCanRead(mail) {
  261. delete(this.unreadMailList, id)
  262. continue
  263. }
  264. this.setMailRead(mail)
  265. ntfMsg.MailStateList = append(ntfMsg.MailStateList, &serverproto.KeyValueType{
  266. Key: mail.Id,
  267. Value: mail.State,
  268. })
  269. }
  270. }
  271. //小红点数据
  272. if len(this.unreadMailList) > 0 {
  273. this.role.roleRed.SetUnRedMail(true, true)
  274. } else {
  275. this.role.roleRed.SetUnRedMail(false, true)
  276. }
  277. if len(ntfMsg.MailStateList) <= 0 {
  278. return serverproto.ErrorCode_ERROR_MAIL_NO_READ_MAIL
  279. }
  280. this.role.ReplayGate(ntfMsg, true)
  281. util.DebugF("uid=%v MailRead msg=%v", this.role.GetUUid(), ntfMsg)
  282. return serverproto.ErrorCode_ERROR_OK
  283. }
  284. func (this *RoleMail) MailDelRead() serverproto.ErrorCode {
  285. if len(this.mailMap) <= 0 {
  286. return serverproto.ErrorCode_ERROR_MAIL_NO_MAIL
  287. }
  288. ntfMsg := &serverproto.SCMailChangeNtf{}
  289. //删除已读文件,一键删除
  290. for _, data := range this.mailMap {
  291. //当前邮件未读取状态
  292. if this.isMailCanRead(data) {
  293. continue
  294. }
  295. //当前邮件附件未领取
  296. if this.isMailCanReward(data) {
  297. continue
  298. }
  299. ntfMsg.DelMailList = append(ntfMsg.DelMailList, data.Id)
  300. //添加到邮件删除列表
  301. this.addDelMail(data.Id)
  302. }
  303. if len(ntfMsg.DelMailList) <= 0 {
  304. return serverproto.ErrorCode_ERROR_MAIL_NO_DEL_MAIL
  305. }
  306. ntfMsg.TotalMailCount = int32(len(this.mailMap))
  307. this.role.ReplayGate(ntfMsg, true)
  308. util.DebugF("uid=%v MailDelRead msg=%v", this.role.GetUUid(), ntfMsg)
  309. return serverproto.ErrorCode_ERROR_OK
  310. }
  311. func (this *RoleMail) doMailReward(mail *serverproto.MailContent, addItemList map[int32]int32) serverproto.ErrorCode {
  312. //判断是否能整体添加邮件附件
  313. for _, data := range mail.RewardList {
  314. ret := this.role.GetRoleBag().CanAddItem(data.Key, data.Value)
  315. if ret != serverproto.ErrorCode_ERROR_OK {
  316. return ret
  317. }
  318. }
  319. for _, data := range mail.RewardList {
  320. addItemList[data.Key] += data.Value
  321. }
  322. this.setMailRead(mail)
  323. this.setMailReward(mail)
  324. return serverproto.ErrorCode_ERROR_OK
  325. }
  326. func (this *RoleMail) MailReward(idList []int32) serverproto.ErrorCode {
  327. if len(this.mailMap) <= 0 {
  328. return serverproto.ErrorCode_ERROR_MAIL_NO_MAIL
  329. }
  330. ntfMsg := &serverproto.SCMailChangeNtf{}
  331. ntfMsg.TotalMailCount = int32(len(this.mailMap))
  332. //一键领取奖励
  333. bFull := serverproto.ErrorCode_ERROR_OK
  334. var addItemList = map[int32]int32{}
  335. if len(idList) <= 0 {
  336. if len(this.unRewardMailList) <= 0 {
  337. return serverproto.ErrorCode_ERROR_MAIL_NO_REWARD_MAIL
  338. }
  339. for _, mail := range this.unRewardMailList {
  340. if !this.isMailCanReward(mail) {
  341. delete(this.unRewardMailList, mail.Id)
  342. continue
  343. }
  344. if ret := this.doMailReward(mail, addItemList); ret != serverproto.ErrorCode_ERROR_OK {
  345. bFull = ret
  346. continue
  347. }
  348. ntfMsg.MailStateList = append(ntfMsg.MailStateList, &serverproto.KeyValueType{
  349. Key: mail.Id,
  350. Value: mail.State,
  351. })
  352. }
  353. } else {
  354. for _, id := range idList {
  355. mail, ok := this.mailMap[id]
  356. if !ok {
  357. continue
  358. }
  359. if !this.isMailCanReward(mail) {
  360. delete(this.unRewardMailList, id)
  361. continue
  362. }
  363. if ret := this.doMailReward(mail, addItemList); ret != serverproto.ErrorCode_ERROR_OK {
  364. bFull = ret
  365. continue
  366. }
  367. ntfMsg.MailStateList = append(ntfMsg.MailStateList, &serverproto.KeyValueType{
  368. Key: mail.Id,
  369. Value: mail.State,
  370. })
  371. }
  372. }
  373. //小红点数据
  374. if len(this.unreadMailList) > 0 {
  375. this.role.roleRed.SetUnRedMail(true, true)
  376. } else {
  377. this.role.roleRed.SetUnRedMail(false, true)
  378. }
  379. if len(ntfMsg.MailStateList) > 0 {
  380. this.role.ReplayGate(ntfMsg, true)
  381. util.DebugF("uid=%v MailReward msg=%v", this.role.GetUUid(), ntfMsg)
  382. }
  383. //获取附件资源
  384. if len(addItemList) > 0 {
  385. ackMsg := &serverproto.SCMailRewardAck{
  386. Error: int32(serverproto.ErrorCode_ERROR_OK),
  387. }
  388. //背包已满
  389. if bFull != serverproto.ErrorCode_ERROR_OK {
  390. ackMsg.Error = int32(bFull)
  391. }
  392. this.role.AddItemList(addItemList, AddFrom_Mail, true)
  393. for key, value := range addItemList {
  394. ackMsg.RewardList = append(ackMsg.RewardList, &serverproto.KeyValueType{
  395. Key: key,
  396. Value: value,
  397. })
  398. }
  399. this.role.ReplayGate(ackMsg, true)
  400. } else if bFull != serverproto.ErrorCode_ERROR_OK {
  401. ackMsg := &serverproto.SCMailRewardAck{
  402. Error: int32(bFull),
  403. }
  404. this.role.ReplayGate(ackMsg, true)
  405. }
  406. return serverproto.ErrorCode_ERROR_OK
  407. }
  408. func (this *RoleMail) addMail(mail *serverproto.MailContent, notify bool) {
  409. ntfMsg := &serverproto.SCMailChangeNtf{}
  410. //判断当前邮件是否已满
  411. if len(this.mailMap) >= MaxMailNum {
  412. var delMailId int32 = math.MaxInt32
  413. var minMailId int32 = math.MaxInt32
  414. for _, data := range this.mailMap {
  415. if !this.isMailCanRead(data) && !this.isMailCanReward(data) && data.Id < delMailId {
  416. delMailId = data.Id
  417. }
  418. if data.Id < minMailId {
  419. minMailId = data.Id
  420. }
  421. }
  422. if delMailId == 0 || delMailId == math.MaxInt32 {
  423. delMailId = minMailId
  424. }
  425. this.addDelMail(delMailId)
  426. ntfMsg.DelMailList = append(ntfMsg.DelMailList, delMailId)
  427. }
  428. this.mailMap[mail.Id] = mail
  429. this.addMailList.Add(mail.Id)
  430. this.unreadMailList[mail.Id] = mail
  431. if len(mail.RewardList) > 0 {
  432. this.unRewardMailList[mail.Id] = mail
  433. }
  434. ntfMsg.AddMailList = append(ntfMsg.AddMailList, mail)
  435. ntfMsg.TotalMailCount = int32(len(this.mailMap))
  436. //有可能是离线玩家
  437. if this.role.GetState() != ROLE_STATE_OFFLINE {
  438. //小红点数据
  439. this.role.roleRed.SetUnRedMail(true, true)
  440. if notify {
  441. this.role.ReplayGate(ntfMsg, true)
  442. }
  443. }
  444. util.DebugF("uid=%v AddMail maxMailId=%v msg=%v ", this.role.GetUUid(), this.maxMailId, ntfMsg)
  445. this.SetDirty(true)
  446. }
  447. func (this *RoleMail) refreshMailNum() []int32 {
  448. delNum := len(this.mailMap) - MaxMailNum
  449. if delNum <= 0 {
  450. return nil
  451. }
  452. var delMailIdList []int32
  453. var tmpMailList []*serverproto.MailContent
  454. for _, data := range this.mailMap {
  455. tmpMailList = append(tmpMailList, data)
  456. if !this.isMailCanRead(data) && !this.isMailCanReward(data) {
  457. delMailIdList = append(delMailIdList, data.Id)
  458. }
  459. }
  460. sort.Slice(tmpMailList, func(i, j int) bool {
  461. return tmpMailList[i].Id < tmpMailList[j].Id
  462. })
  463. if delNum <= len(delMailIdList) {
  464. sort.Slice(delMailIdList, func(i, j int) bool {
  465. return delMailIdList[i] < delMailIdList[j]
  466. })
  467. return delMailIdList[0:delNum]
  468. } else {
  469. for idx := 0; idx < len(tmpMailList); idx++ {
  470. bFind := false
  471. for k := 0; k < len(delMailIdList); k++ {
  472. if delMailIdList[k] == tmpMailList[idx].Id {
  473. bFind = true
  474. break
  475. }
  476. }
  477. if bFind {
  478. continue
  479. }
  480. delMailIdList = append(delMailIdList, tmpMailList[idx].Id)
  481. delNum--
  482. if delNum <= 0 {
  483. break
  484. }
  485. }
  486. }
  487. return delMailIdList
  488. }
  489. func (this *RoleMail) AddMail(configId int32, mailType serverproto.MailType,
  490. itemList map[int32]int32, paramList []int32, title, content string) bool {
  491. //后台发送的邮件
  492. if mailType == serverproto.MailType_MailType_GM || mailType == serverproto.MailType_MailType_GM_Self {
  493. if this.curGlobalMailId < configId && mailType == serverproto.MailType_MailType_GM {
  494. this.curGlobalMailId = configId
  495. }
  496. //获取配置文件
  497. this.maxMailId++
  498. nowTime := util.GetTimeMilliseconds()
  499. mail := &serverproto.MailContent{
  500. ConfigId: 0,
  501. Id: this.maxMailId,
  502. Type: int32(mailType),
  503. BeginTime: nowTime,
  504. ExpireTime: nowTime + 30*DayMS,
  505. Title: title,
  506. Content: content,
  507. }
  508. if len(paramList) > 0 {
  509. mail.ParamList = append(paramList)
  510. }
  511. if len(itemList) > 0 {
  512. for key, value := range itemList {
  513. mail.RewardList = append(mail.RewardList, &serverproto.KeyValueType{
  514. Key: key,
  515. Value: value,
  516. })
  517. }
  518. }
  519. this.addMail(mail, true)
  520. return true
  521. } else {
  522. var expireTime uint64 = 30 * DayMS
  523. if configId > 0 {
  524. cfgData, ok := serverproto.MailCfgLoader[configId]
  525. if !ok {
  526. util.DebugF("uid=%v AddMail mailConfigId=%v not find!!!", this.role.GetUUid(), configId)
  527. return false
  528. }
  529. expireTime = uint64(cfgData.Time) * DayMS
  530. }
  531. //获取配置文件
  532. this.maxMailId++
  533. nowTime := util.GetTimeMilliseconds()
  534. mail := &serverproto.MailContent{
  535. Id: this.maxMailId,
  536. ConfigId: configId,
  537. Type: int32(mailType),
  538. BeginTime: nowTime,
  539. ExpireTime: nowTime + expireTime,
  540. Title: title,
  541. Content: content,
  542. }
  543. if len(paramList) > 0 {
  544. mail.ParamList = append(paramList)
  545. }
  546. if len(itemList) > 0 {
  547. for key, value := range itemList {
  548. mail.RewardList = append(mail.RewardList, &serverproto.KeyValueType{
  549. Key: key,
  550. Value: value,
  551. })
  552. }
  553. }
  554. //if mailType != serverproto.MailType_MailType_Competition {
  555. // this.addMail(mail, true)
  556. //} else {
  557. // this.addMail(mail, false)
  558. //}
  559. this.addMail(mail, true)
  560. return true
  561. }
  562. }
  563. func (this *RoleMail) AddMail1(configId int32, mailType serverproto.MailType,
  564. itemList []*serverproto.KeyValueType, paramList []int32, title, content string) bool {
  565. //后台发送的邮件
  566. if mailType == serverproto.MailType_MailType_GM ||
  567. mailType == serverproto.MailType_MailType_GM_Self {
  568. //全局邮件才处理
  569. if this.curGlobalMailId < configId && mailType == serverproto.MailType_MailType_GM {
  570. this.curGlobalMailId = configId
  571. }
  572. //获取配置文件
  573. this.maxMailId++
  574. nowTime := util.GetTimeMilliseconds()
  575. mail := &serverproto.MailContent{
  576. ConfigId: 0,
  577. Id: this.maxMailId,
  578. Type: int32(mailType),
  579. BeginTime: nowTime,
  580. Title: title,
  581. Content: content,
  582. ExpireTime: nowTime + 30*DayMS,
  583. }
  584. if len(paramList) > 0 {
  585. mail.ParamList = append(paramList)
  586. }
  587. if len(itemList) > 0 {
  588. mail.RewardList = append(itemList)
  589. }
  590. this.addMail(mail, true)
  591. return true
  592. } else {
  593. var expireTime uint64 = 30 * DayMS
  594. if configId > 0 {
  595. cfgData, ok := serverproto.MailCfgLoader[configId]
  596. if !ok {
  597. util.DebugF("uid=%v AddMail mailConfigId=%v", this.role.GetUUid(), configId)
  598. return false
  599. }
  600. expireTime = uint64(cfgData.Time) * DayMS
  601. }
  602. //获取配置文件
  603. this.maxMailId++
  604. nowTime := util.GetTimeMilliseconds()
  605. mail := &serverproto.MailContent{
  606. Id: this.maxMailId,
  607. ConfigId: configId,
  608. Type: int32(mailType),
  609. BeginTime: nowTime,
  610. ExpireTime: nowTime + expireTime,
  611. Title: title,
  612. Content: content,
  613. }
  614. if len(paramList) > 0 {
  615. mail.ParamList = append(paramList)
  616. }
  617. if len(itemList) > 0 {
  618. mail.RewardList = append(itemList)
  619. }
  620. //if mailType != serverproto.MailType_MailType_Competition {
  621. // this.addMail(mail, true)
  622. //} else {
  623. // this.addMail(mail, false)
  624. //}
  625. this.addMail(mail, true)
  626. return true
  627. }
  628. }
  629. func AddMailOnlineAndOffline(uidList []uint64, configId, mailType int32, itemList []*serverproto.KeyValueType,
  630. paramList []int32, title, content string) {
  631. //判断当前玩家是否在线,或者在离线池中
  632. for idx := 0; idx < len(uidList); {
  633. uid := uidList[idx]
  634. if uid <= 0 {
  635. continue
  636. }
  637. role := RoleMag.GetRoleFromUUid(uid)
  638. if role == nil {
  639. role = RoleMag.GetRoleFromOffline(uid)
  640. }
  641. if role != nil {
  642. role.(RoleLogicOuter).AddMail1(configId, mailType, itemList, paramList, title, content)
  643. uidList = append(uidList[:idx], uidList[idx+1:]...)
  644. } else {
  645. idx++
  646. }
  647. }
  648. if len(uidList) > 0 {
  649. msg := &serverproto.SSAddMailNtf{
  650. NotifyList: uidList,
  651. MailConfigId: configId,
  652. MailType: mailType,
  653. RewardList: itemList,
  654. MailParamList: paramList,
  655. Title: title,
  656. Content: content,
  657. }
  658. SendDb(msg)
  659. }
  660. }