Exemplo n.º 1
0
func register(w http.ResponseWriter, r *http.Request, s *ServerState) {
	var form struct {
		Name      string                        `json:"name" form:"name" validate:"required"`
		PublicKey string                        `json:"public_key" form:"public_key" validate:"required"`
		Category  tournament.TournamentCategory `json:"category" form:"category" validate:"required"`
	}
	if err := parseForm(r, &form); err != nil {
		web.WriteJsonWebError(w, err)
	} else if !NameRegex.MatchString(form.Name) {
		web.WriteJsonError(w, errors.New("Invalid Name"))
	} else if match := git.PublicKeyRegex.FindStringSubmatch(strings.TrimSpace(form.PublicKey)); match == nil {
		web.WriteJsonError(w, errors.New("Invalid Public Key"))
	} else if commitHash, err := s.Tournament.CreateUser(form.Name, match[1], form.Category); err != nil {
		web.WriteJsonError(w, err)
	} else if err := s.Tournament.SubmitCommit(form.Name, form.Category, commitHash, time.Now()); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteJson(w, struct {
			Name      string                        `json:"name"`
			Category  tournament.TournamentCategory `json:"category"`
			PublicKey string                        `json:"public_key"`
			RepoUrl   string                        `json:"repo_url"`
			Commit    string                        `json:"commit_hash"`
		}{
			form.Name,
			form.Category,
			form.PublicKey,
			s.Tournament.GitHost.ExternalRepositoryURL(form.Name),
			commitHash,
		})
	}
}
Exemplo n.º 2
0
func runMatch(w http.ResponseWriter, r *http.Request, s *ServerState) {
	var form struct {
		Player1  string                        `json:"player1" form:"player1" validate:"required"`
		Player2  string                        `json:"player2" form:"player2" validate:"required"`
		Commit1  string                        `json:"commit1" form:"commit1" validate:"required"`
		Commit2  string                        `json:"commit2" form:"commit2" validate:"required"`
		Category tournament.TournamentCategory `json:"category" form:"category" validate:"required"`
		Map      string                        `json:"map" form:"map" validate:"required"`
	}
	if err := parseForm(r, &form); err != nil {
		web.WriteJsonWebError(w, err)
	} else if exists, err := s.Tournament.UserExists(form.Player1); err != nil {
		web.WriteJsonError(w, err)
	} else if !exists {
		web.WriteJsonError(w, errors.New("Unknown player1"))
	} else if exists, err := s.Tournament.UserExists(form.Player2); err != nil {
		web.WriteJsonError(w, err)
	} else if !exists {
		web.WriteJsonError(w, errors.New("Unknown player2"))
	} else if exists, err := s.Tournament.MapExists(form.Map, form.Category); err != nil {
		web.WriteJsonError(w, err)
	} else if !exists {
		web.WriteJsonError(w, errors.New("Unknown map"))
	} else if !CommitHashRegex.MatchString(form.Commit1) {
		web.WriteJsonError(w, errors.New("Invalid commit hash 1"))
	} else if !CommitHashRegex.MatchString(form.Commit2) {
		web.WriteJsonError(w, errors.New("Invalid commit hash 2"))
	} else if id, result, err := s.Tournament.RunMatch(form.Category, form.Map, tournament.Submission{form.Player1, form.Commit1}, tournament.Submission{form.Player2, form.Commit2}, tournament.SystemClock()); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteJson(w, JSONResponse{"id": id, "result": result})
	}
}
Exemplo n.º 3
0
func leaderboard(w http.ResponseWriter, r *http.Request, s *ServerState) {
	var form struct {
		Category tournament.TournamentCategory `json:"category" form:"category" validate:"required"`
	}
	if err := parseForm(r, &form); err != nil {
		web.WriteJsonWebError(w, err)
	} else if ranks, matches, err := s.Tournament.GetLeaderboard(form.Category); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteJson(w, JSONResponse{"ranks": ranks, "matches": matches})
	}
}
Exemplo n.º 4
0
func replay(w http.ResponseWriter, r *http.Request, s *ServerState) {
	var form struct {
		Id int64 `json:"id" form:"id" validate:"required,nonzero"`
	}
	if err := parseForm(r, &form); err != nil {
		web.WriteJsonWebError(w, err)
	} else if replay, err := s.Tournament.GetMatchReplayRaw(form.Id); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteRawGzipJson(w, replay)
	}
}
Exemplo n.º 5
0
func maps(w http.ResponseWriter, r *http.Request, s *ServerState) {
	var form struct {
		Category tournament.TournamentCategory `json:"category" form:"category" validate:"required"`
	}
	if err := parseForm(r, &form); err != nil {
		web.WriteJsonWebError(w, err)
	} else if maps, err := s.Tournament.ListMaps(form.Category); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteJson(w, JSONResponse{"maps": maps})
	}
}
Exemplo n.º 6
0
func mapSource(w http.ResponseWriter, r *http.Request, s *ServerState) {
	var form struct {
		Name     string                        `form:"name" validate:"required"`
		Category tournament.TournamentCategory `json:"category" form:"category" validate:"required"`
	}
	if err := parseForm(r, &form); err != nil {
		web.WriteJsonWebError(w, err)
	} else if source, err := s.Tournament.GetMapSource(form.Name, form.Category); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteXml(w, []byte(source))
	}
}
Exemplo n.º 7
0
func runLatestMatches(w http.ResponseWriter, r *http.Request, s *ServerState) {
	var form struct {
		Category tournament.TournamentCategory `json:"category" form:"category" validate:"required"`
	}
	if err := parseForm(r, &form); err != nil {
		web.WriteJsonWebError(w, err)
	} else if err := s.Tournament.RunLatestMatches(form.Category); err != nil {
		web.WriteJsonError(w, err)
	} else if err := s.Tournament.CalculateLeaderboard(form.Category); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteJson(w, JSONResponse{"success": true})
	}
}
Exemplo n.º 8
0
func createMap(w http.ResponseWriter, r *http.Request, s *ServerState) {
	var form struct {
		Name     string                        `json:"name" form:"name" validate:"required"`
		Category tournament.TournamentCategory `json:"category" form:"category" validate:"required"`
		Source   string                        `json:"source" form:"source" validate:"required"`
	}
	if err := parseForm(r, &form); err != nil {
		web.WriteJsonWebError(w, err)
	} else if !NameRegex.MatchString(form.Name) {
		web.WriteJsonError(w, errors.New("Invalid Name"))
	} else if err := s.Tournament.CreateMap(form.Name, form.Source, form.Category); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteJson(w, form)
	}
}
Exemplo n.º 9
0
func commits(w http.ResponseWriter, r *http.Request, s *ServerState) {
	var form struct {
		Name     string                        `json:"name" form:"name" validate:"required"`
		Category tournament.TournamentCategory `json:"category" form:"category" validate:"required"`
	}
	if err := parseForm(r, &form); err != nil {
		web.WriteJsonWebError(w, err)
	} else if exists, err := s.Tournament.UserExists(form.Name); err != nil {
		web.WriteJsonError(w, err)
	} else if !exists {
		web.WriteJsonError(w, errors.New("Unknown player"))
	} else if commits, err := s.Tournament.ListCommits(form.Name, form.Category); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteJson(w, JSONResponse{"commits": commits})
	}
}
Exemplo n.º 10
0
func submit(w http.ResponseWriter, r *http.Request, s *ServerState) {
	var form struct {
		Name       string                        `json:"name" form:"name" validate:"required"`
		CommitHash string                        `json:"commit_hash" form:"commit_hash" validate:"required"`
		Category   tournament.TournamentCategory `json:"category" form:"category" validate:"required"`
	}
	if err := parseForm(r, &form); err != nil {
		web.WriteJsonWebError(w, err)
	} else if exists, err := s.Tournament.UserExists(form.Name); err != nil {
		web.WriteJsonError(w, err)
	} else if !exists {
		web.WriteJsonError(w, errors.New("Unknown player"))
	} else if !CommitHashRegex.MatchString(form.CommitHash) {
		web.WriteJsonError(w, errors.New("Invalid commit hash"))
	} else if err := s.Tournament.SubmitCommit(form.Name, form.Category, form.CommitHash, time.Now()); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteJson(w, form)
	}
}