互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
6.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package client
  2. import (
  3. "hudongzhuanjia/controllers"
  4. "hudongzhuanjia/models"
  5. bahe_service "hudongzhuanjia/services/bahe"
  6. "hudongzhuanjia/utils/code"
  7. "hudongzhuanjia/utils/define"
  8. "time"
  9. "github.com/ouxuanserver/osmanthuswine/src/core"
  10. )
  11. type TugOfWarCtl struct {
  12. controllers.AuthorCtl
  13. }
  14. // 获取当前状态(废弃)
  15. // @Summary client tug war
  16. // @Description get current status
  17. // @Tags client tug war
  18. // @Accept json
  19. // @Produce json
  20. // @Param bahe_activity_id query int true "Bahe Activity ID"
  21. // @Success 0 {object} models.TugOfWar
  22. // @Failure 503 {string} string "参数不存在"
  23. // @Failure 504 {object} string "用户不存在"
  24. // @Router /Client/TugOfWarCtl/status [get]
  25. func (t *TugOfWarCtl) Status() {
  26. // 获取此次活动的状态
  27. baheId := t.MustGetInt64("bahe_activity_id")
  28. uid := t.MustGetUID()
  29. // 找到活动
  30. bahe := new(models.TugOfWar)
  31. exist, err := models.Get(bahe, baheId)
  32. t.CheckErr(err)
  33. t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在")
  34. // 主活动
  35. activity := new(models.Activity)
  36. exist, err = models.Get(activity, bahe.ActivityId)
  37. t.CheckErr(err)
  38. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存")
  39. // 找到用户
  40. member := new(models.BaheTeamMember)
  41. exist, err = member.GetMemberByBaheIdAndUserId(uid, bahe.Id, activity.RehearsalId)
  42. t.CheckErr(err)
  43. t.Assert(exist, code.MSG_TUGWAR_MEMBER_NOT_EXIST, "队员不存在")
  44. score, err := bahe_service.GetScoreByTeamId(member.TeamId, activity.RehearsalId)
  45. t.CheckErr(err)
  46. rank := 1
  47. team := new(models.BaheTeam)
  48. exist, err = models.Get(team, member.TeamId)
  49. t.CheckErr(err)
  50. t.Assert(exist, code.MSG_TUGWAR_TEAM_NOT_EXIST, "队伍不存在")
  51. otherTeam := new(models.BaheTeam)
  52. exist, err = otherTeam.GetOtherTeam(member.TeamId, team.BaheActivityId)
  53. t.CheckErr(err)
  54. t.Assert(exist, code.MSG_TUGWAR_TEAM_NOT_EXIST, "队伍不存在")
  55. otherScore, err := bahe_service.GetScoreByTeamId(otherTeam.Id, activity.RehearsalId)
  56. t.CheckErr(err)
  57. if otherScore > score {
  58. rank = 2
  59. }
  60. t.JSON(map[string]interface{}{
  61. "status": bahe.Status,
  62. "score": score,
  63. "rank": rank,
  64. })
  65. }
  66. // tug_war doc
  67. // @Summary tug war
  68. // @Description shake bahe activity
  69. // @Tags tug war
  70. // @Accept json
  71. // @Produce json
  72. // @Param bahe_activity_id query int true "Bahe Activity ID"
  73. // @Success 0 {object} models.TugOfWar
  74. // @Success 0 {object} models.BaheTeamMember
  75. // @Success 0 {object} models.BaheTeam
  76. // @Failure 404 {string} string "参数不存在"
  77. // @Failure 405 {object} string "用户不存在"
  78. // @Router /Client/TugOfWarCtl/shake [get]
  79. func (t *TugOfWarCtl) Shake() {
  80. uid := t.MustGetUID()
  81. baheId := t.MustGetInt64("bahe_activity_id")
  82. score := t.MustGetInt64("score") // 增加的分数
  83. bahe := new(models.TugOfWar)
  84. exist, err := models.Get(bahe, baheId)
  85. t.CheckErr(err)
  86. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河活动不存在")
  87. t.CheckRunning(bahe.Status)
  88. activity := new(models.Activity)
  89. exist, err = models.Get(activity, bahe.ActivityId)
  90. t.CheckErr(err)
  91. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  92. t.CheckRunning(activity.Status)
  93. member := new(models.BaheTeamMember)
  94. exist, err = member.GetMemberByBaheIdAndUserId(uid, baheId, activity.RehearsalId)
  95. t.CheckErr(err)
  96. t.Assert(exist, code.MSG_ERR, "该用户尚未加入队伍")
  97. if score != 0 {
  98. _, err = member.IncrScoreById(member.Id, score)
  99. t.CheckErr(err)
  100. member.Score += score
  101. }
  102. // 根据team_id 获取所有成员的分数
  103. members := make([]*models.BaheTeamMember, 0)
  104. err = core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=?",
  105. member.TeamId, member.RehearsalId).Desc("score").Asc("sort_time").Find(&members)
  106. t.CheckErr(err)
  107. var rank int
  108. for index, m := range members {
  109. if m.Id == member.Id {
  110. rank = index + 1
  111. break
  112. }
  113. }
  114. t.JSON(map[string]interface{}{
  115. "rank": rank, // 队内排名
  116. "score": member.Score, // 人员分数
  117. })
  118. }
  119. type JoinTeamResult struct {
  120. Number int
  121. TeamId int64
  122. TeamName string
  123. }
  124. func (t *TugOfWarCtl) JoinTeam() {
  125. teamId := t.MustGetInt64("bahe_team_id")
  126. baheActivityId := t.MustGetInt64("bahe_activity_id")
  127. uid := t.MustGetUID()
  128. bahe := new(models.TugOfWar)
  129. exist, err := models.Get(bahe, baheActivityId)
  130. t.CheckErr(err)
  131. t.Assert(exist, code.MSG_TUGWAR_TEAM_OVER_LIMIT, "拔河不存在")
  132. if bahe.Status == define.StatusEnding {
  133. t.ERROR("该拔河活动已结束", code.MSG_MODULE_STATUS_END)
  134. }
  135. activity := new(models.Activity)
  136. exist, err = models.Get(activity, bahe.ActivityId)
  137. t.CheckErr(err)
  138. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  139. t.CheckRunning(activity.Status)
  140. // 判斷是否已經存在该用户
  141. member := new(models.BaheTeamMember)
  142. exist, err = member.GetMemberByBaheIdAndUserId(uid, bahe.Id, activity.RehearsalId)
  143. t.CheckErr(err)
  144. if exist { // 存在改用户
  145. t.JSON(map[string]interface{}{
  146. "member": member,
  147. })
  148. return
  149. }
  150. team := new(JoinTeamResult)
  151. session := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
  152. Select("t.id as team_id, t.bahe_team_name as team_name, count(m.id) as number").
  153. Join("LEFT", new(models.BaheTeamMember).Alias("m"),
  154. "m.team_id=t.id and m.is_delete=0 and m.rehearsal_id=?", activity.RehearsalId)
  155. if teamId == 0 { // 人数最少的一队
  156. session = session.Where("t.is_delete=0 and t.bahe_activity_id=? ", bahe.Id).
  157. GroupBy("t.id").Asc("number")
  158. } else {
  159. session = session.Where("t.is_delete=0 and t.id=?", teamId).GroupBy("t.id")
  160. }
  161. exist, err = session.Get(team)
  162. t.CheckErr(err)
  163. t.Assert(exist, code.MSG_ERR, "队伍信息错误, 请重新扫码")
  164. t.Assert(team.Number < bahe.Number, code.MSG_TUGWAR_TEAM_OVER_LIMIT, "队伍满人,请等待下一轮")
  165. // done: 均等分配 + 人数限制
  166. user := new(models.User)
  167. exist, err = models.Get(user, uid)
  168. t.CheckErr(err)
  169. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  170. member.TeamId = team.TeamId
  171. member.TeamName = team.TeamName
  172. member.MemberId = uid
  173. member.Avatar = user.Avatar
  174. member.NickName = user.Nickname
  175. member.BaheActivityId = baheActivityId
  176. member.RehearsalId = activity.RehearsalId
  177. member.SortTime = time.Now().UnixNano()
  178. member.CreatedAt = time.Now()
  179. member.UpdatedAt = time.Now()
  180. _, err = core.GetXormAuto().InsertOne(member)
  181. t.CheckErr(err)
  182. t.JSON(map[string]interface{}{
  183. "member": member,
  184. })
  185. }