role_mail.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 = 50
  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. }
  213. func (this *RoleMail) addDelMail(mailId int32) {
  214. this.delMailSet = append(this.delMailSet, mailId)
  215. this.saveMailSet.Remove(mailId)
  216. this.addMailList.Remove(mailId)
  217. delete(this.unreadMailList, mailId)
  218. delete(this.unRewardMailList, mailId)
  219. delete(this.mailMap, mailId)
  220. this.SetDirty(true)
  221. }
  222. func (this *RoleMail) GetMailList() {
  223. ackMsg := &serverproto.SCMailListAck{}
  224. if len(this.mailMap) <= 0 {
  225. this.role.ReplayGate(ackMsg, true)
  226. return
  227. }
  228. ackMsg.TotalMailCount = int32(len(this.mailMap))
  229. for _, data := range this.mailMap {
  230. ackMsg.MailList = append(ackMsg.MailList, data)
  231. }
  232. this.role.ReplayGate(ackMsg, true)
  233. }
  234. func (this *RoleMail) MailRead(idList []int32) serverproto.ErrorCode {
  235. ntfMsg := &serverproto.SCMailChangeNtf{}
  236. ntfMsg.TotalMailCount = int32(len(this.mailMap))
  237. if len(idList) <= 0 {
  238. if len(this.unreadMailList) <= 0 {
  239. return serverproto.ErrorCode_ERROR_MAIL_NO_READ_MAIL
  240. }
  241. //一键读取
  242. for _, mail := range this.unreadMailList {
  243. if !this.isMailCanRead(mail) {
  244. continue
  245. }
  246. this.setMailRead(mail)
  247. ntfMsg.MailStateList = append(ntfMsg.MailStateList, &serverproto.KeyValueType{
  248. Key: mail.Id,
  249. Value: mail.State,
  250. })
  251. }
  252. } else {
  253. for _, id := range idList {
  254. mail, ok := this.mailMap[id]
  255. if !ok {
  256. continue
  257. }
  258. if !this.isMailCanRead(mail) {
  259. delete(this.unreadMailList, id)
  260. continue
  261. }
  262. this.setMailRead(mail)
  263. ntfMsg.MailStateList = append(ntfMsg.MailStateList, &serverproto.KeyValueType{
  264. Key: mail.Id,
  265. Value: mail.State,
  266. })
  267. }
  268. }
  269. //小红点数据
  270. if len(this.unreadMailList) > 0 {
  271. this.role.roleRed.SetUnRedMail(true, true)
  272. } else {
  273. this.role.roleRed.SetUnRedMail(false, true)
  274. }
  275. if len(ntfMsg.MailStateList) <= 0 {
  276. return serverproto.ErrorCode_ERROR_MAIL_NO_READ_MAIL
  277. }
  278. this.role.ReplayGate(ntfMsg, true)
  279. util.DebugF("uid=%v MailRead msg=%v", this.role.GetUUid(), ntfMsg)
  280. return serverproto.ErrorCode_ERROR_OK
  281. }
  282. func (this *RoleMail) MailDelRead() serverproto.ErrorCode {
  283. if len(this.mailMap) <= 0 {
  284. return serverproto.ErrorCode_ERROR_MAIL_NO_MAIL
  285. }
  286. ntfMsg := &serverproto.SCMailChangeNtf{}
  287. //删除已读文件,一键删除
  288. for _, data := range this.mailMap {
  289. //当前邮件未读取状态
  290. if this.isMailCanRead(data) {
  291. continue
  292. }
  293. //当前邮件附件未领取
  294. if this.isMailCanReward(data) {
  295. continue
  296. }
  297. ntfMsg.DelMailList = append(ntfMsg.DelMailList, data.Id)
  298. //添加到邮件删除列表
  299. this.addDelMail(data.Id)
  300. }
  301. if len(ntfMsg.DelMailList) <= 0 {
  302. return serverproto.ErrorCode_ERROR_MAIL_NO_DEL_MAIL
  303. }
  304. ntfMsg.TotalMailCount = int32(len(this.mailMap))
  305. this.role.ReplayGate(ntfMsg, true)
  306. util.DebugF("uid=%v MailDelRead msg=%v", this.role.GetUUid(), ntfMsg)
  307. return serverproto.ErrorCode_ERROR_OK
  308. }
  309. func (this *RoleMail) doMailReward(mail *serverproto.MailContent, addItemList map[int32]int32) serverproto.ErrorCode {
  310. //判断是否能整体添加邮件附件
  311. for _, data := range mail.RewardList {
  312. ret := this.role.GetRoleBag().CanAddItem(data.Key, data.Value)
  313. if ret != serverproto.ErrorCode_ERROR_OK {
  314. return ret
  315. }
  316. }
  317. for _, data := range mail.RewardList {
  318. addItemList[data.Key] += data.Value
  319. }
  320. this.setMailRead(mail)
  321. this.setMailReward(mail)
  322. return serverproto.ErrorCode_ERROR_OK
  323. }
  324. func (this *RoleMail) MailReward(idList []int32) serverproto.ErrorCode {
  325. if len(this.mailMap) <= 0 {
  326. return serverproto.ErrorCode_ERROR_MAIL_NO_MAIL
  327. }
  328. ntfMsg := &serverproto.SCMailChangeNtf{}
  329. ntfMsg.TotalMailCount = int32(len(this.mailMap))
  330. //一键领取奖励
  331. bFull := serverproto.ErrorCode_ERROR_OK
  332. var addItemList = map[int32]int32{}
  333. if len(idList) <= 0 {
  334. if len(this.unRewardMailList) <= 0 {
  335. return serverproto.ErrorCode_ERROR_MAIL_NO_REWARD_MAIL
  336. }
  337. for _, mail := range this.unRewardMailList {
  338. if !this.isMailCanReward(mail) {
  339. delete(this.unRewardMailList, mail.Id)
  340. continue
  341. }
  342. if ret := this.doMailReward(mail, addItemList); ret != serverproto.ErrorCode_ERROR_OK {
  343. bFull = ret
  344. continue
  345. }
  346. ntfMsg.MailStateList = append(ntfMsg.MailStateList, &serverproto.KeyValueType{
  347. Key: mail.Id,
  348. Value: mail.State,
  349. })
  350. }
  351. } else {
  352. for _, id := range idList {
  353. mail, ok := this.mailMap[id]
  354. if !ok {
  355. continue
  356. }
  357. if !this.isMailCanReward(mail) {
  358. delete(this.unRewardMailList, id)
  359. continue
  360. }
  361. if ret := this.doMailReward(mail, addItemList); ret != serverproto.ErrorCode_ERROR_OK {
  362. bFull = ret
  363. continue
  364. }
  365. ntfMsg.MailStateList = append(ntfMsg.MailStateList, &serverproto.KeyValueType{
  366. Key: mail.Id,
  367. Value: mail.State,
  368. })
  369. }
  370. }
  371. //小红点数据
  372. if len(this.unreadMailList) > 0 {
  373. this.role.roleRed.SetUnRedMail(true, true)
  374. } else {
  375. this.role.roleRed.SetUnRedMail(false, true)
  376. }
  377. if len(ntfMsg.MailStateList) > 0 {
  378. this.role.ReplayGate(ntfMsg, true)
  379. util.DebugF("uid=%v MailReward msg=%v", this.role.GetUUid(), ntfMsg)
  380. }
  381. //获取附件资源
  382. if len(addItemList) > 0 {
  383. ackMsg := &serverproto.SCMailRewardAck{
  384. Error: int32(serverproto.ErrorCode_ERROR_OK),
  385. }
  386. //背包已满
  387. if bFull != serverproto.ErrorCode_ERROR_OK {
  388. ackMsg.Error = int32(bFull)
  389. }
  390. this.role.AddItemList(addItemList, AddFrom_Mail, true)
  391. for key, value := range addItemList {
  392. ackMsg.RewardList = append(ackMsg.RewardList, &serverproto.KeyValueType{
  393. Key: key,
  394. Value: value,
  395. })
  396. }
  397. this.role.ReplayGate(ackMsg, true)
  398. } else if bFull != serverproto.ErrorCode_ERROR_OK {
  399. ackMsg := &serverproto.SCMailRewardAck{
  400. Error: int32(bFull),
  401. }
  402. this.role.ReplayGate(ackMsg, true)
  403. }
  404. return serverproto.ErrorCode_ERROR_OK
  405. }
  406. func (this *RoleMail) addMail(mail *serverproto.MailContent, notify bool) {
  407. ntfMsg := &serverproto.SCMailChangeNtf{}
  408. //判断当前邮件是否已满
  409. if len(this.mailMap) >= MaxMailNum {
  410. var delMailId int32 = math.MaxInt32
  411. var minMailId int32 = math.MaxInt32
  412. for _, data := range this.mailMap {
  413. if !this.isMailCanRead(data) && !this.isMailCanReward(data) && data.Id < delMailId {
  414. delMailId = data.Id
  415. }
  416. if data.Id < minMailId {
  417. minMailId = data.Id
  418. }
  419. }
  420. if delMailId == 0 || delMailId == math.MaxInt32 {
  421. delMailId = minMailId
  422. }
  423. this.addDelMail(delMailId)
  424. ntfMsg.DelMailList = append(ntfMsg.DelMailList, delMailId)
  425. }
  426. this.mailMap[mail.Id] = mail
  427. this.addMailList.Add(mail.Id)
  428. this.unreadMailList[mail.Id] = mail
  429. if len(mail.RewardList) > 0 {
  430. this.unRewardMailList[mail.Id] = mail
  431. }
  432. ntfMsg.AddMailList = append(ntfMsg.AddMailList, mail)
  433. ntfMsg.TotalMailCount = int32(len(this.mailMap))
  434. //有可能是离线玩家
  435. if this.role.GetState() != ROLE_STATE_OFFLINE {
  436. //小红点数据
  437. this.role.roleRed.SetUnRedMail(true, true)
  438. if notify {
  439. this.role.ReplayGate(ntfMsg, true)
  440. }
  441. }
  442. util.DebugF("uid=%v AddMail maxMailId=%v msg=%v ", this.role.GetUUid(), this.maxMailId, ntfMsg)
  443. this.SetDirty(true)
  444. }
  445. func (this *RoleMail) refreshMailNum() []int32 {
  446. delNum := len(this.mailMap) - MaxMailNum
  447. if delNum <= 0 {
  448. return nil
  449. }
  450. var delMailIdList []int32
  451. var tmpMailList []*serverproto.MailContent
  452. for _, data := range this.mailMap {
  453. tmpMailList = append(tmpMailList, data)
  454. if !this.isMailCanRead(data) && !this.isMailCanReward(data) {
  455. delMailIdList = append(delMailIdList, data.Id)
  456. }
  457. }
  458. sort.Slice(tmpMailList, func(i, j int) bool {
  459. return tmpMailList[i].Id < tmpMailList[j].Id
  460. })
  461. if delNum <= len(delMailIdList) {
  462. sort.Slice(delMailIdList, func(i, j int) bool {
  463. return delMailIdList[i] < delMailIdList[j]
  464. })
  465. return delMailIdList[0:delNum]
  466. } else {
  467. for idx := 0; idx < len(tmpMailList); idx++ {
  468. bFind := false
  469. for k := 0; k < len(delMailIdList); k++ {
  470. if delMailIdList[k] == tmpMailList[idx].Id {
  471. bFind = true
  472. break
  473. }
  474. }
  475. if bFind {
  476. continue
  477. }
  478. delMailIdList = append(delMailIdList, tmpMailList[idx].Id)
  479. delNum--
  480. if delNum <= 0 {
  481. break
  482. }
  483. }
  484. }
  485. return delMailIdList
  486. }
  487. func (this *RoleMail) AddMail(configId int32, mailType serverproto.MailType,
  488. itemList map[int32]int32, paramList []int32, title, content string) bool {
  489. //后台发送的邮件
  490. if mailType == serverproto.MailType_MailType_GM || mailType == serverproto.MailType_MailType_GM_Self {
  491. if this.curGlobalMailId < configId && mailType == serverproto.MailType_MailType_GM {
  492. this.curGlobalMailId = configId
  493. }
  494. //获取配置文件
  495. this.maxMailId++
  496. nowTime := util.GetTimeMilliseconds()
  497. mail := &serverproto.MailContent{
  498. ConfigId: 0,
  499. Id: this.maxMailId,
  500. Type: int32(mailType),
  501. BeginTime: nowTime,
  502. ExpireTime: nowTime + 30*DayMS,
  503. Title: title,
  504. Content: content,
  505. }
  506. if len(paramList) > 0 {
  507. mail.ParamList = append(paramList)
  508. }
  509. if len(itemList) > 0 {
  510. for key, value := range itemList {
  511. mail.RewardList = append(mail.RewardList, &serverproto.KeyValueType{
  512. Key: key,
  513. Value: value,
  514. })
  515. }
  516. }
  517. this.addMail(mail, true)
  518. return true
  519. } else {
  520. var expireTime uint64 = 30 * DayMS
  521. if configId > 0 {
  522. cfgData, ok := serverproto.MailCfgLoader[configId]
  523. if !ok {
  524. util.DebugF("uid=%v AddMail mailConfigId=%v not find!!!", this.role.GetUUid(), configId)
  525. return false
  526. }
  527. expireTime = uint64(cfgData.Time) * DayMS
  528. }
  529. //获取配置文件
  530. this.maxMailId++
  531. nowTime := util.GetTimeMilliseconds()
  532. mail := &serverproto.MailContent{
  533. Id: this.maxMailId,
  534. ConfigId: configId,
  535. Type: int32(mailType),
  536. BeginTime: nowTime,
  537. ExpireTime: nowTime + expireTime,
  538. Title: title,
  539. Content: content,
  540. }
  541. if len(paramList) > 0 {
  542. mail.ParamList = append(paramList)
  543. }
  544. if len(itemList) > 0 {
  545. for key, value := range itemList {
  546. mail.RewardList = append(mail.RewardList, &serverproto.KeyValueType{
  547. Key: key,
  548. Value: value,
  549. })
  550. }
  551. }
  552. //if mailType != serverproto.MailType_MailType_Competition {
  553. // this.addMail(mail, true)
  554. //} else {
  555. // this.addMail(mail, false)
  556. //}
  557. this.addMail(mail, true)
  558. return true
  559. }
  560. }
  561. func (this *RoleMail) AddMail1(configId int32, mailType serverproto.MailType,
  562. itemList []*serverproto.KeyValueType, paramList []int32, title, content string) bool {
  563. //后台发送的邮件
  564. if mailType == serverproto.MailType_MailType_GM ||
  565. mailType == serverproto.MailType_MailType_GM_Self {
  566. //全局邮件才处理
  567. if this.curGlobalMailId < configId && mailType == serverproto.MailType_MailType_GM {
  568. this.curGlobalMailId = configId
  569. }
  570. //获取配置文件
  571. this.maxMailId++
  572. nowTime := util.GetTimeMilliseconds()
  573. mail := &serverproto.MailContent{
  574. ConfigId: 0,
  575. Id: this.maxMailId,
  576. Type: int32(mailType),
  577. BeginTime: nowTime,
  578. Title: title,
  579. Content: content,
  580. ExpireTime: nowTime + 30*DayMS,
  581. }
  582. if len(paramList) > 0 {
  583. mail.ParamList = append(paramList)
  584. }
  585. if len(itemList) > 0 {
  586. mail.RewardList = append(itemList)
  587. }
  588. this.addMail(mail, true)
  589. return true
  590. } else {
  591. var expireTime uint64 = 30 * DayMS
  592. if configId > 0 {
  593. cfgData, ok := serverproto.MailCfgLoader[configId]
  594. if !ok {
  595. util.DebugF("uid=%v AddMail mailConfigId=%v", this.role.GetUUid(), configId)
  596. return false
  597. }
  598. expireTime = uint64(cfgData.Time) * DayMS
  599. }
  600. //获取配置文件
  601. this.maxMailId++
  602. nowTime := util.GetTimeMilliseconds()
  603. mail := &serverproto.MailContent{
  604. Id: this.maxMailId,
  605. ConfigId: configId,
  606. Type: int32(mailType),
  607. BeginTime: nowTime,
  608. ExpireTime: nowTime + expireTime,
  609. Title: title,
  610. Content: content,
  611. }
  612. if len(paramList) > 0 {
  613. mail.ParamList = append(paramList)
  614. }
  615. if len(itemList) > 0 {
  616. mail.RewardList = append(itemList)
  617. }
  618. //if mailType != serverproto.MailType_MailType_Competition {
  619. // this.addMail(mail, true)
  620. //} else {
  621. // this.addMail(mail, false)
  622. //}
  623. this.addMail(mail, true)
  624. return true
  625. }
  626. }
  627. func AddMailOnlineAndOffline(uidList []uint64, configId, mailType int32, itemList []*serverproto.KeyValueType,
  628. paramList []int32, title, content string) {
  629. //判断当前玩家是否在线,或者在离线池中
  630. for idx := 0; idx < len(uidList); {
  631. uid := uidList[idx]
  632. if uid <= 0 {
  633. continue
  634. }
  635. role := RoleMag.GetRoleFromUUid(uid)
  636. if role == nil {
  637. role = RoleMag.GetRoleFromOffline(uid)
  638. }
  639. if role != nil {
  640. role.(RoleLogicOuter).AddMail1(configId, mailType, itemList, paramList, title, content)
  641. uidList = append(uidList[:idx], uidList[idx+1:]...)
  642. } else {
  643. idx++
  644. }
  645. }
  646. if len(uidList) > 0 {
  647. msg := &serverproto.SSAddMailNtf{
  648. NotifyList: uidList,
  649. MailConfigId: configId,
  650. MailType: mailType,
  651. RewardList: itemList,
  652. MailParamList: paramList,
  653. Title: title,
  654. Content: content,
  655. }
  656. SendDb(msg)
  657. }
  658. }