playerprofile.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package model
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. )
  6. // 所在页面
  7. const (
  8. PlayerStateOnline = 1 // 在线
  9. PlayerStateOffline = 2 // 离线
  10. PlayerStateGameOut = 3 // 游戏外
  11. PlayerStateGameIn = 4 // 游戏内
  12. )
  13. // 头像信息
  14. const (
  15. // 符文: 位置 符文 等级 等阶
  16. // 头像 头像框 昵称 等级 ID, 经验
  17. // 主线 合作模式
  18. // 神话英雄: 英雄 等级
  19. // 宝物: 宝物 等级
  20. PlayerMaskHead = 1 << 0
  21. PlayerMaskHeadFrame = 1 << 1
  22. PlayerMaskNick = 1 << 2
  23. PlayerMaskLevel = 1 << 3
  24. PlayerMaskExp = 1 << 4
  25. PlayerMaskMainChapter = 1 << 5
  26. PlayerMaskCoopChapter = 1 << 6
  27. PlayerMaskRune = 1 << 7
  28. PlayerMaskHeros = 1 << 8
  29. PlayerMaskTreasures = 1 << 9
  30. PlayerMaskStatus = 1 << 10
  31. PlayerMaskCredit = 1 << 11
  32. PlayerMaskRenown = 1 << 12
  33. PlayerMaskFort = 1 << 13
  34. PlayerMaskServer = 1 << 14
  35. // PlayerMaskFortSkin = 1 << 14
  36. // PlayerMask = 0x
  37. PlayerMaskAll = 0x7fff
  38. MaxPlayerMaskNum = 15
  39. )
  40. type RunesType []PartShow
  41. type HerosType []*HeroShow
  42. type TreasuresType []*TreasureShow
  43. type FortType struct {
  44. Level int64 `json:"level"` //等级
  45. WearSkin int64 `json:"wear"`
  46. }
  47. type MapKV map[int64]int64
  48. func (r *RunesType) RedisScan(src interface{}) error {
  49. src2 := src.([]byte)
  50. err := json.Unmarshal(src2, r)
  51. if err != nil {
  52. return err
  53. }
  54. return nil
  55. }
  56. func (r *HerosType) RedisScan(src interface{}) error {
  57. src2 := src.([]byte)
  58. err := json.Unmarshal(src2, r)
  59. if err != nil {
  60. return err
  61. }
  62. return nil
  63. }
  64. func (r *TreasuresType) RedisScan(src interface{}) error {
  65. src2 := src.([]byte)
  66. err := json.Unmarshal(src2, r)
  67. if err != nil {
  68. return err
  69. }
  70. return nil
  71. }
  72. func (r *FortType) RedisScan(src interface{}) error {
  73. src2 := src.([]byte)
  74. err := json.Unmarshal(src2, r)
  75. if err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. func (r *MapKV) RedisScan(src interface{}) error {
  81. src2 := src.([]byte)
  82. err := json.Unmarshal(src2, r)
  83. if err != nil {
  84. return err
  85. }
  86. return nil
  87. }
  88. type PlayerProfile struct {
  89. PlayerId int64 `json:"playerId" redis:"playerId"`
  90. HeadId int64 `json:"headId" redis:"headId"`
  91. FrameId int64 `json:"frameId" redis:"frameId"`
  92. Nick string `json:"nick" redis:"nick"`
  93. Level int64 `json:"level" redis:"level"`
  94. Exp int64 `json:"exp" redis:"exp"`
  95. Runes RunesType `json:"runes" redis:"runes"`
  96. MainChapter int64 `json:"mainChapter" redis:"mainChapter"`
  97. CoopChapter MapKV `json:"coopChapter" redis:"coopChapter"`
  98. Heros HerosType `json:"heros" redis:"heros"`
  99. Treasures TreasuresType `json:"treasures" redis:"treasures"`
  100. Status int64 `json:"status" redis:"status"` //游戏中 游戏外 离线
  101. LastStatusTs int64 `json:"lastStatusTs" redis:"lastStatusTs"`
  102. CreditScore int64 `json:"creditScore" redis:"creditScore"` // 信誉积分
  103. Renown int64 `json:"renown" redis:"renown"` // 军衔
  104. Fort *FortType `json:"fort" redis:"fort"`
  105. ServerId int64 `json:"serverId" redis:"serverId"` //几服
  106. Mask uint32 `json:"-"`
  107. }
  108. func (pp *PlayerProfile) GetKey() int64 {
  109. return pp.PlayerId
  110. }
  111. func (p *PlayerProfile) TableName() string {
  112. return "playerProfile"
  113. }
  114. func (p *PlayerProfile) GetUniqueKey() string {
  115. return strconv.FormatInt(p.PlayerId, 10)
  116. }
  117. func NewPlayerProfile(player *GamePlayer) *PlayerProfile {
  118. return &PlayerProfile{
  119. PlayerId: player.PlayerId,
  120. Nick: player.NickName,
  121. Level: player.Level,
  122. }
  123. }
  124. // 必须是game模块主协程调用,并且生成的PlayerProfile的成员不能是GamePlayer的成员指针
  125. func CreatePlayerProfile(player *GamePlayer) *PlayerProfile {
  126. p := new(PlayerProfile)
  127. p.PlayerId = player.PlayerId
  128. p.HeadId = player.HeadId
  129. p.FrameId = player.HeadFrameId
  130. p.Nick = player.NickName
  131. p.Level = player.Level
  132. p.Exp = player.Exp
  133. p.MainChapter = 0 // 默认值,可以根据实际情况调整
  134. if player.Chapters != nil {
  135. cp := findChapterProcess(player.Chapters.Process, LevelTypeMainSingle)
  136. if cp != nil {
  137. levelId := CombineLevelId(LevelTypeMainSingle, cp.Chapter, cp.Level)
  138. p.MainChapter = levelId
  139. }
  140. p.CoopChapter = make(MapKV) // 默认值,可以根据实际情况调整
  141. cp = findChapterProcess(player.Chapters.Process, LevelTypeCooperation)
  142. if cp != nil {
  143. levelId := CombineLevelId(LevelTypeCooperation, cp.Chapter, cp.Level)
  144. p.CoopChapter[levelId] = cp.Wave
  145. }
  146. }
  147. p.Runes = player.GetRuneShow()
  148. p.Heros = player.GetHeroShow()
  149. p.Treasures = player.GetTreasureShow()
  150. if player.IsOnline() {
  151. p.Status = PlayerStateOnline
  152. p.LastStatusTs = player.LastLoginTick
  153. } else {
  154. p.Status = PlayerStateOffline
  155. p.LastStatusTs = player.LastLogoutTick
  156. }
  157. p.CreditScore = player.MapValues[MapValueCreditScore]
  158. p.Renown = player.MapValues[MapValueRenown]
  159. p.Fort = new(FortType)
  160. if player.Fort != nil {
  161. p.Fort.Level = player.Fort.Level
  162. p.Fort.WearSkin = player.Fort.WearSkin
  163. }
  164. p.ServerId = player.LineId
  165. return p
  166. }
  167. // 获取关卡进度
  168. func findChapterProcess(process []*ChapterProcess, typ int64) *ChapterProcess {
  169. for _, v := range process {
  170. if v.Type == typ {
  171. return v
  172. }
  173. }
  174. return nil
  175. }
  176. type PlayerBrief struct {
  177. PlayerId int64 `json:"playerId"`
  178. HeadId int64 `json:"headId"` //头像
  179. FrameId int64 `json:"frameId"` //头像框
  180. Nick string `json:"nick"` //昵称
  181. Level int64 `json:"level"` //等级
  182. ServerId int64 `json:"serverId"` //服务器id
  183. Online int64 `json:"online"` //在线状态
  184. LastLogInOutTick int64 `json:"lastLogInOutTick"` //上次登录时间
  185. // FriendStatus int64 `json:"friendStatus"` // 0未添加 1已申请未通过 2已是好友
  186. }
  187. func CreatePlayerBrief(player *GamePlayer) *PlayerBrief {
  188. p := new(PlayerBrief)
  189. p.PlayerId = player.PlayerId
  190. p.HeadId = player.HeadId
  191. p.FrameId = player.HeadFrameId
  192. p.Nick = player.NickName
  193. p.Level = player.Level
  194. p.ServerId = player.LineId
  195. if player.IsOnline() {
  196. p.Online = PlayerStateOnline
  197. p.LastLogInOutTick = player.LastLoginTick
  198. } else {
  199. p.Online = PlayerStateOffline
  200. p.LastLogInOutTick = player.LastLogoutTick
  201. }
  202. return p
  203. }