Beispiel #1
0
// To-do: Show programs solved by the user
func (this *UserController) Show() {
	if this.Ctx.Input.Param("0") == "" {
		this.Abort("404")
	}
	user := models.User{Username: this.Ctx.Input.Param("0")}
	if user.GetByUsername() {
		this.Data["title"] = user.Username
		this.Data["userDetails"] = user

		log := models.Problemlogs{Uid: user.Uid}
		logs, count := log.GetByUid()
		problems := make(map[int]models.Problem)
		if count == 0 {
			this.Data["solvedProblemsExist"] = false
		} else {
			this.Data["solvedProblemsExist"] = true
			for index, element := range logs {
				p := models.Problem{Pid: element.Pid}
				p.GetByPid()
				problems[index] = p
			}
		}

		this.Data["solvedProblems"] = problems

		this.Layout = "layout.tpl"
		this.TplNames = "user/show.tpl"
		this.LayoutSections = make(map[string]string)
		this.LayoutSections["HtmlHead"] = ""
		this.LayoutSections["Sidebar"] = ""
	} else {
		this.Abort("404")
	}
}
// /contest/:name/:pid/submit
func (this *ContestController) submit() {
	if !this.isLoggedIn() {
		this.Redirect("/user/login", 302)
		return
	}
	contestName := this.Ctx.Input.Param(":name")
	pid, err := strconv.Atoi(this.Ctx.Input.Param(":pid"))
	if err != nil {
		log.Println(err)
		this.Redirect("/", 302)
		return
	}
	contest := models.Contest{}
	contest.Name = contestName
	if err := contest.GetByName(); !err {
		this.Redirect("/", 302)
		return
	}
	currTime := time.Now()
	if contest.EndTime.Before(currTime) {
		this.Redirect("/contest/"+contestName, 302)
		return
	}
	uid := this.GetSession("id")
	code := this.GetString("code")
	lang := this.GetString("language")

	problemLog := models.Problemlogs{}
	problemLog.Uid = uid.(int)
	problemLog.Pid = pid
	problemLog.GetByPidUid()

	output := models.SubmitUpdateScore(uid.(int), pid, code, lang)

	contestLog := models.Contestlogs{}
	contestLog.Uid = uid.(int)
	contestLog.Cid = contest.Id
	status := contestLog.GetByUidCid()
	if !status {
		contestLog.Points = output.Score
		contestLog.Add()
	} else {
		if output.Score > problemLog.Points {
			contestLog.Points += output.Score - problemLog.Points
			contestLog.Update()
		}
	}

	js, _ := json.Marshal(output)
	this.Data["json"] = string(js)
	this.ServeJson()

}
// Serves the Problem Page
// To-do: Show recently solved users and their language on sidebar
// To-do: Later, add least execution time log on sidebar
func (this *ProblemController) ProblemById() {
	pid := this.Ctx.Input.Param(":id")
	id, err := strconv.Atoi(pid)
	if err != nil {
		// Redirect to 404
		this.Abort("404")
	}
	p := models.Problem{Pid: id}
	p.GetById()
	if strings.Contains(p.Type, "contest") {
		this.Redirect("/", 302)
		return
	}
	log := models.Problemlogs{Pid: id}
	logs, count := log.GetRecentByPid()
	users := make(map[int]models.User)
	if count == 0 {
		this.Data["recentlySolvedUsersExist"] = false
	} else {
		this.Data["recentlySolvedUsersExist"] = true
		for index, element := range logs {
			u := models.User{Uid: element.Uid}
			u.GetUserInfo()
			users[index] = u
		}
	}

	//Author added
	user := models.User{}
	user.Uid = p.Uid
	user.GetUserInfo()
	this.Data["title"] = p.Statement
	this.Data["problem"] = p
	this.Data["Author"] = user.Username
	this.Data["recentlySolvedUsers"] = users

	// Handle problem log of a user
	if this.isLoggedIn() {
		problemLog := models.Problemlogs{}
		problemLog.Pid = p.Pid
		problemLog.Uid = p.Uid
		if problemLog.GetByPidUid() {
			this.Data["userScore"] = problemLog.Points
			this.Data["solvedCount"] = problemLog.Solved
		}
	}

	this.Layout = "layout.tpl"
	this.TplNames = "problem/show.tpl"
	this.LayoutSections = make(map[string]string)
	this.LayoutSections["HtmlHead"] = "problem/submit_head.tpl"
	this.LayoutSections["Sidebar"] = "sidebar/recently_solved_by.tpl"
}
// /contest/:name/:pid
func (this *ContestController) GetProblem() {
	pid := this.Ctx.Input.Param(":id")
	contestName := this.Ctx.Input.Param(":name")
	id, err := strconv.Atoi(pid)
	if err != nil {
		// Redirect to 404
		this.Abort("404")
	}
	p := models.Problem{Pid: id}
	p.GetById()
	check := strings.Contains(p.Type, contestName)
	if !check {
		this.Redirect("/contest/"+contestName, 302)
		return
	}
	log := models.Problemlogs{Pid: id}
	logs, count := log.GetRecentByPid()
	users := make(map[int]models.User)
	if count == 0 {
		this.Data["recentlySolvedUsersExist"] = false
	} else {
		this.Data["recentlySolvedUsersExist"] = true
		for index, element := range logs {
			u := models.User{Uid: element.Uid}
			u.GetUserInfo()
			users[index] = u
		}
	}

	this.Data["title"] = p.Statement
	this.Data["problem"] = p
	this.Data["Author"] = contestName
	this.Data["recentlySolvedUsers"] = users

	// Handle problem log of a user
	if this.isLoggedIn() {
		problemLog := models.Problemlogs{}
		problemLog.Pid = p.Pid
		problemLog.Uid = p.Uid
		if problemLog.GetByPidUid() {
			this.Data["userScore"] = problemLog.Points
			this.Data["solvedCount"] = problemLog.Solved
		}
	}
}