mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-13 22:32:09 +00:00
feat: expose authoritative ranked profile
This commit is contained in:
+41
-6
@@ -21,12 +21,14 @@ const maxBodyBytes = 8 << 10
|
||||
type CandidateProvider func(playerID, ticketID string) (domain.Candidate, error)
|
||||
|
||||
type Service struct {
|
||||
Sessions *domain.SessionStore
|
||||
Queue *domain.Queue
|
||||
Candidate CandidateProvider
|
||||
Now func() time.Time
|
||||
Proposals map[string]*domain.Proposal
|
||||
proposalMu sync.Mutex
|
||||
Sessions *domain.SessionStore
|
||||
Queue *domain.Queue
|
||||
Candidate CandidateProvider
|
||||
Now func() time.Time
|
||||
Proposals map[string]*domain.Proposal
|
||||
RankedProfiles map[string]domain.RankedProfile
|
||||
TierPolicy domain.TierPolicy
|
||||
proposalMu sync.Mutex
|
||||
}
|
||||
|
||||
func (s *Service) Handler() http.Handler {
|
||||
@@ -35,6 +37,7 @@ func (s *Service) Handler() http.Handler {
|
||||
mux.HandleFunc("/v1/queue", s.queueCreate)
|
||||
mux.HandleFunc("/v1/queue/", s.queueMutation)
|
||||
mux.HandleFunc("/v1/proposals/", s.proposalMutation)
|
||||
mux.HandleFunc("/v1/profile/ranked", s.rankedProfile)
|
||||
return mux
|
||||
}
|
||||
|
||||
@@ -184,6 +187,38 @@ func (s *Service) proposalMutation(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, toProposalResponse(updated))
|
||||
}
|
||||
|
||||
type rankedProfileResponse struct {
|
||||
Rating float64 `json:"rating"`
|
||||
RD float64 `json:"rd"`
|
||||
Volatility float64 `json:"volatility"`
|
||||
RankedGames int `json:"ranked_games"`
|
||||
Tier string `json:"tier"`
|
||||
Provisional bool `json:"provisional"`
|
||||
SeasonID string `json:"season_id,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Service) rankedProfile(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method_not_allowed")
|
||||
return
|
||||
}
|
||||
playerID, ok := s.authenticate(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
profile, exists := s.RankedProfiles[playerID]
|
||||
if !exists {
|
||||
writeError(w, http.StatusNotFound, "not_found")
|
||||
return
|
||||
}
|
||||
tier, err := domain.RankedTier(profile, s.TierPolicy)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "ranked_profile_unavailable")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, rankedProfileResponse{Rating: profile.Value, RD: profile.RD, Volatility: profile.Volatility, RankedGames: profile.RankedGames, Tier: string(tier), Provisional: domain.RankedIsProvisional(profile), SeasonID: profile.LastSeasonID})
|
||||
}
|
||||
|
||||
func (s *Service) authenticate(w http.ResponseWriter, r *http.Request) (string, bool) {
|
||||
if s.Sessions == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "auth_unavailable")
|
||||
|
||||
Reference in New Issue
Block a user