feat(backend): per-tier, per-kind active-game limits with a guest funnel
CI / changes (pull_request) Successful in 5s
CI / unit (pull_request) Successful in 11s
CI / integration (pull_request) Successful in 20s
CI / ui (pull_request) Has been skipped
CI / conformance (pull_request) Successful in 11s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 2m41s
CI / changes (pull_request) Successful in 5s
CI / unit (pull_request) Successful in 11s
CI / integration (pull_request) Successful in 20s
CI / ui (pull_request) Has been skipped
CI / conformance (pull_request) Successful in 11s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 2m41s
Cap a player's simultaneous unfinished games per kind (vs_ai, random, friends) with independent guest and durable-account tiers, held in a new single-row backend.config table (-1 = unlimited) behind an in-memory cache and editable live in the admin console (/_gm/limits). Each game is tagged with games.game_kind on creation. This replaces the earlier flat MaxActiveQuickGames=10 combined cap: the per-tier/kind config is the single mechanism, enforced at the same handler gate (ensureUnderGameLimit by kind on lobby/enqueue) plus the durable friends cap in CreateInvitation. game.Service.AtGameLimit only resolves the tier and counts; the limit policy stays at the request edge. Guests are now refused friend requests, friend-code redemption, befriend-in-game and invitation creation outright (403 guest_forbidden) -- previously only the UI hid these. Admin: a kind column in both game lists and the config editor. Defaults: guest 1 vs_ai / 1 random / 0 friends; durable 10 / 10 / 10.
This commit is contained in:
@@ -319,6 +319,8 @@ func statusForError(err error) (int, string) {
|
||||
return http.StatusConflict, "request_declined"
|
||||
case errors.Is(err, social.ErrFriendCodeInvalid):
|
||||
return http.StatusUnprocessableEntity, "friend_code_invalid"
|
||||
case errors.Is(err, social.ErrGuestForbidden), errors.Is(err, lobby.ErrGuestForbidden):
|
||||
return http.StatusForbidden, "guest_forbidden"
|
||||
case errors.Is(err, feedback.ErrGuestForbidden):
|
||||
return http.StatusForbidden, "feedback_guest_forbidden"
|
||||
case errors.Is(err, feedback.ErrBanned):
|
||||
|
||||
@@ -70,6 +70,10 @@ func (s *Server) registerConsole(router *gin.Engine) {
|
||||
gm.POST("/bans/unban", s.consoleUnban)
|
||||
gm.GET("/games", s.consoleGames)
|
||||
gm.GET("/games/:id", s.consoleGameDetail)
|
||||
if s.gamelimits != nil {
|
||||
gm.GET("/limits", s.consoleLimits)
|
||||
gm.POST("/limits", s.consoleUpdateLimits)
|
||||
}
|
||||
gm.GET("/complaints", s.consoleComplaints)
|
||||
gm.GET("/complaints/:id", s.consoleComplaintDetail)
|
||||
gm.POST("/complaints/:id/resolve", s.consoleResolveComplaint)
|
||||
@@ -1244,7 +1248,7 @@ func (s *Server) consoleUUID(c *gin.Context, back string) (uuid.UUID, bool) {
|
||||
|
||||
// gameRow projects a game summary into its console row.
|
||||
func gameRow(g game.Game) adminconsole.GameRow {
|
||||
return adminconsole.GameRow{ID: g.ID.String(), Variant: g.Variant.String(), Status: g.Status, Players: g.Players, UpdatedAt: fmtTime(g.UpdatedAt), VsAI: g.VsAI}
|
||||
return adminconsole.GameRow{ID: g.ID.String(), Variant: g.Variant.String(), Status: g.Status, Players: g.Players, UpdatedAt: fmtTime(g.UpdatedAt), VsAI: g.VsAI, Kind: g.Kind.String()}
|
||||
}
|
||||
|
||||
// trimForm returns the trimmed value of a posted form field.
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"scrabble/backend/internal/adminconsole"
|
||||
"scrabble/backend/internal/gamelimits"
|
||||
)
|
||||
|
||||
// consoleLimits renders the per-tier, per-kind active-game limit form (backend.config), read from
|
||||
// the in-memory cache.
|
||||
func (s *Server) consoleLimits(c *gin.Context) {
|
||||
cfg := s.gamelimits.Get()
|
||||
s.renderConsole(c, "limits", "limits", "Game limits", adminconsole.GameLimitsView{
|
||||
GuestVsAI: cfg.Guest.VsAI,
|
||||
GuestRandom: cfg.Guest.Random,
|
||||
GuestFriends: cfg.Guest.Friends,
|
||||
DurableVsAI: cfg.Durable.VsAI,
|
||||
DurableRandom: cfg.Durable.Random,
|
||||
DurableFriends: cfg.Durable.Friends,
|
||||
})
|
||||
}
|
||||
|
||||
// consoleUpdateLimits saves the active-game limits and refreshes the hot cache in place. Each
|
||||
// field is a per-kind cap: -1 is unlimited, 0 blocks the kind, a positive value caps concurrent games.
|
||||
func (s *Server) consoleUpdateLimits(c *gin.Context) {
|
||||
cfg := gamelimits.Config{
|
||||
Guest: gamelimits.Limits{
|
||||
VsAI: atoiForm(c, "guest_vs_ai"),
|
||||
Random: atoiForm(c, "guest_random"),
|
||||
Friends: atoiForm(c, "guest_friends"),
|
||||
},
|
||||
Durable: gamelimits.Limits{
|
||||
VsAI: atoiForm(c, "durable_vs_ai"),
|
||||
Random: atoiForm(c, "durable_random"),
|
||||
Friends: atoiForm(c, "durable_friends"),
|
||||
},
|
||||
}
|
||||
if err := validateGameLimits(cfg); err != nil {
|
||||
s.renderConsoleMessage(c, "Invalid", err.Error(), "/_gm/limits")
|
||||
return
|
||||
}
|
||||
if err := s.gamelimits.Update(c.Request.Context(), cfg); err != nil {
|
||||
s.consoleError(c, err)
|
||||
return
|
||||
}
|
||||
s.renderConsoleMessage(c, "Saved", "game limits updated", "/_gm/limits")
|
||||
}
|
||||
|
||||
// validateGameLimits rejects a limit below -1 (the unlimited sentinel); -1, 0 and any positive count
|
||||
// are valid. It mirrors the backend.config CHECK so a bad value is refused with a clean message
|
||||
// rather than a raw database error.
|
||||
func validateGameLimits(cfg gamelimits.Config) error {
|
||||
for _, v := range []int{
|
||||
cfg.Guest.VsAI, cfg.Guest.Random, cfg.Guest.Friends,
|
||||
cfg.Durable.VsAI, cfg.Durable.Random, cfg.Durable.Friends,
|
||||
} {
|
||||
if v < gamelimits.Unlimited {
|
||||
return fmt.Errorf("a limit must be -1 (unlimited), 0, or a positive count")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"scrabble/backend/internal/engine"
|
||||
"scrabble/backend/internal/game"
|
||||
"scrabble/backend/internal/gamelimits"
|
||||
)
|
||||
|
||||
// The handlers below cover the game and chat operations the UI needs. They follow
|
||||
@@ -46,10 +47,11 @@ type historyDTO struct {
|
||||
}
|
||||
|
||||
// gameListDTO is the caller's games (active and finished) for the lobby. AtGameLimit
|
||||
// reports whether the caller has reached the simultaneous quick-game cap
|
||||
// (game.MaxActiveQuickGames); while it is true the lobby disables "New Game" and shows a
|
||||
// notice. It rides the lobby response — which the lobby re-fetches on every game event —
|
||||
// instead of a separate request.
|
||||
// reports whether the caller has reached its tier's active-game cap for the random
|
||||
// (quick auto-match) kind (the per-tier, per-kind limits in backend.config); while
|
||||
// it is true the lobby disables "New Game" and shows a notice. It rides the lobby
|
||||
// response — which the lobby re-fetches on every game event — instead of a separate
|
||||
// request.
|
||||
type gameListDTO struct {
|
||||
Games []gameDTO `json:"games"`
|
||||
AtGameLimit bool `json:"at_game_limit"`
|
||||
@@ -395,23 +397,13 @@ func (s *Server) handleSaveDraft(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, okResponse{OK: true})
|
||||
}
|
||||
|
||||
// atGameLimit reports whether uid already holds the maximum number of simultaneous
|
||||
// quick games (game.MaxActiveQuickGames). It backs both the lobby's at_game_limit flag
|
||||
// and the new-game gate; friend games created by invitation are not counted.
|
||||
func (s *Server) atGameLimit(ctx context.Context, uid uuid.UUID) (bool, error) {
|
||||
n, err := s.games.CountActiveQuickGames(ctx, uid)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return n >= game.MaxActiveQuickGames, nil
|
||||
}
|
||||
|
||||
// ensureUnderGameLimit aborts the request with 409 game_limit_reached when uid is at the
|
||||
// simultaneous quick-game cap, and reports whether the caller may proceed. It guards
|
||||
// every new-game entry point — quick auto-match/AI and invitation creation; accepting an
|
||||
// incoming invitation is deliberately exempt.
|
||||
func (s *Server) ensureUnderGameLimit(c *gin.Context, uid uuid.UUID) bool {
|
||||
atLimit, err := s.atGameLimit(c.Request.Context(), uid)
|
||||
// ensureUnderGameLimit aborts the request with 409 game_limit_reached when uid has reached its
|
||||
// tier's active-game cap for kind (the per-tier, per-kind limits in backend.config), and reports
|
||||
// whether the caller may proceed. It guards the quick new-game entry points — auto-match (random)
|
||||
// and AI (vs_ai). Friend-invitation limits are enforced in the lobby's CreateInvitation, and
|
||||
// accepting an incoming invitation is deliberately exempt.
|
||||
func (s *Server) ensureUnderGameLimit(c *gin.Context, uid uuid.UUID, kind gamelimits.Kind) bool {
|
||||
atLimit, err := s.games.AtGameLimit(c.Request.Context(), uid, kind)
|
||||
if err != nil {
|
||||
s.abortErr(c, err)
|
||||
return false
|
||||
@@ -437,7 +429,7 @@ func (s *Server) handleListGames(c *gin.Context) {
|
||||
s.abortErr(c, err)
|
||||
return
|
||||
}
|
||||
atLimit, err := s.atGameLimit(c.Request.Context(), uid)
|
||||
atLimit, err := s.games.AtGameLimit(c.Request.Context(), uid, gamelimits.KindRandom)
|
||||
if err != nil {
|
||||
s.abortErr(c, err)
|
||||
return
|
||||
|
||||
@@ -133,9 +133,6 @@ func (s *Server) handleCreateInvitation(c *gin.Context) {
|
||||
}
|
||||
inviteeIDs = append(inviteeIDs, id)
|
||||
}
|
||||
if !s.ensureUnderGameLimit(c, uid) {
|
||||
return
|
||||
}
|
||||
inv, err := s.invitations.CreateInvitation(c.Request.Context(), uid, inviteeIDs, settings)
|
||||
if err != nil {
|
||||
s.abortErr(c, err)
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
|
||||
"scrabble/backend/internal/engine"
|
||||
"scrabble/backend/internal/gamelimits"
|
||||
)
|
||||
|
||||
// The /api/v1/user/* endpoints require X-User-ID (RequireUserID middleware). The
|
||||
@@ -189,13 +190,15 @@ func (s *Server) handleEnqueue(c *gin.Context) {
|
||||
if !s.ensureVariantAllowed(c, uid, variant.String()) {
|
||||
return
|
||||
}
|
||||
if !s.ensureUnderGameLimit(c, uid) {
|
||||
return
|
||||
}
|
||||
kind := gamelimits.KindRandom
|
||||
enter := s.matchmaker.Enqueue
|
||||
if req.VsAI {
|
||||
kind = gamelimits.KindVsAI
|
||||
enter = s.matchmaker.StartVsAI
|
||||
}
|
||||
if !s.ensureUnderGameLimit(c, uid, kind) {
|
||||
return
|
||||
}
|
||||
res, err := enter(c.Request.Context(), uid, variant, req.MultipleWordsPerTurn)
|
||||
if err != nil {
|
||||
s.abortErr(c, err)
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
"scrabble/backend/internal/engine"
|
||||
"scrabble/backend/internal/feedback"
|
||||
"scrabble/backend/internal/game"
|
||||
"scrabble/backend/internal/gamelimits"
|
||||
"scrabble/backend/internal/link"
|
||||
"scrabble/backend/internal/lobby"
|
||||
"scrabble/backend/internal/notify"
|
||||
@@ -100,6 +101,10 @@ type Deps struct {
|
||||
// console routes are registered when the wallet surface lands. A nil Payments
|
||||
// omits them.
|
||||
Payments *payments.Service
|
||||
// GameLimits is the per-tier, per-kind active-game limit config, cached in memory. The
|
||||
// game domain reads it through game.Service (SetGameLimits) for the new-game gate; the admin
|
||||
// console reads and edits it here. A nil GameLimits omits the limits console section.
|
||||
GameLimits *gamelimits.Service
|
||||
// Notifier publishes live-event intents — here the banner-eligibility re-poll
|
||||
// signal the banner/hint/role console actions emit. A nil Notifier discards
|
||||
// them (notify.Nop).
|
||||
@@ -140,6 +145,7 @@ type Server struct {
|
||||
banview *banview.View
|
||||
ads *ads.Service
|
||||
payments *payments.Service
|
||||
gamelimits *gamelimits.Service
|
||||
robokassa robokassa.Config
|
||||
notifier notify.Publisher
|
||||
console *adminconsole.Renderer
|
||||
@@ -194,6 +200,7 @@ func New(addr string, deps Deps) *Server {
|
||||
banview: deps.BanView,
|
||||
ads: deps.Ads,
|
||||
payments: deps.Payments,
|
||||
gamelimits: deps.GameLimits,
|
||||
robokassa: deps.Robokassa,
|
||||
notifier: notifier,
|
||||
renderer: deps.Renderer,
|
||||
|
||||
Reference in New Issue
Block a user