Beispiel #1
0
//列出某问题的详细信息
//@URL: /problems/(\d+) @method: GET
func (pc *ProblemController) Detail(Pid string) {
	restweb.Logger.Debug("Problem Detail")

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

	problemModel := model.ProblemModel{}
	one, err := problemModel.Detail(pid)
	if err != nil {
		pc.Err400("Problem "+Pid, "No such problem")
		return
	}
	pc.Output["Detail"] = one

	if pc.Privilege <= config.PrivilegePU && one.Status == config.StatusReverse { // 如果问题状态为普通用户不可见
		pc.Err400("Problem "+Pid, "No such problem")
		return
	}

	pc.Output["Compiler_id"] = pc.GetSession("Compiler_id")
	pc.Output["Privilege"] = pc.Privilege
	pc.Output["Title"] = "Problem — " + Pid
	pc.RenderTemplate("view/layout.tpl", "view/problem_detail.tpl")
}
Beispiel #2
0
func (this *ProblemController) Edit(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Admin Problem Edit")
	this.Init(w, r)

	if this.Privilege != config.PrivilegeAD {
		this.Err400(w, r, "Warning", "Error Privilege to Edit problem")
		return
	}
	args := this.ParseURL(r.URL.String())
	pid, err := strconv.Atoi(args["pid"])
	if err != nil {
		http.Error(w, "args error", 400)
		return
	}

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

	this.Data["Detail"] = one
	this.Data["Title"] = "Admin - Problem Edit"
	this.Data["IsProblem"] = true
	this.Data["IsList"] = false
	this.Data["IsEdit"] = true

	err = this.Execute(w, "view/admin/layout.tpl", "view/admin/problem_edit.tpl")
	if err != nil {
		http.Error(w, "tpl error", 500)
		return
	}
}
Beispiel #3
0
func (this *ProblemController) Detail(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Contest Problem Detail")

	args := this.ParseURL(r.URL.String())
	pid, err := strconv.Atoi(args["pid"])
	if err != nil {
		http.Error(w, "args error", 400)
		return
	}
	problemModel := model.ProblemModel{}
	one, err := problemModel.Detail(this.ContestDetail.List[pid])
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	this.Data["Detail"] = one
	this.Data["Pid"] = pid
	this.Data["Status"] = this.ContestDetail.Status

	err = this.Execute(w, "view/layout.tpl", "view/contest/problem_detail.tpl")
	if err != nil {
		http.Error(w, "tpl error", 500)
		return
	}
}
Beispiel #4
0
//@URL: /admin/problems/(\d+)/status/ @method: POST
func (pc *AdminProblem) Status(Pid string) {
	restweb.Logger.Debug("Admin Problem Status")

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

	problemModel := model.ProblemModel{}
	one, err := problemModel.Detail(pid)
	if err != nil {
		pc.Error(err.Error(), 400)
		return
	}
	pc.Output["Detail"] = one
	var status int
	switch one.Status {
	case config.StatusAvailable:
		status = config.StatusReverse
	case config.StatusReverse:
		status = config.StatusAvailable
	}
	err = problemModel.Status(pid, status)
	if err != nil {
		pc.Error(err.Error(), 500)
		return
	}

	pc.Redirect("/admin/problems", http.StatusFound)
}
Beispiel #5
0
//@URL: /admin/problems/(\d+)/ @method: GET
func (pc *AdminProblem) Edit(Pid string) {
	restweb.Logger.Debug("Admin Problem Edit")

	if pc.Privilege != config.PrivilegeAD {
		pc.Err400("Warning", "Error Privilege to Edit problem")
		return
	}

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

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

	pc.Output["Detail"] = one
	pc.Output["Title"] = "Admin - Problem Edit"
	pc.Output["IsProblem"] = true
	pc.Output["IsList"] = false
	pc.Output["IsEdit"] = true

	pc.RenderTemplate("view/admin/layout.tpl", "view/admin/problem_edit.tpl")
}
Beispiel #6
0
//列出某问题的详细信息,URL,/probliem?detail/pid?<pid>
func (this *ProblemController) Detail(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Problem Detail")
	this.Init(w, r)

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

	problemModel := model.ProblemModel{}
	one, err := problemModel.Detail(pid)
	if err != nil {
		t := template.New("layout.tpl")
		t, err = t.ParseFiles("view/layout.tpl", "view/400.tpl")
		if err != nil {
			class.Logger.Debug(err)
			http.Error(w, "tpl error", 500)
			return
		}

		this.Data["Info"] = "No such problem"
		this.Data["Title"] = "No such problem"
		err = t.Execute(w, this.Data)
		if err != nil {
			http.Error(w, "tpl error", 500)
			return
		}
		return
	}
	this.Data["Detail"] = one

	if this.Privilege <= config.PrivilegePU && one.Status == config.StatusReverse { // 如果问题状态为普通用户不可见
		t := template.New("layout.tpl")
		t, err = t.ParseFiles("view/layout.tpl", "view/400.tpl")
		if err != nil {
			class.Logger.Debug(err)
			http.Error(w, "tpl error", 500)
			return
		}

		this.Data["Info"] = "No such problem"
		this.Data["Title"] = "No such problem"
		err = t.Execute(w, this.Data)
		if err != nil {
			http.Error(w, "tpl error", 500)
			return
		}
		return
	}

	this.Data["Privilege"] = this.Privilege
	this.Data["Title"] = "Problem — " + strconv.Itoa(pid)
	err = this.Execute(w, "view/layout.tpl", "view/problem_detail.tpl")
	if err != nil {
		http.Error(w, "tpl error", 500)
		return
	}
}
Beispiel #7
0
func (this *ProblemController) Detail(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Admin Problem Detail")
	this.Init(w, r)

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

	problemModel := model.ProblemModel{}
	one, err := problemModel.Detail(pid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	this.Data["Detail"] = one
	this.Data["Title"] = "Admin - Problem Detail"
	this.Data["IsProblem"] = true
	this.Data["IsList"] = false

	err = this.Execute(w, "view/admin/layout.tpl", "view/problem_detail.tpl")
	if err != nil {
		http.Error(w, "tpl error", 500)
		return
	}
}
Beispiel #8
0
func (this *ProblemController) Status(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Admin Problem Status")
	if r.Method != "POST" {
		this.Err400(w, r, "Error", "Error Method to Change problem status")
		return
	}

	this.Init(w, r)

	if this.Privilege != config.PrivilegeAD {
		this.Err400(w, r, "Warning", "Error Privilege to Change problem status")
		return
	}

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

	problemModel := model.ProblemModel{}
	one, err := problemModel.Detail(pid)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	this.Data["Detail"] = one
	var status int
	switch one.Status {
	case config.StatusAvailable:
		status = config.StatusReverse
	case config.StatusReverse:
		status = config.StatusAvailable
	}
	err = problemModel.Status(pid, status)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	http.Redirect(w, r, "/admin/problem?list", http.StatusFound)
}
Beispiel #9
0
func (this *ProblemController) List(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Contest Problem List")

	list := make([]*model.Problem, len(this.ContestDetail.List))
	for k, v := range this.ContestDetail.List {
		problemModel := model.ProblemModel{}
		one, err := problemModel.Detail(v)
		if err != nil {
			http.Error(w, err.Error(), 400)
			return
		}
		one.Pid = k
		qry := make(map[string]string)
		qry["pid"] = strconv.Itoa(v)
		qry["action"] = "accept"
		one.Solve, err = this.GetCount(qry)
		if err != nil {
			http.Error(w, "count error", 500)
			return
		}
		qry["action"] = "submit"
		one.Submit, err = this.GetCount(qry)
		if err != nil {
			http.Error(w, "count error", 500)
			return
		}

		list[k] = one
	}

	this.Data["Problem"] = list
	this.Data["IsContestProblem"] = true
	this.Data["Start"] = this.ContestDetail.Start
	this.Data["End"] = this.ContestDetail.End
	this.Data["Status"] = this.ContestDetail.Status

	err := this.Execute(w, "view/layout.tpl", "view/contest/problem_list.tpl")
	if err != nil {
		class.Logger.Debug(err)
		http.Error(w, "tpl error", 500)
		return
	}
}
Beispiel #10
0
//@URL: /contests/(\d+)/problems/(\d+) @method:GET
func (pc *ContestProblem) Detail(Cid, Pid string) {
	pc.InitContest(Cid)
	restweb.Logger.Debug("Contest Problem Detail")

	pid, err := strconv.Atoi(Pid)
	if err != nil {
		pc.Error("args error", 400)
		return
	}
	rpid := pc.ContestDetail.List[pid]
	problemModel := model.ProblemModel{}
	one, err := problemModel.Detail(rpid)
	if err != nil {
		pc.Error(err.Error(), 500)
		return
	}
	qry := make(map[string]string)
	qry["pid"] = strconv.Itoa(rpid)
	qry["action"] = "accept"
	one.Solve, err = pc.GetCount(qry)
	if err != nil {
		pc.Error(err.Error(), 500)
		return
	}
	qry["action"] = "submit"
	one.Submit, err = pc.GetCount(qry)
	if err != nil {
		pc.Error(err.Error(), 500)
		return
	}

	pc.Output["Compiler_id"] = pc.GetSession("Compiler_id")
	pc.Output["Detail"] = one
	pc.Output["Pid"] = pid
	pc.Output["Status"] = pc.ContestDetail.Status

	pc.RenderTemplate("view/layout.tpl", "view/contest/problem_detail.tpl")
}
Beispiel #11
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)
		}
	}()
}
Beispiel #12
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)
		}
	}()
}
Beispiel #13
0
//@URL: /contests/(\d+) @method: GET
func (c *Contest) Detail(Cid string) {
	restweb.Logger.Debug("Contest Problem List")

	c.InitContest(Cid)
	list := make([]*model.Problem, len(c.ContestDetail.List))
	idx := 0
	solutionModel := &model.SolutionModel{}
	achieve, _ := solutionModel.Achieve(c.Uid, config.ModuleC, c.Cid)

	for _, v := range c.ContestDetail.List {
		problemModel := model.ProblemModel{}
		one, err := problemModel.Detail(v)
		if err != nil {
			restweb.Logger.Debug(err)
			continue
		}

		qry := make(map[string]string)
		qry["pid"] = strconv.Itoa(v)
		qry["module"] = strconv.Itoa(config.ModuleC)
		qry["action"] = "accept"
		one.Solve, err = c.GetCount(qry)
		if err != nil {
			restweb.Logger.Debug(err)
			continue
		}
		qry["action"] = "submit"
		one.Submit, err = c.GetCount(qry)
		if err != nil {
			restweb.Logger.Debug(err)
			continue
		}

		one.Flag = config.FlagNA
		for _, i := range achieve {
			if one.Pid == i {
				one.Flag = config.FLagAC
				break
			}
		}

		if one.Flag == config.FlagNA {
			args := make(map[string]string)
			args["pid"] = strconv.Itoa(one.Pid)
			args["module"] = strconv.Itoa(config.ModuleC)
			args["mid"] = strconv.Itoa(c.Cid)
			args["uid"] = c.Uid
			l, _ := solutionModel.List(args)
			if len(l) > 0 {
				one.Flag = config.FLagER
			}
		}

		one.Pid = idx
		list[idx] = one
		idx++
	}

	c.Output["Problem"] = list
	c.Output["IsContestProblem"] = true
	c.Output["Start"] = c.ContestDetail.Start
	c.Output["End"] = c.ContestDetail.End
	c.Output["Time"] = time.Now().Unix()
	c.RenderTemplate("view/layout.tpl", "view/contest/problem_list.tpl")
}
Beispiel #14
0
func (cc *AdminContest) contest() (one model.Contest) {

	one.Title = cc.Input.Get("title")
	year, _ := strconv.Atoi(cc.Input.Get("startTimeYear"))
	month, _ := strconv.Atoi(cc.Input.Get("startTimeMonth"))
	day, _ := strconv.Atoi(cc.Input.Get("startTimeDay"))
	hour, _ := strconv.Atoi(cc.Input.Get("startTimeHour"))
	min, _ := strconv.Atoi(cc.Input.Get("startTimeMinute"))
	start := time.Date(year, time.Month(month), day, hour, min, 0, 0, time.Local)
	one.Start = start.Unix()

	year, _ = strconv.Atoi(cc.Input.Get("endTimeYear"))
	month, _ = strconv.Atoi(cc.Input.Get("endTimeMonth"))
	day, _ = strconv.Atoi(cc.Input.Get("endTimeDay"))
	hour, _ = strconv.Atoi(cc.Input.Get("endTimeHour"))
	min, _ = strconv.Atoi(cc.Input.Get("endTimeMinute"))
	end := time.Date(year, time.Month(month), day, hour, min, 0, 0, time.Local)
	one.End = end.Unix()

	if start.After(end) {
		cc.Error("args error", 400)
		return
	}

	switch cc.Input.Get("type") {
	case "public":
		one.Encrypt = config.EncryptPB
	case "private": //TODO 设置argument为一个string数组
		one.Encrypt = config.EncryptPT
		argument := cc.Input.Get("userlist")
		var cr rune = 13
		crStr := string(cr)
		argument = strings.Trim(argument, crStr)
		argument = strings.Trim(argument, "\r\n")
		one.Argument = argument
		restweb.Logger.Debug(one.Argument)
	case "password":
		one.Encrypt = config.EncryptPW
		one.Argument = cc.Input.Get("password")
	default:
		cc.Error("args error", 400)
		return
	}

	problemString := cc.Input.Get("problemList")
	problemString = strings.Trim(problemString, " ")
	problemString = strings.Trim(problemString, ";")
	problemList := strings.Split(problemString, ";")
	var list []int
	for _, v := range problemList {
		pid, err := strconv.Atoi(v)
		if err != nil {
			restweb.Logger.Debug(err)
			continue
		}
		problemModel := model.ProblemModel{}
		_, err = problemModel.Detail(pid) //检查题目是否存在
		if err != nil {
			restweb.Logger.Debug(err)
			continue
		}
		list = append(list, pid)
	}
	one.List = list
	one.Creator = cc.Uid
	restweb.Logger.Debug(one.Creator)
	return one
}
Beispiel #15
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)
			}
		}()
	}
}
Beispiel #16
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")
		}
	}()
}
Beispiel #17
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")
		}
	}()
}