Games now spend the wallet's EnakCoin instead of the per-type tokens (docs/prd-point-coin.md F8, K1, PC-403). GamePlayProcessor.PlayGame charges the game's metadata.coin_cost, 1 when it is not set; a cost that is not a whole number of at least 1 refuses the game. In one transaction it picks the prize, takes the EnakCoin with a GAME_SPEND row pointing at the new game_plays.id (which locks the wallet, so a customer's plays at the same time queue up), records the play and takes the prize from stock. The play owns its transaction, so the spin service no longer wraps it, and the admin play endpoint is now atomic too. The game, game prize and game play repositories go through DBFromContext so they join that transaction. DecreaseStock now reports a prize that ran out (ErrGamePrizeOutOfStock) instead of silently updating nothing; that, or any other stock failure, cancels the whole play, where it used to be only printed. The manual AddTokens rollback is gone. Not enough EnakCoin, an inactive game or a prize that ran out answer 400 on /customer/spin instead of 500. game_plays.token_used is renamed coins_used (migration 000095). What a play costs is no longer the caller's choice, so PlayGameRequest loses token_used. Responses carry coins_used and coins_remaining; token_used and tokens_remaining stay as deprecated copies until the apps move over, and sort_by=token_used still sorts by coins_used. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
2.4 KiB
Go
64 lines
2.4 KiB
Go
package contract
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type CreateGamePlayRequest struct {
|
|
GameID uuid.UUID `json:"game_id" validate:"required"`
|
|
CustomerID uuid.UUID `json:"customer_id" validate:"required"`
|
|
CoinsUsed int `json:"coins_used" validate:"min=0"`
|
|
RandomSeed *string `json:"random_seed,omitempty"`
|
|
}
|
|
|
|
type GamePlayResponse struct {
|
|
ID uuid.UUID `json:"id"`
|
|
GameID uuid.UUID `json:"game_id"`
|
|
CustomerID uuid.UUID `json:"customer_id"`
|
|
PrizeID *uuid.UUID `json:"prize_id,omitempty"`
|
|
CoinsUsed int `json:"coins_used"`
|
|
// Deprecated: the same as CoinsUsed, kept until the apps read coins_used.
|
|
TokenUsed int `json:"token_used"`
|
|
RandomSeed *string `json:"random_seed,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
Game *GameResponse `json:"game,omitempty"`
|
|
Customer *CustomerResponse `json:"customer,omitempty"`
|
|
Prize *GamePrizeResponse `json:"prize,omitempty"`
|
|
}
|
|
|
|
type ListGamePlaysRequest struct {
|
|
Page int `json:"page" validate:"min=1"`
|
|
Limit int `json:"limit" validate:"min=1,max=100"`
|
|
Search string `json:"search"`
|
|
GameID *uuid.UUID `json:"game_id"`
|
|
CustomerID *uuid.UUID `json:"customer_id"`
|
|
PrizeID *uuid.UUID `json:"prize_id"`
|
|
SortBy string `json:"sort_by" validate:"omitempty,oneof=created_at coins_used token_used"`
|
|
SortOrder string `json:"sort_order" validate:"omitempty,oneof=asc desc"`
|
|
}
|
|
|
|
type PaginatedGamePlaysResponse struct {
|
|
Data []GamePlayResponse `json:"data"`
|
|
TotalCount int `json:"total_count"`
|
|
Page int `json:"page"`
|
|
Limit int `json:"limit"`
|
|
TotalPages int `json:"total_pages"`
|
|
}
|
|
|
|
// PlayGameRequest plays a game for a customer. What it costs is the game's
|
|
// metadata.coin_cost (docs/prd-point-coin.md F8), never the caller's choice.
|
|
type PlayGameRequest struct {
|
|
GameID uuid.UUID `json:"game_id" validate:"required"`
|
|
CustomerID uuid.UUID `json:"customer_id" validate:"required"`
|
|
}
|
|
|
|
type PlayGameResponse struct {
|
|
GamePlay GamePlayResponse `json:"game_play"`
|
|
PrizeWon *GamePrizeResponse `json:"prize_won,omitempty"`
|
|
CoinsRemaining int64 `json:"coins_remaining"`
|
|
// Deprecated: the same as CoinsRemaining, kept until the apps read coins_remaining.
|
|
TokensRemaining int64 `json:"tokens_remaining"`
|
|
}
|