Files
evening_detective_server/internal/services/game_service/service.go
T
2026-09-17 02:38:05 +07:00

332 lines
8.7 KiB
Go

package game_service
import (
"context"
"errors"
"evening_detective_server/internal/modules/cleaner"
"evening_detective_server/internal/modules/game_model"
"evening_detective_server/internal/modules/storytelling"
"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
storyteller storytelling.IStory
actionsRepo *actions_repo.ActionsRepo
applicationsRepo *applications_repo.ApplicationsRepo
cleaner cleaner.ICleaner
}
func NewGameService(
gamesRepo *games_repo.GamesRepo,
teamsRepo *teams_repo.TeamsRepo,
scenariosRepo *scenarios_repo.ScenariosRepo,
scenarioService *scenarios_service.ScenarioService,
domain string,
storyteller storytelling.IStory,
actionsRepo *actions_repo.ActionsRepo,
applicationsRepo *applications_repo.ApplicationsRepo,
cleaner cleaner.ICleaner,
) *GameService {
return &GameService{
gamesRepo: gamesRepo,
teamsRepo: teamsRepo,
scenariosRepo: scenariosRepo,
scenarioService: scenarioService,
domain: domain,
storyteller: storyteller,
actionsRepo: actionsRepo,
applicationsRepo: applicationsRepo,
cleaner: cleaner,
}
}
func (s *GameService) AddGame(
ctx context.Context,
name string,
description 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, 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,
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, 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)
}
func (s *GameService) GiveTeamApplications(ctx context.Context, teamId int, name string) error {
return s.applicationsRepo.UpdateApplicationStatus(ctx, teamId, name, "done")
}
func (s *GameService) 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
}
if team.Password != password || team.ParticipationStatus == "cancelled" {
return nil, nil, nil, teams_repo.ErrTeamNotFound
}
game, err := s.gamesRepo.GetGameByID(ctx, team.GameId)
if err != nil {
return nil, nil, nil, err
}
if game.Status == "finish" {
return &storytelling.Story{}, game_model.MapGame(game, s.domain), nil, nil
}
actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId)
if err != nil {
return nil, nil, nil, err
}
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioId)
if err != nil {
return nil, nil, nil, err
}
codes := make([]string, len(actions))
for i, action := range actions {
codes[i] = action.Code
}
story := s.storyteller.GetStory(scenario.Story, codes)
filterTeamStory(story, game.Format)
return story, game_model.MapGame(game, s.domain), actions, nil
}
func (s *GameService) 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
}
if team.Password != password || team.ParticipationStatus == "cancelled" {
return teams_repo.ErrTeamNotFound
}
_, err = s.actionsRepo.AddAction(ctx, teamId, actionCode, viaMCP)
if err != nil {
return err
}
if team.ParticipationStatus == "pending" {
if err := s.teamsRepo.ConfirmParticipationByFirstAction(ctx, teamId); err != nil {
return err
}
}
game, err := s.gamesRepo.GetGameByID(ctx, team.GameId)
if err != nil {
return err
}
actionDetails, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId)
if err != nil {
return err
}
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioId)
if err != nil {
return err
}
actions := make([]string, len(actionDetails))
for i, action := range actionDetails {
actions[i] = action.Code
}
teamStory := s.storyteller.GetStory(scenario.Story, actions)
lastPlace := teamStory.Places[len(teamStory.Places)-1]
for _, application := range lastPlace.Applications {
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 nil
}
func (s *GameService) DeleteLastTeamAction(ctx context.Context, teamId int) error {
team, err := s.teamsRepo.GetTeamByID(ctx, teamId)
if err != nil {
return err
}
game, err := s.gamesRepo.GetGameByID(ctx, team.GameId)
if err != nil {
return err
}
actionDetails, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId)
if err != nil {
return err
}
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioId)
if err != nil {
return err
}
actions := make([]string, len(actionDetails))
for i, action := range actionDetails {
actions[i] = action.Code
}
teamStory := s.storyteller.GetStory(scenario.Story, actions)
lastPlace := teamStory.Places[len(teamStory.Places)-1]
for _, application := range lastPlace.Applications {
if err := s.applicationsRepo.DeleteApplicationByTeamIdAndName(ctx, teamId, application.Name); err != nil {
return err
}
}
return s.actionsRepo.DeleteLastActionByTeamId(ctx, teamId)
}