This commit is contained in:
2026-10-03 01:28:09 +07:00
parent 2e1f8e21e9
commit 55d8522f7c
5 changed files with 63 additions and 35 deletions
+37 -20
View File
@@ -2,6 +2,7 @@ package gameplay_service
import (
"context"
"errors"
"evening_detective_server/internal/modules/cleaner"
"evening_detective_server/internal/modules/storytelling"
"evening_detective_server/internal/repos/actions_repo"
@@ -10,6 +11,12 @@ import (
"evening_detective_server/internal/repos/teams_repo"
"evening_detective_server/internal/services/game_model"
"evening_detective_server/internal/services/scenarios_service"
"fmt"
)
var (
ErrTeamNotFound = errors.New("Команда не найдена")
ErrGameNotFound = errors.New("Игра не найдена")
)
type GameplayService struct {
@@ -46,22 +53,22 @@ func NewGameplayService(
}
func (s *GameplayService) GiveTeamApplications(ctx context.Context, teamID int, name string) error {
return s.applicationsRepo.UpdateApplicationStatus(ctx, teamID, name, "done")
return mapGameplayError(s.applicationsRepo.UpdateApplicationStatus(ctx, teamID, name, "done"))
}
func (s *GameplayService) GetTeamActions(ctx context.Context, teamID int, password string) (*storytelling.Story, *game_model.Game, []actions_repo.Action, error) {
team, err := s.teamsRepo.GetTeamByID(ctx, teamID)
if err != nil {
return nil, nil, nil, err
return nil, nil, nil, mapGameplayError(err)
}
if team.Password != password || team.ParticipationStatus == "cancelled" {
return nil, nil, nil, teams_repo.ErrTeamNotFound
return nil, nil, nil, fmt.Errorf("%w: %v", ErrTeamNotFound, teams_repo.ErrTeamNotFound)
}
game, err := s.gamesRepo.GetGameByID(ctx, team.GameID)
if err != nil {
return nil, nil, nil, err
return nil, nil, nil, mapGameplayError(err)
}
if game.Status == "finish" {
return &storytelling.Story{}, game_model.MapGame(game, s.domain), nil, nil
@@ -69,12 +76,12 @@ func (s *GameplayService) GetTeamActions(ctx context.Context, teamID int, passwo
actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID)
if err != nil {
return nil, nil, nil, err
return nil, nil, nil, mapGameplayError(err)
}
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID)
if err != nil {
return nil, nil, nil, err
return nil, nil, nil, mapGameplayError(err)
}
codes := make([]string, len(actions))
@@ -89,36 +96,36 @@ func (s *GameplayService) GetTeamActions(ctx context.Context, teamID int, passwo
func (s *GameplayService) AddTeamAction(ctx context.Context, teamID int, password string, actionCode string, viaMCP bool) error {
team, err := s.teamsRepo.GetTeamByID(ctx, teamID)
if err != nil {
return err
return mapGameplayError(err)
}
if team.Password != password || team.ParticipationStatus == "cancelled" {
return teams_repo.ErrTeamNotFound
return fmt.Errorf("%w: %v", ErrTeamNotFound, teams_repo.ErrTeamNotFound)
}
_, err = s.actionsRepo.AddAction(ctx, teamID, actionCode, viaMCP)
if err != nil {
return err
return mapGameplayError(err)
}
if team.ParticipationStatus == "pending" {
if err := s.teamsRepo.ConfirmParticipationByFirstAction(ctx, teamID); err != nil {
return err
return mapGameplayError(err)
}
}
game, err := s.gamesRepo.GetGameByID(ctx, team.GameID)
if err != nil {
return err
return mapGameplayError(err)
}
actionDetails, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID)
if err != nil {
return err
return mapGameplayError(err)
}
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID)
if err != nil {
return err
return mapGameplayError(err)
}
actions := make([]string, len(actionDetails))
@@ -133,7 +140,7 @@ func (s *GameplayService) AddTeamAction(ctx context.Context, teamID int, passwor
app := newApplication(application, s.domain)
app.Name = s.cleaner.ClearText(app.Name)
if _, err := s.applicationsRepo.AddApplication(ctx, teamID, app.Name, app.Image, app.FileType); err != nil {
return err
return mapGameplayError(err)
}
}
@@ -143,22 +150,22 @@ func (s *GameplayService) AddTeamAction(ctx context.Context, teamID int, passwor
func (s *GameplayService) DeleteLastTeamAction(ctx context.Context, teamID int) error {
team, err := s.teamsRepo.GetTeamByID(ctx, teamID)
if err != nil {
return err
return mapGameplayError(err)
}
game, err := s.gamesRepo.GetGameByID(ctx, team.GameID)
if err != nil {
return err
return mapGameplayError(err)
}
actionDetails, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID)
if err != nil {
return err
return mapGameplayError(err)
}
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID)
if err != nil {
return err
return mapGameplayError(err)
}
actions := make([]string, len(actionDetails))
@@ -171,9 +178,19 @@ func (s *GameplayService) DeleteLastTeamAction(ctx context.Context, teamID int)
for _, application := range lastPlace.Applications {
if err := s.applicationsRepo.DeleteApplicationByTeamIDAndName(ctx, teamID, application.Name); err != nil {
return err
return mapGameplayError(err)
}
}
return s.actionsRepo.DeleteLastActionByTeamID(ctx, teamID)
return mapGameplayError(s.actionsRepo.DeleteLastActionByTeamID(ctx, teamID))
}
func mapGameplayError(err error) error {
if errors.Is(err, teams_repo.ErrTeamNotFound) {
return fmt.Errorf("%w: %v", ErrTeamNotFound, err)
}
if errors.Is(err, games_repo.ErrGameNotFound) {
return fmt.Errorf("%w: %v", ErrGameNotFound, err)
}
return err
}
+1 -1
View File
@@ -264,7 +264,7 @@ func (s *UsersService) Login(
)
if err != nil {
if errors.Is(err, users_repo.ErrUserNotFound) {
return "", "", err
return "", "", fmt.Errorf("%w: %w", ErrInvalidCredentials, err)
}
return "", "", err
}