chat.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package model
  2. import (
  3. "fmt"
  4. "leafstalk/otherutils/deepcopy"
  5. "strconv"
  6. "xorm.io/xorm"
  7. )
  8. // 聊天类型
  9. const (
  10. ChatTypeConfig = "config" // 配置
  11. ChatTypeWorld = "world" // 世界频道
  12. ChatTypeRecruit = "recruit" // 招募频道
  13. ChatTypePrivate = "private" // 私聊
  14. )
  15. // 定义三种掩码类型
  16. const (
  17. ChangedMaskConfig uint32 = 1 << iota // 配置变动掩码
  18. ChangedMaskWorld // 世界频道变动掩码
  19. ChangedMaskRecruit // 招募频道变动掩码
  20. )
  21. // 玩家聊天数据
  22. type ChatPlayer struct {
  23. Id int64 // id
  24. PlayerId int64 `xorm:"BIGINT index 'playerid'"` // 玩家ID
  25. WearFrame int64 `xorm:"BIGINT 'wearFrame'"` //当前使用的聊天框
  26. // EmojiIds map[int64]struct{} `xorm:" Text 'emojiIds'"` // 激活的表情Id(花费)
  27. ChatFrames map[int64]struct{} `xorm:"Text chatFrames"` // 激活的框Id(花费)
  28. ChatFriends map[int64]int64 `xorm:"Text privateFriends"` // 私聊好友列表 {playerId:lastMsgTime}
  29. GateId int64 `xorm:"BIGINT gateId"` // 最近所在网关
  30. SendTs int64 `xorm:"-"` // 发言CD时间
  31. }
  32. // TableName 表名
  33. func (m ChatPlayer) TableName() string {
  34. return "chat_player"
  35. }
  36. func (m *ChatPlayer) QueryExist(eng *xorm.Engine) (bool, error) {
  37. player := new(ChatPlayer)
  38. player.PlayerId = m.PlayerId
  39. return eng.Exist(player)
  40. }
  41. func (m *ChatPlayer) UpdateDB(eng *xorm.Engine) (int64, error) {
  42. return eng.Where("playerid=?", m.PlayerId).AllCols().Update(m)
  43. }
  44. func (m *ChatPlayer) GetUniqueKey() string {
  45. return strconv.FormatInt(m.PlayerId, 10)
  46. }
  47. func CopyChat(old *ChatPlayer) *ChatPlayer {
  48. nu := deepcopy.MustCopy(old).(*ChatPlayer)
  49. return nu
  50. }
  51. // 聊天数据
  52. type ChatChannel struct {
  53. Id int64 // id
  54. ChatType string `xorm:"varchar(100) index 'chatType'"` // 类型
  55. Content string `xorm:"text json 'content'"` // 内容
  56. LastChatTs int64 `xorm:"BIGINT 'lastChatTs'"` // 最后一条聊天
  57. }
  58. func (m *ChatChannel) TableName() string {
  59. return "chat_channel"
  60. }
  61. // ChatConfig 配置
  62. // type ChatConfig struct {
  63. // ChatBlockedUsers map[int64]int64 `json:"chatBlockedUsers"` // 禁止玩家 Key: userID, Value: 截止日期
  64. // ChatBlockedChannels map[string]int64 `json:"chatBlockedChannels"` // 禁止频道 Key: channelID, Value: 截止日期
  65. // }
  66. // ChatContent 频道内容
  67. type ChatContent struct {
  68. Id string `json:"id"` // 聊天Id
  69. ChatType string `json:"chatType"` // 聊天类型
  70. SenderId int64 `json:"senderId"`
  71. SenderName string `json:"senderName"`
  72. SenderHeadId int64 `json:"senderHeadId"` // 头像 -1 代表使用 AvatarURL 微信授权头像
  73. SenderFrameId int64 `json:"senderFrameId"` // 头像框
  74. SenderLevel int `json:"senderLevel"`
  75. SenderRenow int `json:"senderRenow"`
  76. ChatFrame int64 `json:"chatFrame"`
  77. ReceiverId int64 `json:"receiverId"` // 接收者Id
  78. MsgType string `json:"msgType"` // text:文本 emoji:表情
  79. Content string `json:"content"` // 聊天内容
  80. StrParam map[string]string `json:"strParam"` // string参数信息
  81. CreateTime int64 `json:"createTime"` // 产生时间(毫秒)
  82. }
  83. // ChannelInfo 频道信息
  84. type ChannelInfo struct {
  85. ChatType string `json:"chatType"` // 频道类型
  86. Content []*ChatContent `json:"content"` // 频道内容
  87. LastChatTs int64 `json:"lastChatTs"` // 最后一条聊天
  88. // Changed bool `json:"-"` // 变动
  89. }
  90. func NewChannelInfo(chatType string) *ChannelInfo {
  91. return &ChannelInfo{
  92. ChatType: chatType,
  93. Content: make([]*ChatContent, 0),
  94. }
  95. }
  96. // ChatSysInfo 聊天系统信息
  97. type ChatSysInfo struct {
  98. BanUsers map[int64]int64 `json:"users"` // 禁止玩家 Key: userID, Value: 截止日期
  99. BanChannels map[string]int64 `json:"channels"` // 禁止频道 Key: channelID, Value: 截止日期
  100. WorldChannel *ChannelInfo `json:"world"` // 世界频道
  101. RecruitChannel *ChannelInfo `json:"recruit"` // 招募频道
  102. ChangedMask uint32 `json:"-"`
  103. }
  104. // SetChangedMask 设置变动掩码
  105. func (it *ChatSysInfo) SetChangedMask(chatType string) {
  106. switch chatType {
  107. case ChatTypeConfig:
  108. it.ChangedMask |= ChangedMaskConfig
  109. case ChatTypeWorld:
  110. it.ChangedMask |= ChangedMaskWorld
  111. case ChatTypeRecruit:
  112. it.ChangedMask |= ChangedMaskRecruit
  113. }
  114. }
  115. // ClearChangedMask 清除变动掩码
  116. func (it *ChatSysInfo) ClearChangedMask() {
  117. it.ChangedMask = 0
  118. }
  119. func NewChatSysInfo() *ChatSysInfo {
  120. it := new(ChatSysInfo)
  121. it.BanChannels = make(map[string]int64)
  122. it.BanUsers = make(map[int64]int64)
  123. it.WorldChannel = NewChannelInfo(ChatTypeWorld)
  124. it.RecruitChannel = NewChannelInfo(ChatTypeRecruit)
  125. return it
  126. }
  127. // Emojis 聊天表情
  128. type Emojis struct {
  129. Uid int64 `json:"Uid"` // 唯一ID
  130. ItemId int64 `json:"ItemId"` // 解锁道具Id 0:免费
  131. }
  132. // 私人聊天记录
  133. type ChatPrivate struct {
  134. Id int64 // id
  135. PlayerId1 int64 `xorm:"BIGINT index 'playerid1'"` // 玩家ID
  136. PlayerId2 int64 `xorm:"BIGINT index 'playerid2'"` // 玩家ID
  137. Content []*ChatContent `xorm:"Text content"` // 频道内容
  138. LastChatTs int64 `xorm:"updated"` // 最后一条聊天"`
  139. Changed bool `xorm:"-"` // 变动
  140. }
  141. func (m *ChatPrivate) TableName() string {
  142. return "chat_private"
  143. }
  144. func (m *ChatPrivate) QueryExist(eng *xorm.Engine) (bool, error) {
  145. player := new(ChatPrivate)
  146. player.PlayerId1 = m.PlayerId1
  147. player.PlayerId2 = m.PlayerId2
  148. return eng.Exist(player)
  149. }
  150. func (m *ChatPrivate) UpdateDB(eng *xorm.Engine) (int64, error) {
  151. return eng.Where("playerid1=? and playerid2=?", m.PlayerId1, m.PlayerId2).Update(m)
  152. }
  153. func (m *ChatPrivate) GetUniqueKey() string {
  154. return fmt.Sprintf("%v:%v", m.PlayerId1, m.PlayerId2)
  155. }
  156. // 实现DeepCopy接口
  157. func CopyChatPrivate(old *ChatPrivate) *ChatPrivate {
  158. new2 := deepcopy.MustCopy(old).(*ChatPrivate) //
  159. return new2
  160. }