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

83 lines
2.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package client
  2. import (
  3. "github.com/ouxuanserver/osmanthuswine/src/core"
  4. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/models"
  6. "hudongzhuanjia/utils/code"
  7. "hudongzhuanjia/utils/define"
  8. )
  9. type CalorieCtl struct {
  10. controllers.AuthorCtl
  11. }
  12. // h5 1/s
  13. func (t *CalorieCtl) Shake() {
  14. calorieId := t.MustGetInt64("calorie_id")
  15. score := t.DefaultInt("score", 0)
  16. uid := t.MustGetUID()
  17. calorie := new(models.Calorie)
  18. exist, err := models.Get(calorie, calorieId)
  19. t.CheckErr(err)
  20. t.Assert(exist, code.MSG_CALORIE_NOT_EXIST, "卡路里不存在")
  21. activity := new(models.Activity)
  22. exist, err = models.Get(activity, calorie.ActivityId)
  23. t.CheckErr(err)
  24. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  25. t.CheckRunning(activity.Status)
  26. calorieUser := new(models.CalorieUser)
  27. exist, err = calorieUser.GetByCalorieIdAndUserId(calorieId, uid, activity.RehearsalId)
  28. t.CheckErr(err)
  29. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "您尚未参与卡路里活动")
  30. // 增加score
  31. if calorie.Status == define.StatusRunning && score > 0 {
  32. _, err = calorieUser.IncrScore(score)
  33. t.CheckErr(err)
  34. calorieUser.Score += int64(score) // 增加
  35. }
  36. count, err := core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=?", calorie.Id, activity.RehearsalId).Count(new(models.CalorieUser))
  37. t.CheckErr(err)
  38. users := make([]*models.CalorieUser, 0)
  39. err = core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=? and score<=?",
  40. calorie.Id, activity.RehearsalId, calorieUser.Score).Desc("score").Asc("join_time").Find(&users)
  41. t.CheckErr(err)
  42. var rank int
  43. for index, user := range users {
  44. if user.Id == calorieUser.Id {
  45. rank = int(count) - len(users) + index + 1
  46. break
  47. }
  48. }
  49. // 活动时长
  50. //duration := calorie.GameDuration - (time.Now().Unix() - calorie.StartTime) + 6 // 加上倒计时的6s
  51. //if calorie.StartTime == 0 {
  52. // duration = calorie.GameDuration
  53. //}
  54. // 3s 倒计时
  55. //var countDown float64 = 0
  56. //if duration-calorie.GameDuration > 0 {
  57. // countDown = math.Ceil(float64(duration-calorie.GameDuration) / 2.0)
  58. //}
  59. //// 活动结束
  60. //if calorie.Status == define.StatusEnding || duration <= 0 {
  61. // duration = 0
  62. //}
  63. t.JSON(map[string]interface{}{
  64. "rank": rank,
  65. "score": calorieUser.Score, // 分数
  66. //"time": duration - 2*int64(countDown), //时长
  67. //"count_down": countDown, // 倒计时
  68. //"status": calorie.Status, // 状态
  69. })
  70. }