Exemple #1
0
func (h *PKUJudger) SetDetail(pid string, html string) error {
	log.Println(pid)
	pro := model.Problem{}
	pro.RPid, _ = strconv.Atoi(pid)
	pro.ROJ = "PKU"
	pro.Status = StatusAvailable

	titleMatch := h.titleRx.FindStringSubmatch(html)
	if len(titleMatch) < 1 {
		log.Println(titleMatch)
		return ErrMatchFailed
	}
	pro.Title = titleMatch[1]

	if strings.Index(html, "Special Judge") >= 0 {
		pro.Special = 1
	}

	resMatch := h.resLimtRx.FindStringSubmatch(html)
	if len(resMatch) < 3 {
		log.Println(resMatch)
		return ErrMatchFailed
	}
	pro.Time, _ = strconv.Atoi(resMatch[1])
	pro.Time /= 1000 //ms -> s
	pro.Memory, _ = strconv.Atoi(resMatch[2])

	cxtMatch := h.ctxRx.FindAllStringSubmatch(html, 3)
	if len(cxtMatch) < 3 {
		log.Println("ctx match error, PKU pid is", pid)
		return ErrMatchFailed
	}
	pro.Description = template.HTML(h.ReplaceImg(cxtMatch[0][1]))
	pro.Input = template.HTML(h.ReplaceImg(cxtMatch[1][1]))
	pro.Output = template.HTML(h.ReplaceImg(cxtMatch[2][1]))

	test := h.testRx.FindAllStringSubmatch(html, 2)
	if len(test) < 2 {
		log.Println("test data error, PKU pid is", pid)
		return ErrMatchFailed
	}
	pro.In = test[0][1]
	pro.Out = test[1][1]

	src := h.srcRx.FindStringSubmatch(html)
	if len(src) > 1 {
		pro.Source = src[1]
	}

	hint := h.hintRx.FindStringSubmatch(html)
	if len(hint) > 1 {
		pro.Hint = template.HTML(hint[1])
	}

	proModel := &model.ProblemModel{}
	proModel.Insert(pro)
	return nil
}
Exemple #2
0
func (this *ProblemController) Insert(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("Admin Problem Insert")
	if r.Method != "POST" {
		this.Err400(w, r, "Error", "Error Method to Insert problem")
		return
	}

	this.Init(w, r)

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

	one := model.Problem{}
	one.Title = r.FormValue("title")
	time, err := strconv.Atoi(r.FormValue("time"))
	if err != nil {
		http.Error(w, "The value 'Time' is neither too short nor too large", 400)
		return
	}
	one.Time = time
	memory, err := strconv.Atoi(r.FormValue("memory"))
	if err != nil {
		http.Error(w, "The value 'Memory' is neither too short nor too large", 400)
		return
	}
	one.Memory = memory
	if special := r.FormValue("special"); special == "" {
		one.Special = 0
	} else {
		one.Special = 1
	}

	in := r.FormValue("in")
	out := r.FormValue("out")
	one.Description = template.HTML(r.FormValue("description"))
	one.Input = template.HTML(r.FormValue("input"))
	one.Output = template.HTML(r.FormValue("output"))
	one.In = in
	one.Out = out
	one.Source = r.FormValue("source")
	one.Hint = r.FormValue("hint")

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

	createfile(config.Datapath+strconv.Itoa(pid), "sample.in", in)
	createfile(config.Datapath+strconv.Itoa(pid), "sample.out", out)

	http.Redirect(w, r, "/admin/problem?list", http.StatusFound)
}