Example #1
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)
		}
	}()
}
Example #2
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")
		}
	}()
}
Example #3
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")
		}
	}()
}
Example #4
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)
		}
	}()
}