|
|
package client
import ( "hudongzhuanjia/controllers" "hudongzhuanjia/models" bahe_service "hudongzhuanjia/services/bahe" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "time"
"github.com/ouxuanserver/osmanthuswine/src/core" )
type TugOfWarCtl struct { controllers.AuthorCtl }
// 获取当前状态(废弃)
// @Summary client tug war
// @Description get current status
// @Tags client tug war
// @Accept json
// @Produce json
// @Param bahe_activity_id query int true "Bahe Activity ID"
// @Success 0 {object} models.TugOfWar
// @Failure 503 {string} string "参数不存在"
// @Failure 504 {object} string "用户不存在"
// @Router /Client/TugOfWarCtl/status [get]
func (t *TugOfWarCtl) Status() { // 获取此次活动的状态
baheId := t.MustGetInt64("bahe_activity_id") uid := t.MustGetUID() // 找到活动
bahe := new(models.TugOfWar) exist, err := models.GetById(bahe, baheId) t.CheckErr(err) t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在")
// 主活动
activity := new(models.Activity) exist, err = models.GetById(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.GetById(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, }) }
// tug_war doc
// @Summary tug war
// @Description shake bahe activity
// @Tags tug war
// @Accept json
// @Produce json
// @Param bahe_activity_id query int true "Bahe Activity ID"
// @Success 0 {object} models.TugOfWar
// @Success 0 {object} models.BaheTeamMember
// @Success 0 {object} models.BaheTeam
// @Failure 404 {string} string "参数不存在"
// @Failure 405 {object} string "用户不存在"
// @Router /Client/TugOfWarCtl/shake [get]
func (t *TugOfWarCtl) Shake() { uid := t.MustGetUID() baheId := t.MustGetInt64("bahe_activity_id") score := t.MustGetInt64("score") // 增加的分数
bahe := new(models.TugOfWar) exist, err := models.GetById(bahe, baheId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河活动不存在") t.CheckRunning(bahe.Status)
activity := new(models.Activity) exist, err = models.GetById(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 := make([]*models.BaheTeamMember, 0) err = core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=?", member.TeamId, member.RehearsalId).Desc("score").Asc("sort_time").Find(&members) 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, // 人员分数
}) }
type JoinTeamResult struct { Number int TeamId int64 TeamName string }
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.GetById(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.GetById(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 }
team := new(JoinTeamResult) session := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t"). Select("t.id as team_id, t.bahe_team_name as team_name, count(m.id) as number"). Join("LEFT", new(models.BaheTeamMember).Alias("m"), "m.team_id=t.id and m.is_delete=0 and m.rehearsal_id=?", activity.RehearsalId) if teamId == 0 { // 人数最少的一队
session = session.Where("t.is_delete=0 and t.bahe_activity_id=? ", bahe.Id). GroupBy("t.id").Asc("number") } else { session = session.Where("t.is_delete=0 and t.id=?", teamId).GroupBy("t.id") } exist, err = session.Get(team) 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.GetById(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 = core.GetXormAuto().InsertOne(member) t.CheckErr(err) t.JSON(map[string]interface{}{ "member": member, }) }
|