Beispiel #1
0
func LoginHandler(w http.ResponseWriter, r *http.Request) {
	email, password := r.PostFormValue("email"), r.PostFormValue("password")

	u, err := model.SignInUser(email, password)
	switch {
	case err == model.ErrUserNotFound:
		fmt.Fprintln(w, "Invalid username or password.")
		return
	}

	session.Set(w, r, u.Id)

	numProfiles := model.GetNumProfiles(u.Id)
	numQueued := model.GetNumQueuedProfiles(u.Id)
	switch {
	case numProfiles == 0 && numQueued == 0:
		http.Redirect(w, r, "/upload", 302)
	default:
		http.Redirect(w, r, "/", 302)
	}
}
Beispiel #2
0
func ProfileHandler(w http.ResponseWriter, r *http.Request) {
	session, _ := session.Get(r, "tiers")
	userid, ok := session.Values["user"]

	templates := loadTemplates(
		"header.html",
		"footer.html",
		"nav.html",
		"index-unauthed.html",
		"profile.html",
	)

	if ok {
		var profiles []profile.Profile

		period := mux.Vars(r)["period"]

		var p profile.Profile
		var diff profile.Profile

		queue := model.GetNumQueuedProfiles(userid.(int))

		switch period {
		case "daily":
			period = "Daily"
			p1 := model.GetNewestProfile(userid.(int))
			p2 := model.GetProfileByTimestamp(userid.(int), time.Now().AddDate(0, 0, -1).Unix())

			profiles = append(profiles, p1, p2)
		case "weekly":
			period = "Weekly"
			p1 := model.GetNewestProfile(userid.(int))
			p2 := model.GetProfileByTimestamp(userid.(int), time.Now().AddDate(0, 0, -7).Unix())

			profiles = append(profiles, p1, p2)
		case "monthly":
			period = "Monthly"
			p1 := model.GetNewestProfile(userid.(int))
			p2 := model.GetProfileByTimestamp(userid.(int), time.Now().AddDate(0, -1, 0).Unix())

			profiles = append(profiles, p1, p2)
		default:
			period = "Previous"
			profiles = model.GetNewestProfiles(userid.(int), 2)
		}

		switch len(profiles) {
		case 1:
			p = profiles[0]
		case 2:
			diff = profile.Diff(profiles[1], profiles[0])
			p = profiles[0]
		}

		templates.ExecuteTemplate(w, "profile", ProfilePage{
			User: userid.(int),
			Data: ProfilePageData{
				Period:  period,
				Profile: p,
				Diff:    diff,
				Queue:   queue,
			},
		})
	} else {
		templates.ExecuteTemplate(w, "index-unauthed", nil)
	}
}
Beispiel #3
0
func ProgressHandler(w http.ResponseWriter, r *http.Request) {
	templates := loadTemplates(
		"header.html",
		"footer.html",
		"nav.html",
		"progress.html",
	)

	session, _ := session.Get(r, "tiers")
	userid, ok := session.Values["user"]

	if !ok {
		http.Redirect(w, r, "/", 302)
		return
	}

	var view view
	view.User = userid.(int)

	limit := time.Now().Unix() - int64((time.Hour * 24 * 30).Seconds())
	profiles := model.GetAllProfiles(view.User, limit)

	if len(profiles) == 0 {
		view.Warning = "Not enough data to generate page. :("
		templates.ExecuteTemplate(w, "progress", &view)
		return
	}

	view.Queue = model.GetNumQueuedProfiles(userid.(int))

	var x = map[string][]int64{}
	var y = map[string][]int64{}

	for k := range profiles {
		p := &profiles[k]
		profile.HandleProfile(p)
		x["AP"] = append(x["AP"], p.Timestamp)
		y["AP"] = append(y["AP"], p.AP)
	}

	newestProfile := profiles[len(profiles)-1]
	if newestProfile.AP < newestProfile.NextLevel.AP {
		var ts int64

		if y["AP"][0] == y["AP"][len(y["AP"])-1] {
			ts = -1
		} else {
			slope, intercept, _ := lineaRegression(x["AP"], y["AP"])
			ts = int64(((float64(newestProfile.NextLevel.AP) - intercept) / slope)) + 1262304000
		}

		perc := (float64(int(float64(newestProfile.AP)/float64(newestProfile.NextLevel.AP)*1000)) / 10)
		view.AP = progress{
			Name: "AP",

			Tiers: []tier{
				{
					Name:       "info",
					Percentage: perc,
				},
			},

			Current:  newestProfile.AP,
			Required: newestProfile.NextLevel.AP,

			Expected: ts,
		}
	} else {
		view.AP = progress{
			Name: "AP",

			Tiers: []tier{
				{
					Name:       "info",
					Percentage: 100,
				},
			},

			Current:  newestProfile.AP,
			Required: newestProfile.NextLevel.AP,

			Expected: -2,
		}
	}

	var badges []progress

	for badgeName := range profile.BadgeRanks {
		if badgeName == "Innovator" {
			continue
		}

		badges = append(badges, progress{
			Name: badgeName,
		})
	}

	for _, badge := range badges {
		for _, p := range profiles {
			s := reflect.ValueOf(&p.Badges).Elem()
			typeOf := s.Type()

			for i := 0; i < s.NumField(); i++ {
				f := s.Field(i)
				name := typeOf.Field(i).Name
				current := f.FieldByName("Current").Int()

				if badge.Name == name && shouldIncludeBadge(badge.Name, p.Timestamp, current) {
					x[name] = append(x[name], p.Timestamp)
					y[name] = append(y[name], current)
				}
			}
		}

		if badge.Name == "Guardian" && len(x["Guardian"]) > 1 {
			var i int
			var n = len(x["Guardian"]) - 1

			xValue := x["Guardian"][n]
			yValue := y["Guardian"][n]

			for i = n - 1; i > 0; i-- {
				var curX = x["Guardian"][i]
				var curY = y["Guardian"][i]

				diff := xValue - curX
				if curY != yValue {
					if diff <= 86400 {
						xValue = curX
						yValue = curY
					}
				} else if diff > 86400 {
					i++
					break
				}
			}

			x["Guardian"] = x["Guardian"][i : n+1]
			y["Guardian"] = y["Guardian"][i : n+1]
		}

		s := reflect.ValueOf(&newestProfile.Badges).Elem()
		typeOf := s.Type()

		for i := 0; i < s.NumField(); i++ {
			f := s.Field(i)
			name := typeOf.Field(i).Name

			if badge.Name == name {
				badge.Rank = int(f.FieldByName("Rank").Int())
				badge.Current = f.FieldByName("Current").Int()
				badge.Required = f.FieldByName("Next").Int()

				if len(y[name]) > 0 && y[name][0] == y[name][len(y[name])-1] {
					badge.Expected = -2
				} else if badge.Current < badge.Required {
					slope, intercept, _ := lineaRegression(x[badge.Name], y[badge.Name])
					ts := int64(((float64(badge.Required) - intercept) / slope)) + 1262304000

					badge.Expected = ts
				} else {
					badge.Expected = -1
				}

				var total float64
				max := float64(badge.Required)
				for rank, value := range profile.BadgeRanks[name] {
					if rank <= badge.Rank {
						perc := (float64(int(float64(value)/max*1000)) / 10) - total

						badge.Tiers = append(badge.Tiers, tier{
							Name:       rankNames[rank],
							Percentage: perc,
						})

						total += perc
					}
				}

				if badge.Rank != 4 {
					badge.Tiers = append(badge.Tiers, tier{
						Name:       rankNames[badge.Rank+1],
						Percentage: (float64(int(float64(badge.Current)/max*1000)) / 10) - total,
					})
				}

				badge.Icon = fmt.Sprintf("images/badges/%s/%d.png", strings.ToLower(badge.Name), badge.Rank+1)
			}
		}

		view.NextLevel = newestProfile.NextLevel.Level
		view.Progress = append(view.Progress, badge)
	}

	var inprogress []progress
	var completed []progress
	for _, v := range view.Progress {
		switch v.Expected {
		case -1:
			completed = append(completed, v)
		default:
			inprogress = append(inprogress, v)
		}
	}

	sort.Sort(progressByExpected{inprogress})
	sort.Sort(progressByName{completed})

	view.Progress = inprogress
	view.Completed = completed

	templates.ExecuteTemplate(w, "progress", &view)
}