Esempio n. 1
0
func (this *RanklistController) List(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("RankList")
	this.InitContest(w, r)
	qry := make(map[string]string)
	qry["module"] = strconv.Itoa(config.ModuleC)
	qry["mid"] = strconv.Itoa(this.Cid)
	qry["/sort"] = "resort"

	solutionModel := model.SolutionModel{}
	solutionList, err := solutionModel.List(qry)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	UserMap := make(map[string]*userRank)
	var pro *probleminfo
	var user *userRank
	for _, v := range solutionList {
		user = UserMap[v.Uid]
		if user == nil {
			user = &userRank{}
			UserMap[v.Uid] = user
			user.ProblemList = make([]*probleminfo, len(this.Index), len(this.Index))
		}
		user.Uid = v.Uid
		pid := this.Index[v.Pid]
		pro = user.ProblemList[pid]
		if pro == nil {
			pro = &probleminfo{}
			user.ProblemList[pid] = pro
		}
		if pro.Judge == config.JudgeAC {
			continue
		}
		pro.Pid = pid
		if v.Judge != config.JudgeAC && v.Judge != config.JudgePD && v.Judge != config.JudgeRJ {
			pro.Count++
			pro.Time += 20 * 60 //罚时20分钟
		} else if v.Judge == config.JudgeAC {
			pro.Time += v.Create - this.ContestDetail.Start
			pro.Judge = config.JudgeAC
			user.Time += pro.Time
			user.Solved += 1
		}
	}
	UserList := newSorter(UserMap)
	sort.Sort(UserList)

	this.Data["UserList"] = UserList
	this.Data["IsContestRanklist"] = true
	this.Data["Cid"] = this.Cid
	this.Data["ProblemList"] = this.Index
	err = this.Execute(w, "view/layout.tpl", "view/contest/ranklist.tpl")
	if err != nil {
		http.Error(w, "tpl error", 500)
		return
	}
	return
}
Esempio n. 2
0
func (this *StatusController) List(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Contest Status List")
	this.InitContest(w, r)

	solutionModel := model.SolutionModel{}
	qry := make(map[string]string)

	qry["module"] = strconv.Itoa(config.ModuleC)
	qry["mid"] = strconv.Itoa(this.Cid)
	solutionList, err := solutionModel.List(qry)

	if err != nil {
		http.Error(w, "load error", 400)
		return
	}
	for i, v := range solutionList {
		solutionList[i].Pid = this.Index[v.Pid]
	}
	this.Data["Solution"] = solutionList
	this.Data["Privilege"] = this.Privilege
	this.Data["IsContestStatus"] = true
	err = this.Execute(w, "view/layout.tpl", "view/contest/status_list.tpl")
	if err != nil {
		http.Error(w, "tpl error", 500)
		return
	}
}
Esempio n. 3
0
//@URL: /contests/(\d+)/status/(\d+)/code @method:GET
func (sc *ContestStatus) Code(Cid string, Sid string) {
	restweb.Logger.Debug("Status Code")

	sc.InitContest(Cid)
	sid, err := strconv.Atoi(Sid)
	if err != nil {
		sc.Error("args error", 400)
		return
	}

	solutionModel := model.SolutionModel{}
	one, err := solutionModel.Detail(sid)
	if err != nil {
		sc.Error(err.Error(), 500)
		return
	}

	if one.Error != "" {
		one.Code += "\n/*\n" + one.Error + "\n*/"
	}
	if one.Uid == sc.Uid || sc.Privilege >= config.PrivilegeTC {
		sc.Output["Solution"] = one
		sc.Output["Privilege"] = sc.Privilege
		sc.Output["Title"] = "View Code"
		sc.Output["IsCode"] = true
		sc.Output["Pid"] = sc.Index[one.Pid]
		sc.RenderTemplate("view/layout.tpl", "view/contest/status_code.tpl")
	} else {
		sc.Err400("Warning", "You can't see it!")
	}
}
Esempio n. 4
0
func (this *StatusController) Code(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Status Code")
	this.Init(w, r)
	this.InitContest(w, r)

	args := this.ParseURL(r.URL.String())
	sid, err := strconv.Atoi(args["sid"])
	if err != nil {
		http.Error(w, "args error", 400)
		return
	}

	solutionModel := model.SolutionModel{}
	one, err := solutionModel.Detail(sid)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	if one.Uid == this.Uid || this.Privilege > config.PrivilegeTC {
		this.Data["Solution"] = one
		this.Data["Privilege"] = this.Privilege
		this.Data["Title"] = "View Code"
		this.Data["IsCode"] = true
		err = this.Execute(w, "view/layout.tpl", "view/contest/status_code.tpl")
		if err != nil {
			class.Logger.Debug(err)
			http.Error(w, "tpl error", 500)
			return
		}
	}
}
Esempio n. 5
0
func (this *UserController) Detail(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("User Detail")
	this.Init(w, r)

	args := this.ParseURL(r.URL.String())
	uid := args["uid"]
	userModel := model.UserModel{}
	one, err := userModel.Detail(uid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	this.Data["Detail"] = one

	solutionModle := model.SolutionModel{}
	solvedList, err := solutionModle.Achieve(uid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	this.Data["List"] = solvedList
	//class.Logger.Debug(solvedList)
	this.Data["Title"] = "User Detail"
	if uid != "" && uid == this.Uid {
		this.Data["IsSettings"] = true
		this.Data["IsSettingsDetail"] = true
	}

	err = this.Execute(w, "view/layout.tpl", "view/user_detail.tpl")
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
}
Esempio n. 6
0
//@URL: /status/code @method: GET
func (sc *StatusController) Code() {
	restweb.Logger.Debug("Status Code")

	sid, err := strconv.Atoi(sc.Input.Get("sid"))
	if err != nil {
		http.Error(sc.W, "args error", 400)
		return
	}

	solutionModel := model.SolutionModel{}
	one, err := solutionModel.Detail(sid)
	if err != nil {
		http.Error(sc.W, err.Error(), 400)
		return
	}
	if one.Error != "" {
		one.Code = one.Code + "\n/*\n" + one.Error + "*/\n"
	}

	if one.Uid == sc.Uid || sc.Privilege > config.PrivilegePU || one.Share {
		sc.Output["Solution"] = one
		sc.Output["Title"] = "View Code"
		sc.Output["IsCode"] = true
		sc.RenderTemplate("view/layout.tpl", "view/status_code.tpl")
	} else {
		sc.Err400("Warning", "You can't see it!")
	}
}
Esempio n. 7
0
func (this *Contest) GetCount(qry map[string]string) (int, error) {
	if qry == nil {
		qry = make(map[string]string)
	}
	qry["module"] = strconv.Itoa(config.ModuleC)
	qry["mid"] = strconv.Itoa(this.Cid)
	solutionModel := model.SolutionModel{}
	count, err := solutionModel.Count(qry)
	if err != nil {
		return 0, err
	}
	return count, nil
}
Esempio n. 8
0
func (s *solution) Init(info Info) {
	logger.Println(info)

	solutionModel := model.SolutionModel{}
	sol, err := solutionModel.Detail(info.Sid)
	if err != nil {
		logger.Println(err)
		return
	}
	s.Solution = *sol

	s.OJ = info.OJ
	s.Vid = info.Pid
}
Esempio n. 9
0
//ranklist 实时计算排名
func (rc *ContestRanklist) ranklist() UserSorter {
	qry := make(map[string]string)
	qry["module"] = strconv.Itoa(config.ModuleC)
	qry["mid"] = strconv.Itoa(rc.Cid)
	qry["sort"] = "resort"

	solutionModel := model.SolutionModel{}
	solutionList, err := solutionModel.List(qry)
	if err != nil {
		restweb.Logger.Debug(err)
		return nil
	}

	UserMap := make(map[string]*userRank)
	var pro *probleminfo
	var user *userRank
	for _, v := range solutionList {
		user = UserMap[v.Uid]
		if user == nil {
			user = &userRank{}
			UserMap[v.Uid] = user
			user.ProblemList = make([]*probleminfo, len(rc.Index), len(rc.Index))
		}
		user.Uid = v.Uid
		pid := rc.Index[v.Pid]
		pro = user.ProblemList[pid]
		if pro == nil {
			pro = &probleminfo{}
			user.ProblemList[pid] = pro
		}
		if pro.Judge == config.JudgeAC {
			continue
		}
		pro.Pid = pid
		if v.Judge != config.JudgeAC && v.Judge != config.JudgePD && v.Judge != config.JudgeRJ && v.Judge != config.JudgeNA {
			pro.Count++
			pro.Time += 20 * 60 //罚时20分钟
		} else if v.Judge == config.JudgeAC {
			user.Time += pro.Time
			pro.Time = v.Create - rc.ContestDetail.Start
			pro.Judge = config.JudgeAC
			user.Time += pro.Time
			user.Solved += 1
		}
	}
	UserList := newSorter(UserMap)
	sort.Sort(UserList)
	return UserList
}
Esempio n. 10
0
//@URL: /users/(.+) @method: GET
func (uc *UserController) Detail(uid string) {
	restweb.Logger.Debug("User Detail", uid)

	userModel := model.UserModel{}
	one, err := userModel.Detail(uid)
	if err != nil {
		uc.Error(err.Error(), 400)
		return
	}
	uc.Output["Detail"] = one

	solutionModle := model.SolutionModel{}
	solvedList, err := solutionModle.Achieve(uid, config.ModuleP, config.ModuleP)
	if err != nil {
		uc.Error(err.Error(), 400)
		return
	}

	type IPs struct {
		Time int64
		IP   string
	}
	var ips []IPs
	ipo := IPs{}

	for i, lenth := 0, len(one.IPRecord); i < lenth; i++ {
		ipo.Time = one.TimeRecord[i]
		ipo.IP = one.IPRecord[i]
		ips = append(ips, ipo)
	}

	achieveList := sort.IntSlice(solvedList)
	achieveList.Sort()
	uc.Output["List"] = achieveList
	uc.Output["IpList"] = ips
	uc.Output["Title"] = "User Detail"
	if uid != "" && uid == uc.Uid {
		uc.Output["IsSettings"] = true
		uc.Output["IsSettingsDetail"] = true
	}

	uc.RenderTemplate("view/layout.tpl", "view/user_detail.tpl")
}
Esempio n. 11
0
func (this *UserController) Settings(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("User Settings")
	this.Init(w, r)

	if this.Privilege == config.PrivilegeNA {
		this.Data["Title"] = "Warning"
		this.Data["Info"] = "You must login!"
		err := this.Execute(w, "view/layout.tpl", "view/400.tpl")
		if err != nil {
			http.Error(w, "tpl error", 500)
			return
		}
		return
	}

	userModel := model.UserModel{}

	one, err := userModel.Detail(this.Uid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	this.Data["Detail"] = one

	solutionModel := model.SolutionModel{}
	solvedList, err := solutionModel.Achieve(this.Uid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	this.Data["List"] = solvedList

	this.Data["Title"] = "User Settings"
	this.Data["IsSettings"] = true
	this.Data["IsSettingsDetail"] = true

	err = this.Execute(w, "view/layout.tpl", "view/user_detail.tpl")
	if err != nil {
		http.Error(w, "tpl error", 500)
		return
	}
}
Esempio n. 12
0
//UpdateSolution 更新判题结果
func (this *solution) UpdateSolution() {
	sid, err := strconv.Atoi(strconv.Itoa(this.Sid))

	solutionModel := model.SolutionModel{}
	ori, err := solutionModel.Detail(sid)
	if err != nil {
		logger.Println(err)
		return
	}

	ori.Judge = this.Judge
	ori.Time = this.Time
	ori.Memory = this.Memory
	ori.Sim = this.Sim
	ori.Sim_s_id = this.Sim_s_id
	ori.Error = this.Error

	err = solutionModel.Update(sid, *ori)
	if err != nil {
		logger.Println(err)
		return
	}
}
Esempio n. 13
0
func (this *solution) UpdateRecord() {
	if this.Module != config.ModuleP {
		return
	}

	solutionModel := model.SolutionModel{}
	qry := make(map[string]string)
	qry["module"] = strconv.Itoa(config.ModuleP)
	qry["action"] = "submit"
	qry["pid"] = strconv.Itoa(this.Pid)

	submit, _ := solutionModel.Count(qry)

	qry["action"] = "solve"
	solve, _ := solutionModel.Count(qry)

	proModel := model.ProblemModel{}
	err := proModel.Record(this.Pid, solve, submit)
	if err != nil {
		logger.Println(err)
	}

	qry["action"] = "submit"
	qry["uid"] = this.Uid
	delete(qry, "pid")
	delete(qry, "module")
	submit, _ = solutionModel.Count(qry)

	solvelist, err := solutionModel.Achieve(this.Uid, config.ModuleP, config.ModuleP)
	if err != nil {
		logger.Println(err)
	}
	solve = len(solvelist)

	userModel := model.UserModel{}
	err = userModel.Record(this.Uid, solve, submit)
	if err != nil {
		logger.Println(err)
	}
}
Esempio n. 14
0
func (this *ProblemController) Rejudge(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Problem Rejudge")
	this.Init(w, r)

	if this.Privilege < config.PrivilegeTC {
		this.Err400(w, r, "Warning", "Error Privilege to Rejudge problem")
		return
	}

	args := this.ParseURL(r.URL.String())
	types := args["type"]
	id, err := strconv.Atoi(args["id"])
	if err != nil {
		http.Error(w, "args error", 400)
		return
	}

	hint := make(map[string]string)

	if types == "Pid" {
		pid := id
		proModel := model.ProblemModel{}
		pro, err := proModel.Detail(pid)
		if err != nil {
			class.Logger.Debug(err)
			hint["info"] = "Problem does not exist!"

			b, _ := json.Marshal(&hint)
			w.WriteHeader(400)
			w.Write(b)

			return
		}
		qry := make(map[string]string)
		qry["pid"] = strconv.Itoa(pro.Pid)

		solutionModel := model.SolutionModel{}
		list, err := solutionModel.List(qry)

		for i := range list {
			sid := list[i].Sid

			time.Sleep(1 * time.Second)
			go func() {
				cmd := exec.Command("./RunServer", "-sid", strconv.Itoa(sid), "-time", strconv.Itoa(pro.Time), "-memory", strconv.Itoa(pro.Memory), "-rejudge", strconv.Itoa(1)) //Run Judge
				err = cmd.Run()
				if err != nil {
					class.Logger.Debug(err)
				}
			}()
		}
	} else if types == "Sid" {
		sid := id

		solutionModel := model.SolutionModel{}
		sol, err := solutionModel.Detail(sid)
		if err != nil {
			class.Logger.Debug(err)

			hint["info"] = "Solution does not exist!"
			b, _ := json.Marshal(&hint)
			w.WriteHeader(400)
			w.Write(b)

			return
		}

		problemModel := model.ProblemModel{}
		pro, err := problemModel.Detail(sol.Pid)
		if err != nil {
			http.Error(w, err.Error(), 500)
			return
		}

		go func() {
			cmd := exec.Command("./RunServer", "-sid", strconv.Itoa(sid), "-time", strconv.Itoa(pro.Time), "-memory", strconv.Itoa(pro.Memory), "-rejudge", strconv.Itoa(1)) //Run Judge
			err = cmd.Run()
			if err != nil {
				class.Logger.Debug(err)
			}
		}()
	}
}
Esempio n. 15
0
//@URL: /status @method: GET
func (sc *StatusController) List() {
	restweb.Logger.Debug("Status List")

	searchUrl := ""
	qry := make(map[string]string)
	// Search
	if v, ok := sc.Input["uid"]; ok {
		searchUrl += "uid=" + v[0] + "&"
		sc.Output["SearchUid"] = v[0]
		qry["uid"] = v[0]
	}
	if v, ok := sc.Input["pid"]; ok {
		searchUrl += "pid=" + v[0] + "&"
		sc.Output["SearchPid"] = v[0]
		qry["pid"] = v[0]
	}
	if v, ok := sc.Input["judge"]; ok {
		searchUrl += "judge=" + v[0] + "&"
		sc.Output["SearchJudge"+v[0]] = v[0]
		qry["judge"] = v[0]
	}
	if v, ok := sc.Input["language"]; ok {
		searchUrl += "language=" + v[0] + "&"
		sc.Output["SearchLanguage"+v[0]] = v[0]
		qry["language"] = v[0]
	}
	sc.Output["URL"] = "/status?" + searchUrl

	// Page
	qry["page"] = "1"

	if v, ok := sc.Input["page"]; ok {
		qry["page"] = v[0]
	}

	solutionModel := model.SolutionModel{}
	qry["module"] = strconv.Itoa(config.ModuleP)
	qry["action"] = "submit"
	count, err := solutionModel.Count(qry)
	if err != nil {
		sc.Error(err.Error(), 400)
		return
	}
	var pageCount = (count-1)/config.SolutionPerPage + 1

	page, err := strconv.Atoi(qry["page"])
	if err != nil {
		sc.Error("args error", 400)
		return
	}
	if page > pageCount {
		sc.Error("args error", 400)
		return
	}
	qry["offset"] = strconv.Itoa((page - 1) * config.SolutionPerPage)
	qry["limit"] = strconv.Itoa(config.SolutionPerPage)

	pageData := sc.GetPage(page, pageCount)
	for k, v := range pageData {
		sc.Output[k] = v
	}

	list, err := solutionModel.List(qry)
	if err != nil {
		sc.Error(err.Error(), 500)
		return
	}

	sc.Output["Solution"] = list
	sc.Output["Title"] = "Status List"
	sc.Output["IsStatus"] = true
	sc.Output["Privilege"] = sc.Privilege
	sc.Output["Uid"] = sc.Uid

	sc.RenderTemplate("view/layout.tpl", "view/status_list.tpl")
}
Esempio n. 16
0
func (this *ProblemController) Submit(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Contest Problem Submit")
	this.InitContest(w, r)

	args := this.ParseURL(r.URL.String())

	pid, err := strconv.Atoi(args["pid"])
	if err != nil {
		http.Error(w, "args error", 400)
		return
	}

	pid = this.ContestDetail.List[pid] //get real pid
	uid := this.Uid
	if uid == "" {
		w.WriteHeader(401)
		return
	}

	one := model.Solution{}
	one.Pid = pid
	one.Uid = uid
	one.Mid = this.ContestDetail.Cid
	one.Module = config.ModuleC

	problemModel := model.ProblemModel{}
	pro, err := problemModel.Detail(pid)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	code := r.FormValue("code")
	one.Code = code
	one.Length = this.GetCodeLen(len(r.FormValue("code")))
	one.Language, _ = strconv.Atoi(r.FormValue("compiler_id"))

	info := ""
	errflag := true
	switch {
	case pro.Pid == 0 || (pro.Status == config.StatusReverse && this.Privilege <= config.PrivilegePU):
		info = "No such problem"
	case code == "":
		info = "Your source code is too short"
	case time.Now().Unix() > this.ContestDetail.End:
		info = "The contest has ended"
	default:
		errflag = false
	}
	if errflag {
		this.Err400(w, r, "Problem — "+strconv.Itoa(pid), info)
		return
	}

	one.Status = config.StatusAvailable
	one.Judge = config.JudgePD

	solutionModle := model.SolutionModel{}
	sid, err := solutionModle.Insert(one)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	w.WriteHeader(200)
	go func() {
		class.Logger.Debug(sid)
		cmd := exec.Command("./RunServer", "-sid", strconv.Itoa(sid), "-time", strconv.Itoa(pro.Time), "-memory", strconv.Itoa(pro.Memory)) //Run Judge
		err = cmd.Run()
		if err != nil {
			class.Logger.Debug(err)
		}
	}()
}
Esempio n. 17
0
//get_sim 相似度检测,返回值为相似度和相似的id
func (this *solution) get_sim(Sid, Language int) (sim, Sim_s_id int) {
	var extension string

	if this.Language == config.LanguageC {
		extension = "c"
	} else if this.Language == config.LanguageCPP {
		extension = "cc"
	} else if this.Language == config.LanguageJAVA {
		extension = "java"
	}

	qry := make(map[string]string)
	qry["pid"] = strconv.Itoa(this.Pid)
	qry["action"] = "solve"

	solutionModel := model.SolutionModel{}
	list, err := solutionModel.List(qry)
	workdir := runPath + "/" + strconv.Itoa(this.Sid)

	sim_test_dir := workdir + "/sim_test"

	cmd := exec.Command("mkdir", "-p", sim_test_dir)
	err = cmd.Run()
	if err != nil {
		logger.Println(err)
		return
	}
	defer os.RemoveAll(workdir)

	codefile, err := os.Create(sim_test_dir + "/../Main." + extension)
	defer codefile.Close()

	_, err = codefile.WriteString(this.Code)
	if err != nil {
		logger.Println("source code writing to file failed")
	}

	var count int

	for i := range list {
		sid := list[i].Sid

		sol, err := solutionModel.Detail(sid)
		if sid < this.Sid && err == nil {
			filepath := sim_test_dir + "/" + strconv.Itoa(sid) + "." + extension

			codefile, err := os.Create(filepath)
			defer codefile.Close()

			_, err = codefile.WriteString(sol.Code)
			if err != nil {
				logger.Println("source code writing to file failed")
			}
			count++
		}
	}

	cmd = exec.Command("sim.sh", sim_test_dir, extension)

	if err = cmd.Run(); err != nil {
		return
	}
	defer os.Remove("./simfile")

	if _, err := os.Stat("./simfile"); err == nil {

		simfile, err := os.Open("./simfile")

		if err != nil {
			logger.Println("sim file open error")
			return
		}
		defer simfile.Close()
		fmt.Fscanf(simfile, "%d %d", &sim, &Sim_s_id)
	}
	return sim, Sim_s_id
}
Esempio n. 18
0
//@URL: /contests/(\d+)/status @method: GET
func (sc *ContestStatus) List(Cid string) {
	restweb.Logger.Debug("Contest Status List")

	sc.InitContest(Cid)
	solutionModel := model.SolutionModel{}
	qry := make(map[string]string)

	qry["module"] = strconv.Itoa(config.ModuleC)
	qry["mid"] = Cid
	searchUrl := ""
	// Search
	if v, ok := sc.Input["uid"]; ok {
		searchUrl += "uid=" + v[0] + "&"
		sc.Output["SearchUid"] = v[0]
		qry["uid"] = v[0]
	}
	if v, ok := sc.Input["pid"]; ok {
		searchUrl += "pid=" + v[0] + "&"
		sc.Output["SearchPid"] = v[0]
		idx, _ := strconv.Atoi(v[0])
		if idx < len(sc.ContestDetail.List) {
			qry["pid"] = strconv.Itoa(sc.ContestDetail.List[idx])
			restweb.Logger.Debug(qry["pid"], idx)
		}
	}
	if v, ok := sc.Input["judge"]; ok {
		searchUrl += "judge=" + v[0] + "&"
		sc.Output["SearchJudge"+v[0]] = v[0]
		qry["judge"] = v[0]
	}
	if v, ok := sc.Input["language"]; ok {
		searchUrl += "language=" + v[0] + "&"
		sc.Output["SearchLanguage"+v[0]] = v[0]
		qry["language"] = v[0]
	}

	qry["page"] = "1"
	if v, ok := sc.Input["page"]; ok {
		qry["page"] = v[0]
	}

	sc.Output["URL"] = "/contests/" + Cid + "/status?" + searchUrl

	qry["action"] = "submit"
	count, err := solutionModel.Count(qry)
	if err != nil {
		sc.Error(err.Error(), 400)
		return
	}
	var pageCount = (count-1)/config.SolutionPerPage + 1

	page, err := strconv.Atoi(qry["page"])
	if err != nil {
		sc.Error("args error", 400)
		return
	}
	if page > pageCount {
		sc.Error("args error", 400)
		return
	}
	qry["offset"] = strconv.Itoa((page - 1) * config.SolutionPerPage)
	qry["limit"] = strconv.Itoa(config.SolutionPerPage)

	pageData := sc.GetPage(page, pageCount)
	for k, v := range pageData {
		sc.Output[k] = v
	}

	solutionList, err := solutionModel.List(qry)

	if err != nil {
		sc.Error("load error", 400)
		return
	}
	for i, v := range solutionList {
		solutionList[i].Pid = sc.Index[v.Pid]
	}
	sc.Output["Solution"] = solutionList
	sc.Output["Privilege"] = sc.Privilege
	sc.Output["IsContestStatus"] = true
	sc.Output["Privilege"] = sc.Privilege
	sc.Output["Uid"] = sc.Uid

	sc.RenderTemplate("view/layout.tpl", "view/contest/status_list.tpl")
}
Esempio n. 19
0
//提交某一问题的solution, URL /problem?submit/pid?<pid>,method POST
func (this *ProblemController) Submit(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Problem Submit")
	this.Init(w, r)

	if r.Method != "POST" { // 要求请求方法为post
		http.Error(w, "method error", 400)
		return
	}

	args := this.ParseURL(r.URL.String())
	pid, err := strconv.Atoi(args["pid"])
	if err != nil {
		http.Error(w, "args error", 400)
		return
	}

	uid := this.Uid
	if uid == "" {
		http.Error(w, "need sign in", 401)
		return
	}

	var one model.Solution
	one.Pid = pid
	one.Uid = uid
	one.Module = config.ModuleP
	one.Mid = config.ModuleP

	problemModel := model.ProblemModel{}
	pro, err := problemModel.Detail(pid)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	code := r.FormValue("code")

	one.Code = code
	one.Length = this.GetCodeLen(len(r.FormValue("code")))
	one.Language, _ = strconv.Atoi(r.FormValue("compiler_id"))

	if code == "" || pro.Pid == 0 || (pro.Status == config.StatusReverse && this.Privilege <= config.PrivilegePU) {
		switch {
		case pro.Pid == 0 || (pro.Status == config.StatusReverse && this.Privilege <= config.PrivilegePU):
			this.Data["Info"] = "No such problem"
		case code == "":
			this.Data["Info"] = "Your source code is too short"
		}
		this.Data["Title"] = "Problem — " + strconv.Itoa(pid)
		err = this.Execute(w, "view/layout.tpl", "view/400.tpl")
		if err != nil {
			http.Error(w, "tpl error", 500)
			return
		}
		return
	}
	one.Status = config.StatusAvailable
	one.Judge = config.JudgePD

	solutionModel := model.SolutionModel{}
	sid, err := solutionModel.Insert(one)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	w.WriteHeader(200)

	go func() { //编译运行solution
		cmd := exec.Command("./RunServer", "-sid", strconv.Itoa(sid), "-time", strconv.Itoa(pro.Time), "-memory", strconv.Itoa(pro.Memory), "-rejudge", strconv.Itoa(0)) //Run Judge
		err = cmd.Run()
		if err != nil {
			class.Logger.Debug(err)
		}
	}()
}
Esempio n. 20
0
func (this *StatusController) List(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Status List")
	this.Init(w, r)
	args := this.ParseURL(r.URL.String())
	url := "/solution?list"
	searchUrl := ""
	qry := make(map[string]string)
	// Search
	if v, ok := args["uid"]; ok {
		searchUrl += "/uid?" + v
		this.Data["SearchUid"] = v
		qry["uid"] = v
	}
	if v, ok := args["pid"]; ok {
		searchUrl += "/pid?" + v
		this.Data["SearchPid"] = v
		qry["pid"] = v
	}
	if v, ok := args["judge"]; ok {
		searchUrl += "/judge?" + v
		this.Data["SearchJudge"+v] = v
		qry["judge"] = v
	}
	if v, ok := args["language"]; ok {
		searchUrl += "/language?" + v
		this.Data["SearchLanguage"+v] = v
		qry["language"] = v
	}
	url += searchUrl
	this.Data["URL"] = "/status?list" + searchUrl

	// Page
	if _, ok := args["page"]; !ok {
		args["page"] = "1"
	}

	solutionModel := model.SolutionModel{}
	qry["module"] = strconv.Itoa(config.ModuleP)
	qry["action"] = "submit"
	count, err := solutionModel.Count(qry)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	var pageCount = (count-1)/config.SolutionPerPage + 1

	page, err := strconv.Atoi(args["page"])
	if err != nil {
		http.Error(w, "args error", 400)
		return
	}
	if page > pageCount {
		http.Error(w, "args error", 400)
		return
	}
	qry["offset"] = strconv.Itoa((page - 1) * config.SolutionPerPage)
	qry["limit"] = strconv.Itoa(config.SolutionPerPage)

	pageData := this.GetPage(page, pageCount)
	for k, v := range pageData {
		this.Data[k] = v
	}

	list, err := solutionModel.List(qry)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	this.Data["Solution"] = list
	this.Data["Title"] = "Status List"
	this.Data["IsStatus"] = true
	err = this.Execute(w, "view/layout.tpl", "view/status_list.tpl")
	if err != nil {
		http.Error(w, "tpl error", 500)
		return
	}
}
Esempio n. 21
0
//@URL: /contests/(\d+)/problems/(\d+) @method: POST
func (pc *ContestProblem) Submit(Cid, Pid string) {
	restweb.Logger.Debug("Contest Problem Submit")
	pc.InitContest(Cid)

	uid := pc.Uid
	if uid == "" {
		pc.Error("user login required", 401)
		return
	}

	pid, err := strconv.Atoi(Pid)
	if err != nil || pid >= len(pc.ContestDetail.List) {
		pc.Error("args error", 400)
		return
	}

	pid = pc.ContestDetail.List[pid] //get real pid

	one := model.Solution{}
	one.Pid = pid
	one.Uid = uid
	one.Mid = pc.ContestDetail.Cid
	one.Module = config.ModuleC

	problemModel := model.ProblemModel{}
	pro, err := problemModel.Detail(pid)
	if err != nil {
		pc.Error(err.Error(), 500)
		return
	}

	w := pc.W
	code := pc.Input.Get("code")
	one.Code = code
	one.Length = pc.GetCodeLen(len(pc.Input.Get("code")))
	one.Language, _ = strconv.Atoi(pc.Input.Get("compiler_id"))
	pc.SetSession("Compiler_id", pc.Input.Get("compiler_id"))

	hint := make(map[string]string)
	errflag := true
	switch {
	case pro.Pid == 0:
		hint["info"] = "No such problem"
	case code == "":
		hint["info"] = "Your source code is too short"
	case time.Now().Unix() > pc.ContestDetail.End:
		hint["info"] = "The contest has ended"
	default:
		errflag = false
	}
	if errflag {
		b, _ := json.Marshal(&hint)
		w.WriteHeader(400)
		w.Write(b)
		return
	}

	one.Status = config.StatusAvailable
	one.Judge = config.JudgePD

	solutionModle := model.SolutionModel{}
	sid, err := solutionModle.Insert(one)
	if err != nil {
		pc.Error(err.Error(), 500)
		return
	}

	w.WriteHeader(201)

	go func() {
		one := make(map[string]interface{})
		one["Sid"] = sid
		one["Pid"] = pro.RPid
		one["OJ"] = pro.ROJ
		one["Rejudge"] = false
		reader, _ := pc.JsonReader(&one)
		restweb.Logger.Debug(reader)
		_, err := http.Post(config.JudgeHost, "application/json", reader)
		if err != nil {
			restweb.Logger.Debug("sid[", sid, "] submit post error")
		}
	}()
}
Esempio n. 22
0
//提交某一问题的solution
//@URL: /problems/(\d+) @method: POST
func (pc *ProblemController) Submit(Pid string) {
	restweb.Logger.Debug("Problem Submit")

	pid, err := strconv.Atoi(Pid)
	if err != nil {
		pc.Error("args error", 400)
		return
	}

	var one model.Solution
	one.Pid = pid
	one.Uid = pc.Uid
	one.Module = config.ModuleP
	one.Mid = config.ModuleP // Todo use pid as mid

	problemModel := model.ProblemModel{}
	pro, err := problemModel.Detail(pid)
	if err != nil {
		pc.Error(err.Error(), 500)
		return
	}
	code := pc.Input.Get("code")

	one.Code = code
	one.Length = pc.GetCodeLen(len(pc.Input.Get("code")))
	one.Language, _ = strconv.Atoi(pc.Input.Get("compiler_id"))
	pc.SetSession("Compiler_id", pc.Input.Get("compiler_id")) //or set cookie?
	userModel := model.UserModel{}
	user, _ := userModel.Detail(pc.Uid)
	one.Share = user.ShareCode

	hint := make(map[string]string)
	errflag := true
	switch {
	case pro.Pid == 0:
		hint["info"] = "No such problem."
	case code == "":
		hint["info"] = "Your source code is too short."
	default:
		errflag = false
	}
	if errflag {
		b, _ := json.Marshal(&hint)
		pc.W.WriteHeader(400)
		pc.W.Write(b)
		return
	}

	one.Status = config.StatusAvailable
	one.Judge = config.JudgePD

	solutionModel := model.SolutionModel{}
	sid, err := solutionModel.Insert(one)
	if err != nil {
		pc.Error(err.Error(), 500)
		return
	}

	pc.W.WriteHeader(201)
	go func() { //编译运行solution
		one := make(map[string]interface{})
		one["Sid"] = sid
		one["Pid"] = pro.RPid
		one["OJ"] = pro.ROJ
		one["Rejudge"] = false
		reader, _ := pc.JsonReader(&one)
		restweb.Logger.Debug(reader)
		_, err := http.Post(config.JudgeHost, "application/json", reader)
		if err != nil {
			restweb.Logger.Debug("sid[", sid, "] submit post error")
		}
	}()
}