Esempio n. 1
0
func ServeProblemTestAnswer(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	prob, err := data.GetProblem(id)
	catch(err)

	no, err := strconv.ParseInt(vars["no"], 10, 64)
	catch(err)
	test := prob.Tests[no-1]

	w.Header().Add("Content-Type", "text/plain")

	if r.FormValue("download") == "yes" {
		w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s-%d.ans"`, prob.Slug, no))
	}

	blob, err := data.Blobs.Get(test.AnswerKey)
	catch(err)
	_, err = io.Copy(w, blob)
	catch(err)
	err = blob.Close()
	catch(err)
}
Esempio n. 2
0
func ServeSubmissionTestOutput(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	subm, err := data.GetSubmission(id)
	catch(err)

	no, err := strconv.ParseInt(vars["no"], 10, 64)
	catch(err)
	test := subm.Tests[no-1]

	w.Header().Add("Content-Type", "text/plain")

	if r.FormValue("download") == "yes" {
		acc, err := data.GetAccount(subm.AuthorId)
		catch(err)

		prob, err := data.GetProblem(subm.ProblemId)
		catch(err)

		w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%d-%s-%s-%d.out"`, subm.Id, acc.Handle, strings.ToLower(prob.Char), no))
	}

	blob, err := data.Blobs.Get(test.OutputKey)
	catch(err)
	_, err = io.Copy(w, blob)
	catch(err)
	err = blob.Close()
	catch(err)
}
Esempio n. 3
0
func DeleteProblem(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil || me.Level != data.Administrator {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	prob, err := data.GetProblem(id)
	catch(err)

	err = prob.Del()
	catch(err)

	err = json.NewEncoder(w).Encode(&struct {
		Id int64 `json:"id"`
	}{
		Id: prob.Id,
	})
	catch(err)
	hub.Send([]interface{}{"SYNC", "problems"})

	err = data.NewActivity(me, fmt.Sprintf("deleted problem %d", prob.Id)).Put()
	catch(err)
	hub.Send([]interface{}{"SYNC", "activities"})
}
Esempio n. 4
0
func UpdateClarification(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil || (me.Level != data.Judge && me.Level != data.Administrator) {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	clar, err := data.GetClarification(id)
	catch(err)

	body := struct {
		ProblemId int64         `json:"problemId"`
		Question  string        `json:"question"`
		Response  data.Response `json:"response"`
		Message   string        `json:"message"`
	}{}
	err = json.NewDecoder(r.Body).Decode(&body)
	catch(err)

	prob, err := data.GetProblem(body.ProblemId)
	catch(err)
	if prob != nil {
		clar.ProblemId = prob.Id
	} else {
		clar.ProblemId = 0
	}

	clar.Question = body.Question
	clar.Response = body.Response
	clar.Message = body.Message

	err = clar.Put()
	catch(err)

	err = json.NewEncoder(w).Encode(clar)
	catch(err)
	hub.Send([]interface{}{"SYNC", "clarifications", clar.Id})

	err = data.NewActivity(me, fmt.Sprintf("updated clarification %d", clar.Id)).Put()
	catch(err)
	hub.Send([]interface{}{"SYNC", "activities"})

	if clar.Response != data.Unresponded {
		if clar.Response == data.Broadcasted {
			err = data.NewNotification(0, data.Participant, fmt.Sprintf("Clarification %d updated", clar.Id)).Put()
			catch(err)
		} else {
			err = data.NewNotification(clar.AskerId, 0, fmt.Sprintf("Clarification %d updated", clar.Id)).Put()
			catch(err)
		}
		hub.Send([]interface{}{"SYNC", "notifications"})
	}
}
Esempio n. 5
0
func CreateSubmission(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	body := struct {
		ProblemId int64  `json:"problemId"`
		Language  string `json:"language"`
		Source    string `json:"source"`
	}{}
	err := json.NewDecoder(r.Body).Decode(&body)
	catch(err)

	prob, err := data.GetProblem(body.ProblemId)
	catch(err)

	key, err := data.Blobs.Put("", strings.NewReader(body.Source))
	catch(err)

	subm := &data.Submission{
		AuthorId:  me.Id,
		ProblemId: prob.Id,
		Language:  body.Language,
		SourceKey: string(key),
	}
	err = subm.Put()
	catch(err)

	err = json.NewEncoder(w).Encode(subm)
	catch(err)
	hub.Send([]interface{}{"SYNC", "submissions"})

	err = data.NewActivity(me, fmt.Sprintf("created submission %d", subm.Id)).Put()
	catch(err)
	hub.Send([]interface{}{"SYNC", "activities"})

	if prob.Judge == "automatic" {
		exec := &data.Execution{
			SubmissionId: subm.Id,
			Apply:        true,
		}
		err = exec.Put()
		catch(err)
		belt.Push(exec)
	}
}
Esempio n. 6
0
func ServeProblem(w http.ResponseWriter, r *http.Request) {
	cnt, err := data.GetContest()
	catch(err)

	me, _ := context.Get(r, "me").(*data.Account)
	if !cnt.Started() && (me == nil || (me.Level != data.Judge && me.Level != data.Administrator)) {
		http.Error(w, "", http.StatusNotFound)
		return
	}

	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	prob, err := data.GetProblem(id)
	catch(err)

	err = json.NewEncoder(w).Encode(prob)
	catch(err)
}
Esempio n. 7
0
func CreateClarification(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)

	body := struct {
		ProblemId int64  `json:"problemId"`
		Question  string `json:"question"`
	}{}
	err := json.NewDecoder(r.Body).Decode(&body)
	catch(err)

	clar := &data.Clarification{
		AskerId:  me.Id,
		Question: body.Question,
	}

	prob, err := data.GetProblem(body.ProblemId)
	catch(err)
	if prob != nil {
		clar.ProblemId = prob.Id
	}

	err = clar.Put()
	catch(err)

	err = json.NewEncoder(w).Encode(clar)
	catch(err)
	hub.Send([]interface{}{"SYNC", "clarifications"})

	err = data.NewActivity(me, fmt.Sprintf("created clarification %d", clar.Id)).Put()
	catch(err)
	hub.Send([]interface{}{"SYNC", "activities"})

	err = data.NewNotification(0, data.Judge, fmt.Sprintf("Clarification %d requested", clar.Id)).Put()
	catch(err)
	err = data.NewNotification(0, data.Administrator, fmt.Sprintf("Clarification %d requested", clar.Id)).Put()
	catch(err)
	hub.Send([]interface{}{"SYNC", "notifications"})
}
Esempio n. 8
0
func ServeExecutionTestOutput(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil || (me.Level != data.Judge && me.Level != data.Administrator) {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	exec, err := data.GetExecution(id)
	catch(err)

	no, err := strconv.ParseInt(vars["no"], 10, 64)
	catch(err)
	test := exec.Tests[no-1]

	w.Header().Add("Content-Type", "text/plain")

	if r.FormValue("download") == "yes" {
		subm, err := exec.Submission()
		catch(err)
		acc, err := data.GetAccount(subm.AuthorId)
		catch(err)
		prob, err := data.GetProblem(subm.ProblemId)
		catch(err)
		w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%d-%s-%s-%d-%d.out"`, subm.Id, acc.Handle, strings.ToLower(prob.Char), exec.Id, no))
	}

	blob, err := data.Blobs.Get(test.OutputKey)
	catch(err)
	_, err = io.Copy(w, blob)
	catch(err)
	err = blob.Close()
	catch(err)
}
Esempio n. 9
0
func UpdateProblem(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil || me.Level != data.Administrator {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	prob, err := data.GetProblem(id)
	catch(err)

	if prob == nil {
		http.Error(w, "", http.StatusNotFound)
	}

	body := struct {
		Char      string `json:"char"`
		Title     string `json:"title"`
		Statement struct {
			Body   string `json:"body"`
			Input  string `json:"input"`
			Output string `json:"output"`
		} `json:"statement"`
		Samples []struct {
			Input  string `json:"input"`
			Answer string `json:"answer"`
		} `json:"samples"`
		Notes   string `json:"notes"`
		Judge   string `json:"judge"`
		Checker struct {
			Language  string `json:"language"`
			Source    string `json:"source"`
			SourceKey string `json:"sourceKey"`
		} `json:"checker"`
		Limits struct {
			Cpu    float64 `json:"cpu"`
			Memory int     `json:"memory"`
		} `json:"limits"`
		Languages []string `json:"languages"`
		Tests     []struct {
			Input     string `json:"input"`
			InputKey  string `json:"inputKey"`
			Answer    string `json:"answer"`
			AnswerKey string `json:"answerKey"`
			Points    int    `json:"points"`
		} `json:"tests"`
		Scoring string `json:"scoring"`
	}{}
	err = json.NewDecoder(r.Body).Decode(&body)
	catch(err)

	switch {
	case len(body.Char) != 1 || !strings.Contains("abcdefghijklmnopqrstuvwxyz", body.Char):
		http.Error(w, "", http.StatusBadRequest)
		return

	case body.Title == "":
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	prob.Slug = strings.Trim(regexp.MustCompile("[^a-z0-9]+").ReplaceAllString(strings.ToLower(body.Char+" "+body.Title), "-"), " -")
	prob.Char = body.Char
	prob.Title = body.Title
	prob.Statement = body.Statement
	prob.Samples = body.Samples
	prob.Notes = body.Notes
	prob.Judge = body.Judge
	if body.Checker.Language != "" {
		if body.Checker.SourceKey == "" {
			body.Checker.SourceKey = fmt.Sprintf("problems:%d:checker:source", prob.Id)
			_, err := data.Blobs.Put(body.Checker.SourceKey, strings.NewReader(body.Checker.Source))
			catch(err)
		}
		prob.Checker = struct {
			Language  string `json:"language"`
			SourceKey string `json:"sourceKey"`
		}{
			Language:  body.Checker.Language,
			SourceKey: body.Checker.SourceKey,
		}

	} else {
		prob.Checker = struct {
			Language  string `json:"language"`
			SourceKey string `json:"sourceKey"`
		}{
			Language:  "",
			SourceKey: "",
		}
	}
	prob.Limits = body.Limits
	prob.Languages = body.Languages
	prob.Tests = nil
	for i, test := range body.Tests {
		if test.InputKey == "" {
			test.InputKey = fmt.Sprintf("problems:%d:tests:%d:in", prob.Id, i)
			_, err := data.Blobs.Put(test.InputKey, strings.NewReader(test.Input))
			catch(err)
		}

		if test.AnswerKey == "" {
			test.AnswerKey = fmt.Sprintf("problems:%d:tests:%d:ans", prob.Id, i)
			_, err := data.Blobs.Put(test.AnswerKey, strings.NewReader(test.Answer))
			catch(err)
		}

		prob.Tests = append(prob.Tests, struct {
			InputKey  string `json:"inputKey"`
			AnswerKey string `json:"answerKey"`
			Points    int    `json:"points"`
		}{
			InputKey:  test.InputKey,
			AnswerKey: test.AnswerKey,
			Points:    test.Points,
		})
	}
	prob.Scoring = body.Scoring
	err = prob.Put()
	catch(err)

	err = json.NewEncoder(w).Encode(prob)
	catch(err)
	hub.Send([]interface{}{"SYNC", "problems", prob.Id})

	err = data.NewActivity(me, fmt.Sprintf("updated problem %d", prob.Id)).Put()
	catch(err)
	hub.Send([]interface{}{"SYNC", "activities"})
}