Beispiel #1
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)
}
Beispiel #2
0
func getTasksHandler(r *http.Request, w http.ResponseWriter, user *models.Account) {

	/*
		tasklist := user.Tasks

		week := len(tasklist.Completed) / 7
		if week > 0 && len(tasklist.Completed)%7 == 0 &&
			tasklist.Last.After(now.BeginningOfWeek()) {
			week -= 1
		}
		list := make([]models.Task, 7)
		if week*7+7 <= len(models.Tasks)+1 {
			copy(list, models.Tasks[week*7:week*7+7])
		}
		for i, _ := range list {
			list[i].Status = tasklist.TaskStatus(list[i].Id)
			if list[i].Type == models.TaskGame && list[i].Status == "FINISH" {
				rec := &models.Record{Uid: user.Id}
				rec.FindByTask(list[i].Id)
				if rec.Game != nil {
					list[i].Desc = fmt.Sprintf("你在%s游戏中得了%d分",
						rec.Game.Name, rec.Game.Score)
				}
			}
		}
	*/
	count, _ := user.TaskRecordCount("", models.StatusFinish)
	week := count / 7

	var target, actual int

	last, _ := user.LastTaskRecord()
	// all weekly tasks are completed
	if week > 0 && count%7 == 0 && last.AuthTime.After(now.BeginningOfWeek()) {
		week -= 1
	}
	//log.Println("week", week)
	tasks := make([]models.Task, 7)
	if week*7+7 <= len(models.Tasks) {
		copy(tasks, models.Tasks[week*7:week*7+7])
	}

	for i, task := range tasks {
		tasks[i].Status = models.StatusNormal
		record := &models.Record{Uid: user.Id}

		if find, _ := record.FindByTask(task.Id); find {
			tasks[i].Status = record.Status
		}
		if task.Type == models.TaskGame && task.Status == models.StatusFinish &&
			record.Game != nil {
			tasks[i].Result = fmt.Sprintf("你在%s游戏中得了%d分",
				record.Game.Name, record.Game.Score)
		}
		if task.Type == models.TaskRunning {
			target += task.Distance
			if tasks[i].Status == models.StatusFinish && record.Sport != nil {
				actual += record.Sport.Distance
			}
		}
	}
	//log.Println(tasks)
	//random := rand.New(rand.NewSource(time.Now().Unix()))
	respData := map[string]interface{}{
		"week_id":              week + 1,
		"task_list":            tasks,
		"task_target_distance": target,
		"task_actual_distance": actual,
		//"week_desc": tips[random.Int()%len(tips)],
	}

	writeResponse(r.RequestURI, w, respData, nil)
}