Files
evening_detective_server/internal/services/game_service/service.go
T
2026-10-02 23:40:44 +07:00

193 lines
4.9 KiB
Go

package game_service
import (
"context"
"errors"
"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/game_model"
"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)
}