Example #1
0
func loginAwardsHandler(request *http.Request, resp http.ResponseWriter,
	redis *models.RedisLogger, user *models.Account) {

	a := user.LoginAwards
	if (user.LoginDays-1)%7 == 0 || len(a) == 0 {
		a = []int64{}
		startDay := ((user.LoginDays - 1) / 7) * 7
		level := user.Level()
		score := user.Props.Score

		for i := 0; i < 7; i++ {
			awards := loginAwards(startDay+i+1, int(level))
			a = append(a, awards.Wealth, awards.Score)
			score = score + awards.Score
			level = models.Score2Level(score)
		}

		user.SetLoginAwards(a)
	}

	index := (user.LoginDays - 1) % 7
	awards := Awards{Wealth: a[index*2], Score: a[index*2+1]}
	awards.Level = models.Score2Level(user.Props.Score+awards.Score) - user.Level()
	GiveAwards(user, awards, redis)

	loginAwards := []int64{}
	for i := 0; i < 7; i++ {
		loginAwards = append(loginAwards, a[i*2])
	}
	respData := map[string]interface{}{
		"continuous_logined_days": user.LoginDays,
		"login_reward_list":       loginAwards,
	}
	writeResponse(request.RequestURI, resp, respData, nil)
}
Example #2
0
func GiveAwards(user *models.Account, awards Awards, redis *models.RedisLogger) error {
	if awards.Level < 0 || awards.Score < 0 {
		panic("invalid level or score")
	}
	if _, err := sendCoin(user.Wallet.Addr, awards.Wealth); err != nil {
		return err
	}
	redis.SendCoins(user.Id, awards.Wealth)

	err := user.UpdateProps(models.Props{
		Physical: awards.Physical,
		Literal:  awards.Literal,
		Mental:   awards.Mental,
		//Wealth:   awards.Wealth,
		Score: awards.Score,
		//Level: awards.Level,
	})
	if err != nil {
		return err
	}

	if lvl := models.Score2Level(user.Props.Score + awards.Score); lvl > user.Level() {
		// ws push
		event := &models.Event{
			Type: models.EventNotice,
			Time: time.Now().Unix(),
			Data: models.EventData{
				Type: models.EventLevelUP,
				To:   user.Id,
			},
		}
		event.Save()
		redis.PubMsg(event.Type, event.Data.To, event.Bytes())
	}

	return nil
}
Example #3
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
}