Esempio n. 1
0
func newRecordHandler(request *http.Request, resp http.ResponseWriter,
	redis *models.RedisLogger, user *models.Account, p Parameter) {

	form := p.(newRecordForm)

	rec := &models.Record{
		Uid:       user.Id,
		Task:      form.Task,
		Type:      form.Record.Type,
		StartTime: time.Unix(form.Record.BeginTime, 0),
		EndTime:   time.Unix(form.Record.EndTime, 0),
		PubTime:   time.Now(),
	}

	awards := Awards{}
	level := user.Level()

	switch form.Record.Type {
	case "post":
		rec.Status = models.StatusFinish
		if form.Task > 0 {
			rec.Coin = (2 + level) * models.Satoshi
			awards = Awards{
				Score:   2 + level,
				Literal: 2 + level,
				Wealth:  rec.Coin,
			}
		} else {
			rec.Coin = 2 * models.Satoshi
			awards = Awards{
				Score:   2,
				Literal: 2,
				Wealth:  rec.Coin,
			}
		}

		GiveAwards(user, awards, redis)

		redis.AddPost(user.Id, "", 1)
		user.UpdateStat(models.StatArticles, 1)
	case "game":
		if form.Task > 0 {
			awards = gameAwards(level, form.Record.GameScore, true)
			//user.AddTask(models.Tasks[form.Task-1].Type, form.Task, nil)
			rec.Status = models.StatusFinish
		} else {
			if form.Record.GameScore >= 100 &&
				user.Stat != nil && user.Stat.LastGameTime < nowDate().Unix() {
				awards = gameAwards(level, form.Record.GameScore, false)
			}
			user.UpdateStat(models.StatLastGameTime, time.Now().Unix())
		}
		GiveAwards(user, awards, redis)

		rec.Game = &models.GameRecord{
			Type:     form.Record.GameType,
			Name:     form.Record.GameName,
			Duration: form.Record.Duration,
			Score:    form.Record.GameScore,
			Magic:    int(awards.Mental),
		}
		rec.Coin = awards.Wealth

		redis.AddGameTime(user.Id, int(form.Record.Duration))
		user.UpdateStat(models.StatGameTime, form.Record.Duration)
		redis.SetGameMaxScore(gameType(rec.Game.Type), user.Id, rec.Game.Score)
		user.SetGameTime(gameType(rec.Game.Type), time.Now())

	case "run":
		if rec.Task > 0 {
			rec.Delete()
		}
		rec.Sport = &models.SportRecord{
			Source:    form.Record.Source,
			Duration:  form.Record.Duration,
			Distance:  form.Record.Distance,
			Weight:    form.Record.Weight,
			Mood:      form.Record.Mood,
			HeartRate: form.Record.HeartRate,
			Pics:      form.Record.Pics,
		}

		if rec.Sport.Weight == 0 {
			rec.Sport.Weight = user.Weight
		}
		// update weight
		if rec.Sport.Weight != user.Weight {
			user.SetInfo(&models.SetInfo{Weight: rec.Sport.Weight})
		}

		if form.Record.Duration > 0 {
			rec.Sport.Speed = float64(form.Record.Distance) / float64(form.Record.Duration)
		}
		rec.Status = models.StatusAuth

		redis.AddRecord(user.Id, 1)
		user.UpdateStat(models.StatRecords, 1)
		/*
			if len(form.Record.Source) > 0 {
				level := user.Level()
				awards = Awards{
					Physical: 30 + level,
					Wealth:   30 * models.Satoshi,
					Score:    30 + level,
				}

				GiveAwards(user, awards, redis)
				redis.UpdateRecLB(user.Id, rec.Sport.Distance, int(rec.Sport.Duration))

				rec.Coin = awards.Wealth
				rec.Status = models.StatusFinish
			}
		*/

	default:
		log.Println("Unknown record type:", form.Record.Type)
	}
	// assign task id
	if rec.Task == 0 {
		rec.Task = rec.PubTime.Unix()
	}
	if err := rec.Save(); err != nil {
		writeResponse(request.RequestURI, resp, nil, err)
		return
	}

	if rec.Type == "run" {
		article := &models.Article{
			Author:  user.Id,
			PubTime: time.Now(),
			Loc:     user.Loc,
			Record:  rec.Id.Hex(),
			Type:    models.ArticleRecord,
		}

		if !form.Public {
			article.Privilege = models.PrivPrivate
		}

		if err := article.Save(); err == nil {
			redis.AddPost(user.Id, "", 1)
			user.UpdateStat(models.StatArticles, 1)
		}
	}

	if form.Task > 0 {
		user.UpdateTask(int(form.Task), rec.Status)
		/*
			if rec.Status == models.StatusFinish {
				// 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: "task_id", Content: strconv.Itoa(int(form.Task))},
							{Type: "literature_value", Content: strconv.FormatInt(awards.Literal, 10)},
							{Type: "magic_value", Content: strconv.FormatInt(awards.Mental, 10)},
							{Type: "coin_value", Content: strconv.FormatInt(awards.Wealth, 10)},
						},
					},
				}
				event.Save()
				redis.PubMsg(event.Type, event.Data.To, event.Bytes())
			}
		*/
	}

	respData := map[string]interface{}{
		"record_id": rec.Id.Hex(),
		"ExpEffect": awards,
	}
	writeResponse(request.RequestURI, resp, respData, nil)
}
Esempio n. 2
0
func getTaskHandler(r *http.Request, w http.ResponseWriter,
	user *models.Account, p Parameter) {
	form := p.(getTaskForm)

	tid := user.Taskid
	status := user.TaskStatus

	if tid == 0 {
		rec, _ := user.LastTaskRecord2()
		tid = int(rec.Task + 1) // next task
		status = rec.Status
		if status == "" {
			status = models.StatusNormal
		}
		user.UpdateTask(tid, status)
	}

	if status == "" {
		status = models.StatusNormal
	}

	if form.Next {
		if status == models.StatusFinish {
			tid++
		}
		if status == models.StatusFinish || status == models.StatusUnFinish {
			status = models.StatusNormal
		}

		user.UpdateTask(tid, status)
	}

	if tid > len(models.NewTasks) {
		writeResponse(r.RequestURI, w, nil, nil)
	}

	task := models.NewTasks[tid-1]
	task.Status = status

	config := &models.Config{}
	config.Find()
	if task.Index < len(config.Videos) {
		video := config.Videos[task.Index]
		task.Video = video.Url
		/*
			if len(video.Desc) > 0 {
				task.Desc = video.Desc
			}
		*/
	}

	var stat struct {
		Distance int `json:"distance"`
		Run      int `json:"run"`
		Article  int `json:"article"`
		Game     int `json:"game"`
	}

	stat.Article, _ = user.TaskRecordCount("post", models.StatusFinish)
	stat.Game, _ = user.TaskRecordCount("game", models.StatusFinish)
	records, _ := user.TaskRecords("run")
	stat.Run = len(records)
	for i, _ := range records {
		stat.Distance += records[i].Sport.Distance
	}

	respData := map[string]interface{}{
		"task": task,
		"stat": stat,
	}
	writeResponse(r.RequestURI, w, respData, nil)
}