194 lines
6.0 KiB
Go
194 lines
6.0 KiB
Go
// Package manualreadytostart implements the `lobby.game.ready_to_start`
|
|
// message type. It transitions a game record from `enrollment_open` to
|
|
// `ready_to_start` after enforcing admin-or-owner authorization and the
|
|
// `approved_count >= min_players` precondition. Side effects (expiring
|
|
// every still-created invite and publishing `lobby.invite.expired`) are
|
|
// delegated to shared.CloseEnrollment so the auto-close path used by the
|
|
// enrollment automation worker stays in sync.
|
|
package manualreadytostart
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"galaxy/lobby/internal/domain/common"
|
|
"galaxy/lobby/internal/domain/game"
|
|
"galaxy/lobby/internal/logging"
|
|
"galaxy/lobby/internal/ports"
|
|
"galaxy/lobby/internal/service/shared"
|
|
"galaxy/lobby/internal/telemetry"
|
|
)
|
|
|
|
// Service executes the manual ready-to-start use case.
|
|
type Service struct {
|
|
games ports.GameStore
|
|
memberships ports.MembershipStore
|
|
invites ports.InviteStore
|
|
intents ports.IntentPublisher
|
|
clock func() time.Time
|
|
logger *slog.Logger
|
|
telemetry *telemetry.Runtime
|
|
}
|
|
|
|
// Dependencies groups the collaborators used by Service.
|
|
type Dependencies struct {
|
|
// Games mediates the CAS status transition and the result read.
|
|
Games ports.GameStore
|
|
|
|
// Memberships is scanned for active records to evaluate the
|
|
// approved_count >= min_players precondition.
|
|
Memberships ports.MembershipStore
|
|
|
|
// Invites is forwarded to shared.CloseEnrollment for the cascading
|
|
// expiry of created invites on close.
|
|
Invites ports.InviteStore
|
|
|
|
// Intents publishes lobby.invite.expired notifications produced by
|
|
// the cascading expiry.
|
|
Intents ports.IntentPublisher
|
|
|
|
// Clock supplies the wall-clock used for UpdatedAt and downstream
|
|
// notification timestamps. Defaults to time.Now when nil.
|
|
Clock func() time.Time
|
|
|
|
// Logger records structured service-level events. Defaults to
|
|
// slog.Default when nil.
|
|
Logger *slog.Logger
|
|
|
|
// Telemetry forwards into shared.CloseEnrollment so the
|
|
// `lobby.game.transitions` and `lobby.invite.outcomes` counters
|
|
// are emitted on each manual close. Optional; nil disables metric
|
|
// emission.
|
|
Telemetry *telemetry.Runtime
|
|
}
|
|
|
|
// NewService constructs one Service with deps.
|
|
func NewService(deps Dependencies) (*Service, error) {
|
|
if deps.Games == nil {
|
|
return nil, errors.New("new manual ready-to-start service: nil game store")
|
|
}
|
|
if deps.Memberships == nil {
|
|
return nil, errors.New("new manual ready-to-start service: nil membership store")
|
|
}
|
|
if deps.Invites == nil {
|
|
return nil, errors.New("new manual ready-to-start service: nil invite store")
|
|
}
|
|
if deps.Intents == nil {
|
|
return nil, errors.New("new manual ready-to-start service: nil intent publisher")
|
|
}
|
|
|
|
clock := deps.Clock
|
|
if clock == nil {
|
|
clock = time.Now
|
|
}
|
|
logger := deps.Logger
|
|
if logger == nil {
|
|
logger = slog.Default()
|
|
}
|
|
|
|
return &Service{
|
|
games: deps.Games,
|
|
memberships: deps.Memberships,
|
|
invites: deps.Invites,
|
|
intents: deps.Intents,
|
|
clock: clock,
|
|
logger: logger.With("service", "lobby.manualreadytostart"),
|
|
telemetry: deps.Telemetry,
|
|
}, nil
|
|
}
|
|
|
|
// Input stores the arguments required to manually close enrollment on one
|
|
// game.
|
|
type Input struct {
|
|
// Actor identifies the caller.
|
|
Actor shared.Actor
|
|
|
|
// GameID identifies the target game record.
|
|
GameID common.GameID
|
|
}
|
|
|
|
// Handle authorizes the actor, checks status and roster preconditions,
|
|
// performs the enrollment close (transition + invite expiry +
|
|
// notifications), and returns the post-transition snapshot.
|
|
func (service *Service) Handle(ctx context.Context, input Input) (game.Game, error) {
|
|
if service == nil {
|
|
return game.Game{}, errors.New("manual ready-to-start: nil service")
|
|
}
|
|
if ctx == nil {
|
|
return game.Game{}, errors.New("manual ready-to-start: nil context")
|
|
}
|
|
if err := input.Actor.Validate(); err != nil {
|
|
return game.Game{}, fmt.Errorf("manual ready-to-start: actor: %w", err)
|
|
}
|
|
if err := input.GameID.Validate(); err != nil {
|
|
return game.Game{}, fmt.Errorf("manual ready-to-start: %w", err)
|
|
}
|
|
|
|
record, err := service.games.Get(ctx, input.GameID)
|
|
if err != nil {
|
|
return game.Game{}, fmt.Errorf("manual ready-to-start: %w", err)
|
|
}
|
|
|
|
if err := authorize(input.Actor, record); err != nil {
|
|
return game.Game{}, err
|
|
}
|
|
|
|
if record.Status != game.StatusEnrollmentOpen {
|
|
return game.Game{}, fmt.Errorf(
|
|
"manual ready-to-start: status %q is not enrollment_open: %w",
|
|
record.Status, game.ErrConflict,
|
|
)
|
|
}
|
|
|
|
approvedCount, err := shared.CountActiveMemberships(ctx, service.memberships, input.GameID)
|
|
if err != nil {
|
|
return game.Game{}, fmt.Errorf("manual ready-to-start: %w", err)
|
|
}
|
|
if approvedCount < record.MinPlayers {
|
|
return game.Game{}, fmt.Errorf(
|
|
"manual ready-to-start: approved_count %d is below min_players %d: %w",
|
|
approvedCount, record.MinPlayers, game.ErrConflict,
|
|
)
|
|
}
|
|
|
|
at := service.clock().UTC()
|
|
updated, err := shared.CloseEnrollment(ctx, shared.CloseEnrollmentDeps{
|
|
Games: service.games,
|
|
Invites: service.invites,
|
|
Intents: service.intents,
|
|
Logger: service.logger,
|
|
Telemetry: service.telemetry,
|
|
}, input.GameID, game.TriggerManual, at)
|
|
if err != nil {
|
|
return game.Game{}, fmt.Errorf("manual ready-to-start: %w", err)
|
|
}
|
|
|
|
logArgs := []any{
|
|
"game_id", updated.GameID.String(),
|
|
"from_status", string(game.StatusEnrollmentOpen),
|
|
"to_status", string(updated.Status),
|
|
"trigger", string(game.TriggerManual),
|
|
"approved_count", approvedCount,
|
|
"actor_kind", string(input.Actor.Kind),
|
|
}
|
|
logArgs = append(logArgs, logging.ContextAttrs(ctx)...)
|
|
service.logger.InfoContext(ctx, "game manually moved to ready_to_start", logArgs...)
|
|
|
|
return updated, nil
|
|
}
|
|
|
|
// authorize enforces admin OR private-owner access to the record.
|
|
func authorize(actor shared.Actor, record game.Game) error {
|
|
if actor.IsAdmin() {
|
|
return nil
|
|
}
|
|
if record.GameType == game.GameTypePrivate && actor.UserID == record.OwnerUserID {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("%w: actor is not authorized to close enrollment on game %q",
|
|
shared.ErrForbidden, record.GameID.String())
|
|
}
|