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>
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package mappers
|
|
|
|
import (
|
|
"apskel-pos-be/internal/entities"
|
|
"apskel-pos-be/internal/models"
|
|
)
|
|
|
|
// ToGamePlayResponse converts a game play entity to a game play response
|
|
func ToGamePlayResponse(gamePlay *entities.GamePlay) *models.GamePlayResponse {
|
|
if gamePlay == nil {
|
|
return nil
|
|
}
|
|
|
|
return &models.GamePlayResponse{
|
|
ID: gamePlay.ID,
|
|
GameID: gamePlay.GameID,
|
|
CustomerID: gamePlay.CustomerID,
|
|
PrizeID: gamePlay.PrizeID,
|
|
CoinsUsed: gamePlay.CoinsUsed,
|
|
RandomSeed: gamePlay.RandomSeed,
|
|
CreatedAt: gamePlay.CreatedAt,
|
|
Game: ToGameResponse(&gamePlay.Game),
|
|
Customer: ToCustomerResponse(&gamePlay.Customer),
|
|
Prize: ToGamePrizeResponse(gamePlay.Prize),
|
|
}
|
|
}
|
|
|
|
// ToGamePlayResponses converts a slice of game play entities to game play responses
|
|
func ToGamePlayResponses(gamePlays []entities.GamePlay) []models.GamePlayResponse {
|
|
responses := make([]models.GamePlayResponse, len(gamePlays))
|
|
for i, gp := range gamePlays {
|
|
responses[i] = *ToGamePlayResponse(&gp)
|
|
}
|
|
return responses
|
|
}
|
|
|
|
// ToGamePlayResponsesFromPointers converts a slice of game play entity pointers to game play responses
|
|
func ToGamePlayResponsesFromPointers(gamePlays []*entities.GamePlay) []models.GamePlayResponse {
|
|
responses := make([]models.GamePlayResponse, len(gamePlays))
|
|
for i, gp := range gamePlays {
|
|
responses[i] = *ToGamePlayResponse(gp)
|
|
}
|
|
return responses
|
|
}
|
|
|
|
// ToGamePlayEntity converts a create game play request to a game play entity
|
|
func ToGamePlayEntity(req *models.CreateGamePlayRequest) *entities.GamePlay {
|
|
return &entities.GamePlay{
|
|
GameID: req.GameID,
|
|
CustomerID: req.CustomerID,
|
|
CoinsUsed: req.CoinsUsed,
|
|
RandomSeed: req.RandomSeed,
|
|
}
|
|
}
|