Ejemplo n.º 1
0
func taskAuthFunc(userid string, auth *taskAuth, redis *models.RedisLogger) error {
	user := &models.Account{}
	user.FindByUserid(auth.Userid)

	record := &models.Record{Uid: user.Id, Task: auth.Id}
	record.FindByTask(auth.Id)
	awards := controllers.Awards{}

	parent := &models.Article{}
	parent.FindByRecord(record.Id.Hex())
	if len(parent.Id) > 0 && len(auth.Reason) > 0 {
		review := &models.Article{
			Parent:   parent.Id.Hex(),
			Author:   userid,
			Title:    auth.Reason,
			Type:     models.ArticleCoach,
			Contents: []models.Segment{{ContentType: "TEXT", ContentText: auth.Reason}},
			PubTime:  time.Now(),
		}
		review.Save()
	}

	if auth.Pass {
		level := user.Level()
		awards = controllers.Awards{
			Physical: 3 + level,
			Wealth:   3 * models.Satoshi,
			Score:    3 + level,
		}
		awards.Level = models.Score2Level(user.Props.Score+awards.Score) - level

		controllers.GiveAwards(user, awards, redis)

		if record.Sport != nil {
			redis.UpdateRecLB(user.Id, record.Sport.Distance, int(record.Sport.Duration))
		}

		record.SetStatus(models.StatusFinish, auth.Reason, awards.Wealth)
		if auth.Id < 1000 {
			user.UpdateTask(int(auth.Id), models.StatusFinish)
		}
	} else {
		record.SetStatus(models.StatusUnFinish, auth.Reason, 0)
		if auth.Id < 1000 {
			user.UpdateTask(int(auth.Id), models.StatusUnFinish)
		}
		parent.SetPrivilege(models.PrivPrivate)
	}

	// ws push
	event := &models.Event{
		Type: models.EventNotice,
		Time: time.Now().Unix(),
		Data: models.EventData{
			Type: models.EventTaskDone,
			To:   user.Id,
			Body: []models.MsgBody{
				{Type: "physique_value", Content: strconv.FormatInt(awards.Physical, 10)},
				{Type: "coin_value", Content: strconv.FormatInt(awards.Wealth, 10)},
			},
		},
	}
	if auth.Id < 1000 {
		event.Data.Body = append(event.Data.Body, models.MsgBody{Type: "task_id", Content: strconv.Itoa(int(auth.Id))})
	}

	if !auth.Pass {
		event.Data.Type = models.EventTaskFailure
	}
	redis.PubMsg(event.Type, event.Data.To, event.Bytes())
	event.Save()

	event = &models.Event{
		Type: models.EventArticle,
		Time: time.Now().Unix(),
		Data: models.EventData{
			Type: models.EventCoachPass,
			Id:   parent.Id.Hex(),
			From: userid,
			To:   parent.Author,
			Body: []models.MsgBody{
				{Type: "total_count", Content: strconv.Itoa(parent.CoachReviewCount + 1)},
				{Type: "image", Content: ""},
			},
		},
	}
	if !auth.Pass {
		event.Data.Type = models.EventCoachNPass
	}
	event.Save()
	redis.PubMsg(event.Type, event.Data.To, event.Bytes())

	return nil
}