mail.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package model
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "github.com/spf13/cast"
  7. "xorm.io/xorm"
  8. )
  9. // MailStatus 表示邮件状态
  10. type MailStatus int
  11. const (
  12. MailStatusUnread MailStatus = iota
  13. MailStatusRead
  14. MailStatusClaimed
  15. )
  16. // 邮件文案类型
  17. const (
  18. MailTypeRank = 2
  19. MailTypeChapterLost = 3
  20. MailTypeEnergy = 4
  21. MailTypeMonthCardAward = 5
  22. MailTypeWarOrderAward = 6
  23. MailTypeShop = 7 //支付购买
  24. MailType7DayChallenge = 8
  25. MailTypeInviteeReward = 9 //被邀请奖励
  26. MailTypeOfflineStamina = 10 //离线体力
  27. MailTypeFirstCharge = 11 //首充奖励
  28. MailTypeDailyDeal = 12 //每日特惠
  29. MailTypeWeekTask = 13 //周任务奖励
  30. )
  31. type MailPersonal struct {
  32. Id int64
  33. PlayerId int64 `xorm:"BIGINT index 'playerId'"`
  34. MailType int64 `xorm:"Int 'type'"` //排行奖励
  35. Title string `xorm:"Varchar(255) 'title'"`
  36. Content string `xorm:"Varchar(1024) 'content'"`
  37. Extra []*DropedItem `xorm:"Text json 'extra'"`
  38. ExtraInfo map[string]string `xorm:"Text 'extraInfo'"`
  39. Sender string `xorm:"Varchar(255) 'sender'"`
  40. CreateTime time.Time `xorm:"created"`
  41. DisplayTime time.Time `xorm:"displayTime"`
  42. ExpireTime time.Time `xorm:"Varchar(255) 'expireTime'"`
  43. Status MailStatus `xorm:"Varchar(255) 'status'"`
  44. // Extra ExtraData `xorm:"Text json 'extra'"`
  45. // ExtraInfo string `xorm:"Text 'extraInfo'"`
  46. // DisplayTime time.Time `xorm:"displayTime"`
  47. // CreateTime time.Time `xorm:"created"`
  48. // ExpireTime time.Time `xorm:"DateTime 'expireTime'"`
  49. // ViewState int `xorm:"Int default(0) 'viewState'"` //阅读状态
  50. // DrawState int `xorm:"Int default(0) 'drawState'"` // 领取状态
  51. }
  52. func (m *MailPersonal) TableName() string {
  53. return "mailpersonal"
  54. }
  55. // val只能是string、int等通过 cast.ToString可以转换成string的类型
  56. func (m *MailPersonal) SetExtraInfo(key string, val any) {
  57. if m.ExtraInfo == nil {
  58. m.ExtraInfo = make(map[string]string)
  59. }
  60. m.ExtraInfo[key] = cast.ToString(val)
  61. }
  62. func (m *MailPersonal) GetExtraInfo(key string) string {
  63. return m.ExtraInfo[key]
  64. }
  65. func NewMailPersonal(playerId int64, mailType int64, lst []*DropedItem) *MailPersonal {
  66. lettle := new(MailPersonal)
  67. lettle.PlayerId = playerId
  68. lettle.MailType = mailType
  69. if len(lst) > 0 {
  70. lettle.Extra = append(lettle.Extra, lst...)
  71. }
  72. var expire time.Duration
  73. lettle.CreateTime = time.Now()
  74. if len(lst) > 0 {
  75. expire = time.Hour * 24 * 30
  76. } else {
  77. expire = time.Hour * 24 * 7
  78. }
  79. lettle.ExpireTime = lettle.CreateTime.Add(expire)
  80. return lettle
  81. }
  82. // type LetterExtraInfo struct {
  83. // Level int `json:"level"`
  84. // Rank int `json:"rank"`
  85. // ActName string `json:"functionName"`
  86. // }
  87. type DropedItem struct {
  88. Id int64 `json:"id"`
  89. Type int64 `json:"type"`
  90. Num int64 `json:"num"`
  91. }
  92. func SumDropedItem2(items map[string]*DropedItem, typ int64, id int64, num int64) {
  93. key := fmt.Sprintf("%v:%v", id, typ)
  94. if it, ok := items[key]; ok {
  95. it.Num += num
  96. } else {
  97. it := new(DropedItem)
  98. it.Id = id
  99. it.Type = typ
  100. it.Num = num
  101. items[key] = it
  102. }
  103. }
  104. func SumDropedMaterial(items map[int64]int64, news map[int64]int64) {
  105. for k, v := range news {
  106. items[k] += v
  107. }
  108. }
  109. func GetMapKeys[K comparable, V any](items map[K]V) []K {
  110. if len(items) == 0 {
  111. return nil
  112. }
  113. lst := make([]K, 0, len(items))
  114. for k := range items {
  115. lst = append(lst, k)
  116. }
  117. return lst
  118. }
  119. func GetMapValues[K comparable, V any](items map[K]V) []V {
  120. if len(items) == 0 {
  121. return nil
  122. }
  123. lst := make([]V, 0, len(items))
  124. for _, v := range items {
  125. lst = append(lst, v)
  126. }
  127. return lst
  128. }
  129. type LevelReward struct {
  130. Level int64 `json:"level"`
  131. Exp int64 `json:"exp"`
  132. Materials map[int64]int64 `json:"materials"`
  133. }
  134. type DropedBundle struct {
  135. Materials map[int64]int64 `json:"materials"`
  136. Heros map[int64]*GrowableHero `json:"heros"`
  137. Treasures map[int64]*GrowableTreasure `json:"treasures"`
  138. Runes map[int64]*BagRune `json:"runes"`
  139. Legends map[int64]int64 `json:"legends"`
  140. EmojisIds []int64 `json:"emojisIds"`
  141. LevelMaterials LevelReward `json:"levelMaterials"`
  142. HeroSkins []int64 `json:"heroSkins"`
  143. Converts map[int64]int64 `json:"-"` // 辅助查看,已统计到Materials
  144. }
  145. func NewDropedBundle() *DropedBundle {
  146. pi := new(DropedBundle)
  147. pi.Materials = make(map[int64]int64)
  148. pi.Heros = make(map[int64]*GrowableHero)
  149. pi.Treasures = make(map[int64]*GrowableTreasure)
  150. pi.Runes = make(map[int64]*BagRune)
  151. pi.Legends = make(map[int64]int64)
  152. pi.LevelMaterials.Materials = make(map[int64]int64)
  153. pi.Converts = make(map[int64]int64)
  154. return pi
  155. }
  156. func (pi *DropedBundle) SumMaterial(news map[int64]int64) {
  157. for k, v := range news {
  158. pi.Materials[k] += v
  159. }
  160. }
  161. func (pi *DropedBundle) SumConvert(news map[int64]int64) {
  162. for k, v := range news {
  163. pi.Converts[k] += v
  164. }
  165. }
  166. func (pi *DropedBundle) SumHero(news map[int64]*GrowableHero) {
  167. for k, v := range news {
  168. pi.Heros[k] = v
  169. }
  170. }
  171. func (pi *DropedBundle) SumTreasure(news map[int64]*GrowableTreasure) {
  172. for k, v := range news {
  173. pi.Treasures[k] = v
  174. }
  175. }
  176. func (pi *DropedBundle) SumRune(news map[int64]*BagRune) {
  177. for k, v := range news {
  178. pi.Runes[k] = v
  179. }
  180. }
  181. func (pi *DropedBundle) SumLegend(news map[int64]int64) {
  182. for k, v := range news {
  183. pi.Legends[k] += v
  184. }
  185. }
  186. func (pi *DropedBundle) SumLevelMaterial(news map[int64]int64) {
  187. for k, v := range news {
  188. pi.LevelMaterials.Materials[k] += v
  189. }
  190. }
  191. func (pi *DropedBundle) SumEmojisId(ids ...int64) {
  192. pi.EmojisIds = append(pi.EmojisIds, ids...)
  193. }
  194. func (pi *DropedBundle) GetMaterialNum(id int64) int64 {
  195. return pi.Materials[id]
  196. }
  197. func (pi *DropedBundle) SumHeroSkin(skinId int64) {
  198. pi.HeroSkins = append(pi.HeroSkins, skinId)
  199. }
  200. // func (pi *DropedBundle) SumHeadId(ids ...int64) {
  201. // pi.HeadIds = append(pi.HeadIds, ids...)
  202. // }
  203. // func (pi *DropedBundle) SumHeadFrameId(ids ...int64) {
  204. // pi.HeadFrameIds = append(pi.HeadFrameIds, ids...)
  205. // }
  206. // func (pi *DropedBundle) SumChatFrameId(ids ...int64) {
  207. // pi.ChatFrameIds = append(pi.ChatFrameIds, ids...)
  208. // }
  209. // func (m *Letter) QueryExist(eng *xorm.Engine) (bool, error) {
  210. // return false, nil
  211. // }
  212. // // 邮件立即插入,不是延迟插入
  213. // func (m *Letter) UpdateDB(eng *xorm.Engine) (int64, error) {
  214. // return eng.Where("playerid=?", m.PlayerId).MustCols("materials", "equipments", "calcItems", "startTick", "timeLen").Update(m)
  215. // }
  216. // func CopyLetter(old *Letter) *Letter {
  217. // new1, err := deepcopy.Copy(old)
  218. // if err != nil {
  219. // return nil
  220. // }
  221. // return new1.(*Letter)
  222. // }
  223. type MailGlobal struct {
  224. Id int64
  225. MailType int64 `xorm:"Int 'type'"`
  226. MaxPlayerId int64 `xorm:"BIGINT 'maxPlayerId'"`
  227. Title string `xorm:"Varchar(255) 'title'"`
  228. Content string `xorm:"Varchar(1024) 'content'"`
  229. Extra []*DropedItem `xorm:"Text json 'extra'"`
  230. ExtraInfo map[string]string `xorm:"Text 'extraInfo'"`
  231. CreateTime time.Time `xorm:"created"`
  232. ExpireTime time.Time `xorm:"DateTime 'expireTime'"`
  233. //
  234. }
  235. func (m *MailGlobal) TableName() string {
  236. return "mailglobal"
  237. }
  238. // 邮箱
  239. type MailPlayer struct {
  240. Id int64
  241. PlayerId int64 `xorm:"BIGINT index 'playerId'"`
  242. LastGlobalId int64 `xorm:"BIGINT 'lastGlobalId'"`
  243. LastLoadTick int64 `xorm:"-"`
  244. Letters []*MailPersonal `xorm:"-"`
  245. }
  246. func (m *MailPlayer) TableName() string {
  247. return "mailplayer"
  248. }
  249. func (m *MailPlayer) QueryExist(eng *xorm.Engine) (bool, error) {
  250. q := new(MailPlayer)
  251. q.PlayerId = m.PlayerId
  252. return eng.Exist(q)
  253. }
  254. func (m *MailPlayer) UpdateDB(eng *xorm.Engine) (int64, error) {
  255. return eng.Where("playerid=?", m.PlayerId).MustCols("lastGlobalId").Update(m)
  256. }
  257. func (m *MailPlayer) GetUniqueKey() string {
  258. return strconv.FormatInt(m.PlayerId, 10)
  259. }
  260. func (m *MailPlayer) IsDirty() bool {
  261. return (m.LastLoadTick == -1)
  262. }
  263. func CopyMailBox(old *MailPlayer) *MailPlayer {
  264. newIt := new(MailPlayer)
  265. *newIt = *old
  266. newIt.Letters = nil
  267. return newIt
  268. }
  269. // 邮件-Mail exported from 邮件.xlsx
  270. type Mail struct {
  271. Type int64 `json:"Type"` // 邮件类型 1-后台邮件
  272. Title string `json:"Title"` // 标题
  273. Content string `json:"Content"` // 内容
  274. }