互动
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.9 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
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(activity.ArchId, 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 := &models.TugOfWar{}
  59. exist, err := models.Get(bahe, baheId)
  60. t.CheckErr(err)
  61. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河活动不存在")
  62. activity := &models.Activity{}
  63. exist, err = models.Get(activity, bahe.ActivityId)
  64. t.CheckErr(err)
  65. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  66. t.CheckRunning(activity.Status)
  67. member := &models.BaheTeamMember{}
  68. exist, err = member.GetMemberByBaheIdAndUserId(activity.ArchId, uid, baheId, activity.RehearsalId)
  69. t.CheckErr(err)
  70. t.Assert(exist, code.MSG_ERR, "该用户尚未加入队伍")
  71. if bahe.Status == define.StatusRunning && score > 0 { // 增加分数
  72. _, err = member.IncrScoreById(activity.ArchId, member.Id, score)
  73. t.CheckErr(err)
  74. member.Score += score
  75. }
  76. // 根据team_id 获取所有成员的分数
  77. members, err := models.GetBaheMembersByTeamId(member.TeamId, member.RehearsalId)
  78. t.CheckErr(err)
  79. var rank int
  80. for index, m := range members {
  81. if m.Id == member.Id {
  82. rank = index + 1
  83. break
  84. }
  85. }
  86. t.JSON(map[string]interface{}{
  87. "rank": rank, // 队内排名
  88. "score": member.Score, // 人员分数
  89. })
  90. }
  91. func (t *TugOfWarCtl) JoinTeam() {
  92. teamId := t.MustGetInt64("bahe_team_id")
  93. baheActivityId := t.MustGetInt64("bahe_activity_id")
  94. uid := t.MustGetUID()
  95. bahe := new(models.TugOfWar)
  96. exist, err := models.Get(bahe, baheActivityId)
  97. t.CheckErr(err)
  98. t.Assert(exist, code.MSG_TUGWAR_TEAM_OVER_LIMIT, "拔河不存在")
  99. if bahe.Status == define.StatusEnding {
  100. t.ERROR("该拔河活动已结束", code.MSG_MODULE_STATUS_END)
  101. }
  102. activity := new(models.Activity)
  103. exist, err = models.Get(activity, bahe.ActivityId)
  104. t.CheckErr(err)
  105. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  106. t.CheckRunning(activity.Status)
  107. // 判斷是否已經存在该用户
  108. member := new(models.BaheTeamMember)
  109. exist, err = member.GetMemberByBaheIdAndUserId(activity.ArchId, uid, bahe.Id, activity.RehearsalId)
  110. t.CheckErr(err)
  111. if exist { // 存在改用户
  112. t.JSON(map[string]interface{}{
  113. "member": member,
  114. })
  115. return
  116. }
  117. var team *bahe_service.JoinTeamResult
  118. if teamId == 0 {
  119. team, exist, err = bahe_service.GetJoinTeamByBaheId(bahe.Id, activity.RehearsalId)
  120. } else {
  121. team, exist, err = bahe_service.GetJoinTeamByTeamId(teamId, activity.RehearsalId)
  122. }
  123. t.CheckErr(err)
  124. t.Assert(exist, code.MSG_ERR, "队伍信息错误, 请重新扫码")
  125. t.Assert(team.Number < bahe.Number, code.MSG_TUGWAR_TEAM_OVER_LIMIT, "队伍满人,请等待下一轮")
  126. // done: 均等分配 + 人数限制
  127. user := new(models.User)
  128. exist, err = models.Get(user, uid)
  129. t.CheckErr(err)
  130. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  131. member.TeamId = team.TeamId
  132. member.TeamName = team.TeamName
  133. member.MemberId = uid
  134. member.Avatar = user.Avatar
  135. member.ArchId = activity.ArchId
  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. }