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
+20
View File
@@ -0,0 +1,20 @@
package app
import (
"errors"
"evening_detective_server/internal/services/gameplay_service"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func gameplayErrorStatus(err error, internalMessage string) error {
if errors.Is(err, gameplay_service.ErrTeamNotFound) {
return status.Error(codes.NotFound, gameplay_service.ErrTeamNotFound.Error())
}
if errors.Is(err, gameplay_service.ErrGameNotFound) {
return status.Error(codes.NotFound, gameplay_service.ErrGameNotFound.Error())
}
return status.Error(codes.Internal, internalMessage)
}
+4 -12
View File
@@ -105,9 +105,7 @@ func (s *server) GiveTeamApplications(ctx context.Context, req *proto.GiveTeamAp
req.Name, req.Name,
) )
if err != nil { if err != nil {
return &proto.GiveTeamApplicationsRsp{ return nil, gameplayErrorStatus(err, "не удалось обработать заявку команды")
Error: err.Error(),
}, nil
} }
return &proto.GiveTeamApplicationsRsp{}, nil return &proto.GiveTeamApplicationsRsp{}, nil
@@ -120,9 +118,7 @@ func (s *server) GetTeamStory(ctx context.Context, req *proto.GetTeamStoryReq) (
req.Password, req.Password,
) )
if err != nil { if err != nil {
return &proto.GetTeamStoryRsp{ return nil, gameplayErrorStatus(err, "не удалось получить историю команды")
Error: err.Error(),
}, nil
} }
protoActions := make([]*proto.TeamAction, 0, len(actions)) protoActions := make([]*proto.TeamAction, 0, len(actions))
for _, action := range actions { for _, action := range actions {
@@ -145,9 +141,7 @@ func (s *server) AddTeamAction(ctx context.Context, req *proto.AddTeamActionReq)
false, false,
) )
if err != nil { if err != nil {
return &proto.AddTeamActionRsp{ return nil, gameplayErrorStatus(err, "не удалось добавить действие команды")
Error: err.Error(),
}, nil
} }
return &proto.AddTeamActionRsp{}, nil return &proto.AddTeamActionRsp{}, nil
@@ -167,9 +161,7 @@ func (s *server) DeleteLastTeamAction(ctx context.Context, req *proto.DeleteLast
int(req.ID), int(req.ID),
) )
if err != nil { if err != nil {
return &proto.DeleteLastTeamActionRsp{ return nil, gameplayErrorStatus(err, "не удалось удалить последнее действие команды")
Error: err.Error(),
}, nil
} }
return &proto.DeleteLastTeamActionRsp{}, nil return &proto.DeleteLastTeamActionRsp{}, nil
+1 -2
View File
@@ -3,7 +3,6 @@ package app
import ( import (
"errors" "errors"
"evening_detective_server/internal/repos/users_repo"
"evening_detective_server/internal/services/users_service" "evening_detective_server/internal/services/users_service"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
@@ -16,7 +15,7 @@ func userErrorStatus(err error, internalMessage string) error {
return status.Error(codes.NotFound, err.Error()) return status.Error(codes.NotFound, err.Error())
case errors.Is(err, users_service.ErrTermsNotAccepted), errors.Is(err, users_service.ErrInvalidEmail), errors.Is(err, users_service.ErrInvalidRole), errors.Is(err, users_service.ErrInvalidPasswordChange): case errors.Is(err, users_service.ErrTermsNotAccepted), errors.Is(err, users_service.ErrInvalidEmail), errors.Is(err, users_service.ErrInvalidRole), errors.Is(err, users_service.ErrInvalidPasswordChange):
return status.Error(codes.InvalidArgument, err.Error()) return status.Error(codes.InvalidArgument, err.Error())
case errors.Is(err, users_service.ErrInvalidCredentials), errors.Is(err, users_repo.ErrUserNotFound): case errors.Is(err, users_service.ErrInvalidCredentials):
return status.Error(codes.Unauthenticated, users_service.ErrInvalidCredentials.Error()) return status.Error(codes.Unauthenticated, users_service.ErrInvalidCredentials.Error())
case errors.Is(err, users_service.ErrInvalidAccountPassword): case errors.Is(err, users_service.ErrInvalidAccountPassword):
return status.Error(codes.PermissionDenied, err.Error()) return status.Error(codes.PermissionDenied, err.Error())
+37 -20
View File
@@ -2,6 +2,7 @@ package gameplay_service
import ( import (
"context" "context"
"errors"
"evening_detective_server/internal/modules/cleaner" "evening_detective_server/internal/modules/cleaner"
"evening_detective_server/internal/modules/storytelling" "evening_detective_server/internal/modules/storytelling"
"evening_detective_server/internal/repos/actions_repo" "evening_detective_server/internal/repos/actions_repo"
@@ -10,6 +11,12 @@ import (
"evening_detective_server/internal/repos/teams_repo" "evening_detective_server/internal/repos/teams_repo"
"evening_detective_server/internal/services/game_model" "evening_detective_server/internal/services/game_model"
"evening_detective_server/internal/services/scenarios_service" "evening_detective_server/internal/services/scenarios_service"
"fmt"
)
var (
ErrTeamNotFound = errors.New("Команда не найдена")
ErrGameNotFound = errors.New("Игра не найдена")
) )
type GameplayService struct { type GameplayService struct {
@@ -46,22 +53,22 @@ func NewGameplayService(
} }
func (s *GameplayService) GiveTeamApplications(ctx context.Context, teamID int, name string) error { 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) { 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) team, err := s.teamsRepo.GetTeamByID(ctx, teamID)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, mapGameplayError(err)
} }
if team.Password != password || team.ParticipationStatus == "cancelled" { 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) game, err := s.gamesRepo.GetGameByID(ctx, team.GameID)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, mapGameplayError(err)
} }
if game.Status == "finish" { if game.Status == "finish" {
return &storytelling.Story{}, game_model.MapGame(game, s.domain), nil, nil 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) actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, mapGameplayError(err)
} }
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID) scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, mapGameplayError(err)
} }
codes := make([]string, len(actions)) 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 { func (s *GameplayService) AddTeamAction(ctx context.Context, teamID int, password string, actionCode string, viaMCP bool) error {
team, err := s.teamsRepo.GetTeamByID(ctx, teamID) team, err := s.teamsRepo.GetTeamByID(ctx, teamID)
if err != nil { if err != nil {
return err return mapGameplayError(err)
} }
if team.Password != password || team.ParticipationStatus == "cancelled" { 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) _, err = s.actionsRepo.AddAction(ctx, teamID, actionCode, viaMCP)
if err != nil { if err != nil {
return err return mapGameplayError(err)
} }
if team.ParticipationStatus == "pending" { if team.ParticipationStatus == "pending" {
if err := s.teamsRepo.ConfirmParticipationByFirstAction(ctx, teamID); err != nil { if err := s.teamsRepo.ConfirmParticipationByFirstAction(ctx, teamID); err != nil {
return err return mapGameplayError(err)
} }
} }
game, err := s.gamesRepo.GetGameByID(ctx, team.GameID) game, err := s.gamesRepo.GetGameByID(ctx, team.GameID)
if err != nil { if err != nil {
return err return mapGameplayError(err)
} }
actionDetails, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID) actionDetails, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID)
if err != nil { if err != nil {
return err return mapGameplayError(err)
} }
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID) scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID)
if err != nil { if err != nil {
return err return mapGameplayError(err)
} }
actions := make([]string, len(actionDetails)) 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 := newApplication(application, s.domain)
app.Name = s.cleaner.ClearText(app.Name) app.Name = s.cleaner.ClearText(app.Name)
if _, err := s.applicationsRepo.AddApplication(ctx, teamID, app.Name, app.Image, app.FileType); err != nil { 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 { func (s *GameplayService) DeleteLastTeamAction(ctx context.Context, teamID int) error {
team, err := s.teamsRepo.GetTeamByID(ctx, teamID) team, err := s.teamsRepo.GetTeamByID(ctx, teamID)
if err != nil { if err != nil {
return err return mapGameplayError(err)
} }
game, err := s.gamesRepo.GetGameByID(ctx, team.GameID) game, err := s.gamesRepo.GetGameByID(ctx, team.GameID)
if err != nil { if err != nil {
return err return mapGameplayError(err)
} }
actionDetails, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID) actionDetails, err := s.actionsRepo.GetActionsByTeamID(ctx, teamID)
if err != nil { if err != nil {
return err return mapGameplayError(err)
} }
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID) scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioID)
if err != nil { if err != nil {
return err return mapGameplayError(err)
} }
actions := make([]string, len(actionDetails)) actions := make([]string, len(actionDetails))
@@ -171,9 +178,19 @@ func (s *GameplayService) DeleteLastTeamAction(ctx context.Context, teamID int)
for _, application := range lastPlace.Applications { for _, application := range lastPlace.Applications {
if err := s.applicationsRepo.DeleteApplicationByTeamIDAndName(ctx, teamID, application.Name); err != nil { 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 err != nil {
if errors.Is(err, users_repo.ErrUserNotFound) { if errors.Is(err, users_repo.ErrUserNotFound) {
return "", "", err return "", "", fmt.Errorf("%w: %w", ErrInvalidCredentials, err)
} }
return "", "", err return "", "", err
} }