package game_service import ( "context" "errors" "evening_detective_server/internal/modules/game_model" "evening_detective_server/internal/repos/actions_repo" "evening_detective_server/internal/repos/applications_repo" "evening_detective_server/internal/repos/games_repo" "evening_detective_server/internal/repos/scenarios_repo" "evening_detective_server/internal/repos/teams_repo" "evening_detective_server/internal/services/scenarios_service" "time" ) type GameService struct { gamesRepo *games_repo.GamesRepo teamsRepo *teams_repo.TeamsRepo scenariosRepo *scenarios_repo.ScenariosRepo scenarioService *scenarios_service.ScenarioService domain string actionsRepo *actions_repo.ActionsRepo applicationsRepo *applications_repo.ApplicationsRepo } func NewGameService( gamesRepo *games_repo.GamesRepo, teamsRepo *teams_repo.TeamsRepo, scenariosRepo *scenarios_repo.ScenariosRepo, scenarioService *scenarios_service.ScenarioService, domain string, actionsRepo *actions_repo.ActionsRepo, applicationsRepo *applications_repo.ApplicationsRepo, ) *GameService { return &GameService{ gamesRepo: gamesRepo, teamsRepo: teamsRepo, scenariosRepo: scenariosRepo, scenarioService: scenarioService, domain: domain, actionsRepo: actionsRepo, applicationsRepo: applicationsRepo, } } func (s *GameService) AddGame( ctx context.Context, name string, description string, location *string, startAt time.Time, scenarioId int32, format string, ) (int, error) { if format == "" { format = "online" } if format != "online" && format != "offline" { return 0, errors.New("Формат игры должен быть online или offline") } id, err := s.gamesRepo.AddGame(ctx, name, description, location, startAt, scenarioId, format) if err != nil { return 0, err } return id, nil } func (s *GameService) GetGame(ctx context.Context, id int) (*game_model.Game, error) { game, err := s.gamesRepo.GetGameByID(ctx, id) if err != nil { return nil, err } res := game_model.MapGame(game, s.domain) return res, nil } func (s *GameService) GetGameMedia(ctx context.Context, id int) (*game_model.Game, error) { game, err := s.gamesRepo.GetGameByID(ctx, id) if err != nil { return nil, err } gameResult := game_model.MapGame(game, s.domain) gameResult.Scenario, err = s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioId) if err != nil { return nil, err } return gameResult, nil } func (s *GameService) GetFullGame( ctx context.Context, id int, ) (*game_model.Game, error) { game, err := s.gamesRepo.GetGameByID(ctx, id) if err != nil { return nil, err } scenario, err := s.scenariosRepo.GetScenarioByID(ctx, game.ScenarioId) if err != nil { return nil, err } game.Scenario = scenario teams, err := s.teamsRepo.GetTeamsByGameID(ctx, id) if err != nil { return nil, err } game.Teams = teams res := game_model.MapGame(game, s.domain) teamIds := make([]int, 0, len(game.Teams)) for _, team := range game.Teams { teamIds = append(teamIds, team.ID) } actionsCountByTeamIDs, err := s.actionsRepo.GetActionsCountByTeamIDs(ctx, teamIds) if err != nil { return nil, err } needApplicationsByTeamIDs, err := s.applicationsRepo.GetApplicationsByTeamIDsAndState(ctx, teamIds, "need") if err != nil { return nil, err } for _, team := range res.Teams { team.ActionsCount = actionsCountByTeamIDs[team.ID] team.Applications = mapApplications(needApplicationsByTeamIDs[team.ID], s.domain) } return res, nil } func (s *GameService) DeleteGame( ctx context.Context, id int, ) error { return s.gamesRepo.DeleteGameByID(ctx, id) } func (s *GameService) GetGames( ctx context.Context, ) ([]*game_model.Game, error) { games, err := s.gamesRepo.GetGames(ctx) if err != nil { return nil, err } return game_model.MapGames(games, s.domain), nil } func (s *GameService) UpdateGame( ctx context.Context, id int, name string, description string, location *string, startAt time.Time, scenarioId int32, format string, ) error { if format == "" { game, err := s.gamesRepo.GetGameByID(ctx, id) if err != nil { return err } format = game.Format } if format != "online" && format != "offline" { return errors.New("Формат игры должен быть online или offline") } return s.gamesRepo.UpdateGame(ctx, id, name, description, location, startAt, scenarioId, format) } func (s *GameService) StartGame(ctx context.Context, id int) error { return s.gamesRepo.StartGame(ctx, id) } func (s *GameService) PauseGame(ctx context.Context, id int) error { return s.gamesRepo.PauseGame(ctx, id) } func (s *GameService) PlayGame(ctx context.Context, id int) error { return s.gamesRepo.PlayGame(ctx, id) } func (s *GameService) FinishGame(ctx context.Context, id int) error { return s.gamesRepo.FinishGame(ctx, id) } func (s *GameService) ResetGame(ctx context.Context, id int) error { return s.gamesRepo.ResetGame(ctx, id) }