gms_chapter.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package gm_services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "gadmin/config"
  7. )
  8. type ChapterInfo struct {
  9. UserId int64 `json:"user_id" gorm:"column:user_id"` //用户id
  10. ChapterId int32 `json:"chapter_id" gorm:"column:chapter_id"`
  11. Difficulty int32 `json:"difficulty" gorm:"column:difficulty"` //困难度
  12. Pass bool `json:"pass"` //是否通关
  13. MaxRoomId int32 `json:"maxRoomId" gorm:"column:room_id"` //最大探索度
  14. EventAt int32 `json:"event_at" gorm:"column:event_at;not null" ` //埋点事件触发的时间
  15. Extra string `json:"-" gorm:"column:extra" ` //相关参数
  16. PassCount int32 `json:"pass_count"` //通关次数
  17. FailCount int32 `json:"fail_count"` //失败次数
  18. }
  19. //
  20. // QueryPlayerChapter
  21. // @Description: 获取普通关卡进度信息
  22. // @param playerId 用户Id
  23. // @return []ChapterInfo 进度信息
  24. // @return int
  25. //
  26. func QueryPlayerChapter(playerId int64) ([]*ChapterInfo, int) {
  27. var playerRoomInfos []*ChapterInfo
  28. var sqlInfo bytes.Buffer
  29. sqlTemp := `(SELECT
  30. t.user_id,
  31. t.chapter_id,
  32. max( t.room_id ) AS 'room_id',
  33. ANY_VALUE ( t.difficulty ) AS difficulty,
  34. ANY_VALUE ( event_at ) AS event_at,
  35. ANY_VALUE ( extra ) AS extra
  36. FROM
  37. chapter_logs_%d t
  38. WHERE
  39. t.event_id = 11
  40. AND t.extra LIKE '%%\"type\":%%'
  41. AND t.user_id = %d)`
  42. for i := 0; i < 18; i++ {
  43. info := fmt.Sprintf(sqlTemp, i, playerId)
  44. sqlInfo.WriteString(info)
  45. if i != 17 {
  46. sqlInfo.WriteString(" UNION ")
  47. }
  48. }
  49. sqlInfo.WriteString(" ORDER BY user_id")
  50. sqlTemp = sqlInfo.String()
  51. config.DB.Raw(sqlTemp).Scan(&playerRoomInfos)
  52. if len(playerRoomInfos) > 0 && playerRoomInfos[0].UserId != playerId {
  53. playerRoomInfos = append(playerRoomInfos[:0], playerRoomInfos[1:]...)
  54. }
  55. for i := range playerRoomInfos {
  56. if playerRoomInfos[i].UserId != playerId {
  57. continue
  58. }
  59. dataTemp := playerRoomInfos[i]
  60. isPass := playerPass(dataTemp.Extra)
  61. playerRoomInfos[i].Pass = isPass
  62. }
  63. activeInfo := queryActiveChapterInfo(playerId)
  64. playerRoomInfos = append(playerRoomInfos, activeInfo...)
  65. return playerRoomInfos, len(playerRoomInfos)
  66. }
  67. //
  68. // QueryActiveChapterInfo
  69. // @Description: 获取活动关卡进度信息(101,102,103)
  70. // @param chapterId 关卡Id
  71. // @param startDate 开始时间(时间戳)
  72. // @param endDate 结束时间(时间戳)
  73. // @return []*ChapterInfo 进度信息
  74. // @return int
  75. //
  76. func queryActiveChapterInfo(playerId int64) []*ChapterInfo {
  77. var playerRoomInfos []ChapterInfo
  78. sqlTemp := `(SELECT
  79. t.chapter_id,
  80. ANY_VALUE ( t.difficulty ) AS difficulty,
  81. ANY_VALUE ( event_at ) AS event_at,
  82. ANY_VALUE ( extra ) AS extra,
  83. ANY_VALUE ( user_id ) AS user_id
  84. FROM
  85. chapter_logs_%d t
  86. WHERE
  87. t.event_id = 11
  88. AND t.extra LIKE '%%"type":%%'
  89. AND t.user_id = %d
  90. AND LENGTH( t.user_id ) >= 1
  91. )`
  92. var sqlInfo bytes.Buffer
  93. for i := 101; i < 104; i++ {
  94. infoTemp := fmt.Sprintf(sqlTemp, i, playerId)
  95. sqlInfo.WriteString(infoTemp)
  96. if i != 103 {
  97. sqlInfo.WriteString(" UNION ALL ")
  98. }
  99. }
  100. sqlInfo.WriteString("ORDER BY chapter_id")
  101. sqlTemp = sqlInfo.String()
  102. config.DB.Raw(sqlTemp).Scan(&playerRoomInfos)
  103. var result []*ChapterInfo
  104. lastChapterId := int32(-1)
  105. for _, info := range playerRoomInfos {
  106. if info.ChapterId == 0 {
  107. continue
  108. }
  109. if lastChapterId != info.ChapterId {
  110. infoTemp := &ChapterInfo{ChapterId: info.ChapterId,
  111. Difficulty: info.Difficulty}
  112. result = append(result, infoTemp)
  113. }
  114. passChapter := playerPass(info.Extra)
  115. if passChapter {
  116. result[len(result)-1].PassCount++
  117. } else {
  118. result[len(result)-1].FailCount++
  119. }
  120. lastChapterId = info.ChapterId
  121. }
  122. return result
  123. }
  124. //
  125. // playerPass
  126. // @Description: 玩家是否通过关卡
  127. // @param extraInfo 相关参数
  128. // @return bool 是否通关
  129. //
  130. func playerPass(extraInfo string) bool {
  131. type passType struct {
  132. Type int `json:"type"` //-1死亡结算;主动退出结算 1通关结算
  133. }
  134. var passTypeTemp passType
  135. json.Unmarshal([]byte(extraInfo), &passTypeTemp)
  136. return passTypeTemp.Type == 1
  137. }