feat: game lobby service
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
// Package openenrollment implements the `lobby.game.open_enrollment`
|
||||
// message type. It transitions a game record from `draft` to
|
||||
// `enrollment_open` after enforcing admin-or-owner authorization.
|
||||
package openenrollment
|
||||
|
||||
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 open-enrollment use case.
|
||||
type Service struct {
|
||||
games ports.GameStore
|
||||
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
|
||||
|
||||
// Clock supplies the wall-clock used for UpdatedAt. 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 records the `lobby.game.transitions` counter on each
|
||||
// successful enrollment open. 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 open enrollment service: nil game store")
|
||||
}
|
||||
|
||||
clock := deps.Clock
|
||||
if clock == nil {
|
||||
clock = time.Now
|
||||
}
|
||||
logger := deps.Logger
|
||||
if logger == nil {
|
||||
logger = slog.Default()
|
||||
}
|
||||
|
||||
return &Service{
|
||||
games: deps.Games,
|
||||
clock: clock,
|
||||
logger: logger.With("service", "lobby.openenrollment"),
|
||||
telemetry: deps.Telemetry,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Input stores the arguments required to open 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, transitions the record to
|
||||
// `enrollment_open`, 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("open enrollment: nil service")
|
||||
}
|
||||
if ctx == nil {
|
||||
return game.Game{}, errors.New("open enrollment: nil context")
|
||||
}
|
||||
if err := input.Actor.Validate(); err != nil {
|
||||
return game.Game{}, fmt.Errorf("open enrollment: actor: %w", err)
|
||||
}
|
||||
if err := input.GameID.Validate(); err != nil {
|
||||
return game.Game{}, fmt.Errorf("open enrollment: %w", err)
|
||||
}
|
||||
|
||||
record, err := service.games.Get(ctx, input.GameID)
|
||||
if err != nil {
|
||||
return game.Game{}, fmt.Errorf("open enrollment: %w", err)
|
||||
}
|
||||
|
||||
if err := authorize(input.Actor, record); err != nil {
|
||||
return game.Game{}, err
|
||||
}
|
||||
|
||||
at := service.clock().UTC()
|
||||
err = service.games.UpdateStatus(ctx, ports.UpdateStatusInput{
|
||||
GameID: input.GameID,
|
||||
ExpectedFrom: game.StatusDraft,
|
||||
To: game.StatusEnrollmentOpen,
|
||||
Trigger: game.TriggerCommand,
|
||||
At: at,
|
||||
})
|
||||
if err != nil {
|
||||
return game.Game{}, fmt.Errorf("open enrollment: %w", err)
|
||||
}
|
||||
|
||||
service.telemetry.RecordGameTransition(ctx,
|
||||
string(game.StatusDraft),
|
||||
string(game.StatusEnrollmentOpen),
|
||||
string(game.TriggerCommand),
|
||||
)
|
||||
|
||||
updated, err := service.games.Get(ctx, input.GameID)
|
||||
if err != nil {
|
||||
return game.Game{}, fmt.Errorf("open enrollment: %w", err)
|
||||
}
|
||||
|
||||
logArgs := []any{
|
||||
"game_id", updated.GameID.String(),
|
||||
"from_status", string(game.StatusDraft),
|
||||
"to_status", string(updated.Status),
|
||||
"trigger", string(game.TriggerCommand),
|
||||
"actor_kind", string(input.Actor.Kind),
|
||||
}
|
||||
logArgs = append(logArgs, logging.ContextAttrs(ctx)...)
|
||||
service.logger.InfoContext(ctx, "game enrollment opened", 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 open enrollment on game %q",
|
||||
shared.ErrForbidden, record.GameID.String())
|
||||
}
|
||||
Reference in New Issue
Block a user