package client import ( "hudongzhuanjia/controllers" "hudongzhuanjia/models" bahe_service "hudongzhuanjia/services/bahe" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "time" ) type TugOfWarCtl struct { controllers.AuthorCtl } func (t *TugOfWarCtl) Status() { // 获取此次活动的状态 baheId := t.MustGetInt64("bahe_activity_id") uid := t.MustGetUID() // 找到活动 bahe := new(models.TugOfWar) exist, err := models.Get(bahe, baheId) t.CheckErr(err) t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在") // 主活动 activity := new(models.Activity) exist, err = models.Get(activity, bahe.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存") // 找到用户 member := new(models.BaheTeamMember) exist, err = member.GetMemberByBaheIdAndUserId(uid, bahe.Id, activity.RehearsalId) t.CheckErr(err) t.Assert(exist, code.MSG_TUGWAR_MEMBER_NOT_EXIST, "队员不存在") score, err := bahe_service.GetScoreByTeamId(member.TeamId, activity.RehearsalId) t.CheckErr(err) rank := 1 team := new(models.BaheTeam) exist, err = models.Get(team, member.TeamId) t.CheckErr(err) t.Assert(exist, code.MSG_TUGWAR_TEAM_NOT_EXIST, "队伍不存在") otherTeam := new(models.BaheTeam) exist, err = otherTeam.GetOtherTeam(member.TeamId, team.BaheActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_TUGWAR_TEAM_NOT_EXIST, "队伍不存在") otherScore, err := bahe_service.GetScoreByTeamId(otherTeam.Id, activity.RehearsalId) t.CheckErr(err) if otherScore > score { rank = 2 } t.JSON(map[string]interface{}{ "status": bahe.Status, "score": score, "rank": rank, }) } func (t *TugOfWarCtl) Shake() { uid := t.MustGetUID() baheId := t.MustGetInt64("bahe_activity_id") score := t.MustGetInt64("score") // 增加的分数 bahe := new(models.TugOfWar) exist, err := models.Get(bahe, baheId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河活动不存在") t.CheckRunning(bahe.Status) activity := new(models.Activity) exist, err = models.Get(activity, bahe.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") t.CheckRunning(activity.Status) member := new(models.BaheTeamMember) exist, err = member.GetMemberByBaheIdAndUserId(uid, baheId, activity.RehearsalId) t.CheckErr(err) t.Assert(exist, code.MSG_ERR, "该用户尚未加入队伍") if score != 0 { _, err = member.IncrScoreById(member.Id, score) t.CheckErr(err) member.Score += score } // 根据team_id 获取所有成员的分数 members, err := models.GetBaheMembersByTeamId(member.TeamId, member.RehearsalId) t.CheckErr(err) var rank int for index, m := range members { if m.Id == member.Id { rank = index + 1 break } } t.JSON(map[string]interface{}{ "rank": rank, // 队内排名 "score": member.Score, // 人员分数 }) } func (t *TugOfWarCtl) JoinTeam() { teamId := t.MustGetInt64("bahe_team_id") baheActivityId := t.MustGetInt64("bahe_activity_id") uid := t.MustGetUID() bahe := new(models.TugOfWar) exist, err := models.Get(bahe, baheActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_TUGWAR_TEAM_OVER_LIMIT, "拔河不存在") if bahe.Status == define.StatusEnding { t.ERROR("该拔河活动已结束", code.MSG_MODULE_STATUS_END) } activity := new(models.Activity) exist, err = models.Get(activity, bahe.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") t.CheckRunning(activity.Status) // 判斷是否已經存在该用户 member := new(models.BaheTeamMember) exist, err = member.GetMemberByBaheIdAndUserId(uid, bahe.Id, activity.RehearsalId) t.CheckErr(err) if exist { // 存在改用户 t.JSON(map[string]interface{}{ "member": member, }) return } var team *bahe_service.JoinTeamResult if teamId == 0 { team, exist, err = bahe_service.GetJoinTeamByBaheId(bahe.Id, activity.RehearsalId) } else { team, exist, err = bahe_service.GetJoinTeamByTeamId(teamId, activity.RehearsalId) } t.CheckErr(err) t.Assert(exist, code.MSG_ERR, "队伍信息错误, 请重新扫码") t.Assert(team.Number < bahe.Number, code.MSG_TUGWAR_TEAM_OVER_LIMIT, "队伍满人,请等待下一轮") // done: 均等分配 + 人数限制 user := new(models.User) exist, err = models.Get(user, uid) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") member.TeamId = team.TeamId member.TeamName = team.TeamName member.MemberId = uid member.Avatar = user.Avatar member.NickName = user.Nickname member.BaheActivityId = baheActivityId member.RehearsalId = activity.RehearsalId member.SortTime = time.Now().UnixNano() member.CreatedAt = time.Now() member.UpdatedAt = time.Now() _, err = models.Add(member) t.CheckErr(err) t.JSON(map[string]interface{}{ "member": member, }) }