|
|
package client
import ( "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" )
type CalorieCtl struct { controllers.AuthorCtl }
// h5 1/s
func (t *CalorieCtl) Shake() { calorieId := t.MustGetInt64("calorie_id") score := t.DefaultInt("score", 0) uid := t.MustGetUID()
calorie := new(models.Calorie) exist, err := models.Get(calorie, calorieId) t.CheckErr(err) t.Assert(exist, code.MSG_CALORIE_NOT_EXIST, "卡路里不存在")
activity := new(models.Activity) exist, err = models.Get(activity, calorie.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") t.CheckRunning(activity.Status)
calorieUser := new(models.CalorieUser) exist, err = calorieUser.GetByCalorieIdAndUserId(calorieId, uid, activity.RehearsalId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_NOT_EXIST, "您尚未参与卡路里活动")
// 增加score
if calorie.Status == define.StatusRunning && score > 0 { _, err = calorieUser.IncrScore(score) t.CheckErr(err) calorieUser.Score += int64(score) // 增加
}
count, err := core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=?", calorie.Id, activity.RehearsalId).Count(new(models.CalorieUser)) t.CheckErr(err)
users := make([]*models.CalorieUser, 0) err = core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=? and score<=?", calorie.Id, activity.RehearsalId, calorieUser.Score).Desc("score").Asc("join_time").Find(&users) t.CheckErr(err)
var rank int for index, user := range users { if user.Id == calorieUser.Id { rank = int(count) - len(users) + index + 1 break } }
// 活动时长
//duration := calorie.GameDuration - (time.Now().Unix() - calorie.StartTime) + 6 // 加上倒计时的6s
//if calorie.StartTime == 0 {
// duration = calorie.GameDuration
//}
// 3s 倒计时
//var countDown float64 = 0
//if duration-calorie.GameDuration > 0 {
// countDown = math.Ceil(float64(duration-calorie.GameDuration) / 2.0)
//}
//// 活动结束
//if calorie.Status == define.StatusEnding || duration <= 0 {
// duration = 0
//}
t.JSON(map[string]interface{}{ "rank": rank, "score": calorieUser.Score, // 分数
//"time": duration - 2*int64(countDown), //时长
//"count_down": countDown, // 倒计时
//"status": calorie.Status, // 状态
}) }
|