互动
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.

171 lines
4.7 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
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. )
  10. type TugOfWarCtl struct {
  11. controllers.AuthorCtl
  12. }
  13. func (t *TugOfWarCtl) Status() {
  14. // 获取此次活动的状态
  15. baheId := t.MustGetInt64("bahe_activity_id")
  16. uid := t.MustGetUID()
  17. // 找到活动
  18. bahe := new(models.TugOfWar)
  19. exist, err := models.Get(bahe, baheId)
  20. t.CheckErr(err)
  21. t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在")
  22. // 主活动
  23. activity := new(models.Activity)
  24. exist, err = models.Get(activity, bahe.ActivityId)
  25. t.CheckErr(err)
  26. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存")
  27. // 找到用户
  28. member := new(models.BaheTeamMember)
  29. exist, err = member.GetMemberByBaheIdAndUserId(uid, bahe.Id, activity.RehearsalId)
  30. t.CheckErr(err)
  31. t.Assert(exist, code.MSG_TUGWAR_MEMBER_NOT_EXIST, "队员不存在")
  32. score, err := bahe_service.GetScoreByTeamId(member.TeamId, activity.RehearsalId)
  33. t.CheckErr(err)
  34. rank := 1
  35. team := new(models.BaheTeam)
  36. exist, err = models.Get(team, member.TeamId)
  37. t.CheckErr(err)
  38. t.Assert(exist, code.MSG_TUGWAR_TEAM_NOT_EXIST, "队伍不存在")
  39. otherTeam := new(models.BaheTeam)
  40. exist, err = otherTeam.GetOtherTeam(member.TeamId, team.BaheActivityId)
  41. t.CheckErr(err)
  42. t.Assert(exist, code.MSG_TUGWAR_TEAM_NOT_EXIST, "队伍不存在")
  43. otherScore, err := bahe_service.GetScoreByTeamId(otherTeam.Id, activity.RehearsalId)
  44. t.CheckErr(err)
  45. if otherScore > score {
  46. rank = 2
  47. }
  48. t.JSON(map[string]interface{}{
  49. "status": bahe.Status,
  50. "score": score,
  51. "rank": rank,
  52. })
  53. }
  54. func (t *TugOfWarCtl) Shake() {
  55. uid := t.MustGetUID()
  56. baheId := t.MustGetInt64("bahe_activity_id")
  57. score := t.MustGetInt64("score") // 增加的分数
  58. bahe := new(models.TugOfWar)
  59. exist, err := models.Get(bahe, baheId)
  60. t.CheckErr(err)
  61. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河活动不存在")
  62. t.CheckRunning(bahe.Status)
  63. activity := new(models.Activity)
  64. exist, err = models.Get(activity, bahe.ActivityId)
  65. t.CheckErr(err)
  66. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  67. t.CheckRunning(activity.Status)
  68. member := new(models.BaheTeamMember)
  69. exist, err = member.GetMemberByBaheIdAndUserId(uid, baheId, activity.RehearsalId)
  70. t.CheckErr(err)
  71. t.Assert(exist, code.MSG_ERR, "该用户尚未加入队伍")
  72. if score != 0 {
  73. _, err = member.IncrScoreById(member.Id, score)
  74. t.CheckErr(err)
  75. member.Score += score
  76. }
  77. // 根据team_id 获取所有成员的分数
  78. members, err := models.GetBaheMembersByTeamId(member.TeamId, member.RehearsalId)
  79. t.CheckErr(err)
  80. var rank int
  81. for index, m := range members {
  82. if m.Id == member.Id {
  83. rank = index + 1
  84. break
  85. }
  86. }
  87. t.JSON(map[string]interface{}{
  88. "rank": rank, // 队内排名
  89. "score": member.Score, // 人员分数
  90. })
  91. }
  92. func (t *TugOfWarCtl) JoinTeam() {
  93. teamId := t.MustGetInt64("bahe_team_id")
  94. baheActivityId := t.MustGetInt64("bahe_activity_id")
  95. uid := t.MustGetUID()
  96. bahe := new(models.TugOfWar)
  97. exist, err := models.Get(bahe, baheActivityId)
  98. t.CheckErr(err)
  99. t.Assert(exist, code.MSG_TUGWAR_TEAM_OVER_LIMIT, "拔河不存在")
  100. if bahe.Status == define.StatusEnding {
  101. t.ERROR("该拔河活动已结束", code.MSG_MODULE_STATUS_END)
  102. }
  103. activity := new(models.Activity)
  104. exist, err = models.Get(activity, bahe.ActivityId)
  105. t.CheckErr(err)
  106. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  107. t.CheckRunning(activity.Status)
  108. // 判斷是否已經存在该用户
  109. member := new(models.BaheTeamMember)
  110. exist, err = member.GetMemberByBaheIdAndUserId(uid, bahe.Id, activity.RehearsalId)
  111. t.CheckErr(err)
  112. if exist { // 存在改用户
  113. t.JSON(map[string]interface{}{
  114. "member": member,
  115. })
  116. return
  117. }
  118. var team *bahe_service.JoinTeamResult
  119. if teamId == 0 {
  120. team, exist, err = bahe_service.GetJoinTeamByBaheId(bahe.Id, activity.RehearsalId)
  121. } else {
  122. team, exist, err = bahe_service.GetJoinTeamByTeamId(teamId, activity.RehearsalId)
  123. }
  124. t.CheckErr(err)
  125. t.Assert(exist, code.MSG_ERR, "队伍信息错误, 请重新扫码")
  126. t.Assert(team.Number < bahe.Number, code.MSG_TUGWAR_TEAM_OVER_LIMIT, "队伍满人,请等待下一轮")
  127. // done: 均等分配 + 人数限制
  128. user := new(models.User)
  129. exist, err = models.Get(user, uid)
  130. t.CheckErr(err)
  131. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  132. member.TeamId = team.TeamId
  133. member.TeamName = team.TeamName
  134. member.MemberId = uid
  135. member.Avatar = user.Avatar
  136. member.NickName = user.Nickname
  137. member.BaheActivityId = baheActivityId
  138. member.RehearsalId = activity.RehearsalId
  139. member.SortTime = time.Now().UnixNano()
  140. member.CreatedAt = time.Now()
  141. member.UpdatedAt = time.Now()
  142. _, err = models.Add(member)
  143. t.CheckErr(err)
  144. t.JSON(map[string]interface{}{
  145. "member": member,
  146. })
  147. }