Example #1
0
func players(w http.ResponseWriter, r *http.Request, s *ServerState) {
	if userNames, err := s.Tournament.ListUsers(); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteJson(w, JSONResponse{"players": userNames})
	}
}
Example #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})
	}
}
Example #3
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,
		})
	}
}
Example #4
0
func shutdown(w http.ResponseWriter, r *http.Request, s *ServerState) {
	if s.Listener == nil {
		web.WriteJsonError(w, errors.New("Server not listening"))
	} else {
		web.WriteJson(w, JSONResponse{"message": "Shutting Down"})
		if err := s.Listener.Close(); err != nil {
			log.Print(err)
		}
		s.Listener = nil
	}
}
Example #5
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})
	}
}
Example #6
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})
	}
}
Example #7
0
func version(w http.ResponseWriter, r *http.Request, s *ServerState) {
	if schemaVersion, err := s.Tournament.Database.SchemaVersion(); err != nil {
		web.WriteJsonError(w, err)
	} else if gitVersion, err := GitVersion(s.Properties.ResourcePath); err != nil {
		web.WriteJsonError(w, err)
	} else {
		web.WriteJson(w, JSONResponse{
			"schemaVersion": schemaVersion,
			"sourceVersion": gitVersion,
		})
	}
}
Example #8
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})
	}
}
Example #9
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)
	}
}
Example #10
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})
	}
}
Example #11
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)
	}
}
Example #12
0
func categories(w http.ResponseWriter, r *http.Request, s *ServerState) {
	categories := s.Tournament.ListCategories()
	web.WriteJson(w, JSONResponse{"categories": categories})
}
Example #13
0
func api(w http.ResponseWriter, r *http.Request, s *ServerState) {
	web.WriteJson(w, JSONResponse{"routes": s.Routes})
}