Ejemplo n.º 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
}
Ejemplo n.º 2
0
//@URL: /admin/problems/importor/ @method: POST
func (pc *AdminProblem) Import() {
	pc.R.ParseMultipartForm(32 << 20)
	fhs := pc.R.MultipartForm.File["fps.xml"]
	file, err := fhs[0].Open()
	if err != nil {
		restweb.Logger.Debug(err)
		return
	}
	defer file.Close()

	content, err := ioutil.ReadAll(file)
	if err != nil {
		restweb.Logger.Debug(err)
		return
	}
	contentStr := string(content)

	problem := model.Problem{}
	protype := reflect.TypeOf(problem)
	proValue := reflect.ValueOf(&problem).Elem()
	restweb.Logger.Debug(protype.NumField())
	for i, lenth := 0, protype.NumField(); i < lenth; i++ {
		tag := protype.Field(i).Tag.Get("xml")
		restweb.Logger.Debug(i, tag)
		if tag == "" {
			continue
		}
		matchStr := "<" + tag + `><!\[CDATA\[(?ms:(.*?))\]\]></` + tag + ">"
		tagRx := regexp.MustCompile(matchStr)
		tagString := tagRx.FindAllStringSubmatch(contentStr, -1)
		restweb.Logger.Debug(tag)
		if len(tagString) > 0 {
			switch tag {
			case "time_limit", "memory_limit":
				limit, err := strconv.Atoi(tagString[0][1])
				if err != nil {
					restweb.Logger.Debug(err)
					limit = 1
				}
				proValue.Field(i).Set(reflect.ValueOf(limit))
			case "description", "input", "output":
				proValue.Field(i).SetString(tagString[0][1])
			default:
				proValue.Field(i).SetString(tagString[0][1])
			}
		}
	}
	problem.ROJ = "ZJGSU"
	proModel := model.ProblemModel{}
	pid, err := proModel.Insert(problem)
	if err != nil {
		restweb.Logger.Debug(err)
		return
	}

	// 建立测试数据文件
	createfile(config.Datapath+strconv.Itoa(pid), "sample.in", problem.In)
	createfile(config.Datapath+strconv.Itoa(pid), "sample.out", problem.Out)

	flag, flagJ := true, -1
	for _, tag := range []string{"test_input", "test_output"} {
		// restweb.Logger.Debug(tag)
		matchStr := "<" + tag + `><!\[CDATA\[(?ms:(.*?))\]\]></` + tag + ">"
		tagRx := regexp.MustCompile(matchStr)
		tagString := tagRx.FindAllStringSubmatch(contentStr, -1)
		// restweb.Logger.Debug(tagString)
		if flag {
			flag = false
			caselenth := 0
			for matchLen, j := len(tagString), 0; j < matchLen; j++ {
				if len(tagString[j][1]) > caselenth {
					caselenth = len(tagString[j][1])
					flagJ = j
				}
			}
		}
		if flagJ >= 0 && flagJ < len(tagString) {
			// restweb.Logger.Debug(tagString[flagJ][1])
			filename := strings.Replace(tag, "_", ".", 1)
			filename = strings.Replace(filename, "put", "", -1)
			createfile(config.Datapath+strconv.Itoa(pid), filename, tagString[flagJ][1])
		}
	}

	pc.Redirect("/admin/problems", http.StatusFound)
}
Ejemplo n.º 3
0
func (h *VJJudger) SetDetail(pid string, html string) error {
	log.Println(pid)
	pro := model.Problem{}
	pro.RPid, _ = strconv.Atoi(pid)
	pro.ROJ = "VJ"
	pro.Status = StatusReverse

	titleMatch := h.titleRx.FindStringSubmatch(html)
	if len(titleMatch) != 2 {
		log.Println(titleMatch)
		return ErrMatchFailed
	}
	pro.Title = titleMatch[1]
	//	fmt.Println("Title",pro.Title)

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

	TimeMatch := h.TimeRx.FindStringSubmatch(html)
	if len(TimeMatch) != 2 {
		log.Println(TimeMatch)
		return ErrMatchFailed
	}
	pro.Time, _ = strconv.Atoi(TimeMatch[1])
	//	fmt.Println("Time",pro.Time)

	MemoryMatch := h.MemoryRx.FindStringSubmatch(html)
	if len(MemoryMatch) != 2 {
		log.Println(MemoryMatch)
		return ErrMatchFailed
	}
	pro.Memory, _ = strconv.Atoi(MemoryMatch[1])
	//	fmt.Println("Memory",pro.Memory)

	DescriptionMatch := h.DescriptionRx.FindStringSubmatch(html)
	if len(DescriptionMatch) != 2 {
		log.Println(DescriptionMatch)
		return ErrMatchFailed
	}
	pro.Description = template.HTML(h.ReplaceHtml(DescriptionMatch[1]))
	InputMatch := h.InputRx.FindStringSubmatch(html)
	if len(InputMatch) != 2 {
		log.Println(InputMatch)
		return ErrMatchFailed
	}
	pro.Input = template.HTML(h.ReplaceHtml(InputMatch[1]))
	//	fmt.Println("Input",pro.Input)
	OutputMatch := h.OutputRx.FindStringSubmatch(html)
	if len(OutputMatch) != 2 {
		log.Println(OutputMatch)
		return ErrMatchFailed
	}
	pro.Output = template.HTML(h.ReplaceHtml(OutputMatch[1]))
	//	fmt.Println("Output",pro.Output)

	testIn := h.testInRx.FindStringSubmatch(html)
	if len(testIn) != 2 {
		log.Println(testIn)
		return ErrMatchFailed
	}
	pro.In = h.ReplaceHtml(testIn[1])
	//	fmt.Println("In",pro.In)
	testOut := h.testOutRx.FindStringSubmatch(html)
	if len(testOut) != 2 {
		log.Println(testOut)
		return ErrMatchFailed
	}
	pro.Out = h.ReplaceHtml(testOut[1])
	//	fmt.Println("Out",pro.Out)

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

	hint := h.hintRx.FindStringSubmatch(html)
	if len(hint) != 2 {
		log.Println(hint)
		return ErrMatchFailed
	}
	pro.Hint = template.HTML(h.ReplaceHtml(hint[1]))

	proModel := &model.ProblemModel{}
	proModel.Insert(pro)
	return nil
}