add team service

This commit is contained in:
2026-09-17 02:29:13 +07:00
parent 49fe193ed7
commit c70d839038
27 changed files with 227 additions and 190 deletions
+35 -83
View File
@@ -4,9 +4,8 @@ import (
"context"
"errors"
"evening_detective_server/internal/modules/cleaner"
"evening_detective_server/internal/modules/password_generator"
"evening_detective_server/internal/modules/game_model"
"evening_detective_server/internal/modules/storytelling"
"evening_detective_server/internal/modules/team_qr_pdf"
"evening_detective_server/internal/repos"
"evening_detective_server/internal/repos/actions_repo"
"evening_detective_server/internal/repos/applications_repo"
@@ -15,24 +14,20 @@ import (
"evening_detective_server/internal/repos/team_answers_repo"
"evening_detective_server/internal/repos/teams_repo"
"evening_detective_server/internal/services/scenarios_service"
"strings"
"time"
)
type GameService struct {
gamesRepo *games_repo.GamesRepo
teamsRepo *teams_repo.TeamsRepo
scenariosRepo *scenarios_repo.ScenariosRepo
scenarioService *scenarios_service.ScenarioService
domain string
passwordGenerator password_generator.IPasswordGenerator
storyteller storytelling.IStory
actionsRepo *actions_repo.ActionsRepo
applicationsRepo *applications_repo.ApplicationsRepo
answersRepo *team_answers_repo.TeamAnswersRepo
cleaner cleaner.ICleaner
teamQRGenerator team_qr_pdf.IGenerator
appBaseURL string
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
answersRepo *team_answers_repo.TeamAnswersRepo
cleaner cleaner.ICleaner
}
func NewGameService(
@@ -41,29 +36,23 @@ func NewGameService(
scenariosRepo *scenarios_repo.ScenariosRepo,
scenarioService *scenarios_service.ScenarioService,
domain string,
passwordGenerator password_generator.IPasswordGenerator,
storyteller storytelling.IStory,
actionsRepo *actions_repo.ActionsRepo,
applicationsRepo *applications_repo.ApplicationsRepo,
answersRepo *team_answers_repo.TeamAnswersRepo,
cleaner cleaner.ICleaner,
teamQRGenerator team_qr_pdf.IGenerator,
appBaseURL string,
) *GameService {
return &GameService{
gamesRepo: gamesRepo,
teamsRepo: teamsRepo,
scenariosRepo: scenariosRepo,
scenarioService: scenarioService,
domain: domain,
passwordGenerator: passwordGenerator,
storyteller: storyteller,
actionsRepo: actionsRepo,
applicationsRepo: applicationsRepo,
answersRepo: answersRepo,
cleaner: cleaner,
teamQRGenerator: teamQRGenerator,
appBaseURL: appBaseURL,
gamesRepo: gamesRepo,
teamsRepo: teamsRepo,
scenariosRepo: scenariosRepo,
scenarioService: scenarioService,
domain: domain,
storyteller: storyteller,
actionsRepo: actionsRepo,
applicationsRepo: applicationsRepo,
answersRepo: answersRepo,
cleaner: cleaner,
}
}
@@ -88,21 +77,21 @@ func (s *GameService) AddGame(
return id, nil
}
func (s *GameService) GetGame(ctx context.Context, id int) (*Game, error) {
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 := mapGame(game, s.domain)
res := game_model.MapGame(game, s.domain)
return res, nil
}
func (s *GameService) GetGameMedia(ctx context.Context, id int) (*Game, error) {
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 := mapGame(game, s.domain)
gameResult := game_model.MapGame(game, s.domain)
gameResult.Scenario, err = s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioId)
if err != nil {
return nil, err
@@ -113,7 +102,7 @@ func (s *GameService) GetGameMedia(ctx context.Context, id int) (*Game, error) {
func (s *GameService) GetFullGame(
ctx context.Context,
id int,
) (*Game, error) {
) (*game_model.Game, error) {
game, err := s.gamesRepo.GetGameByID(ctx, id)
if err != nil {
return nil, err
@@ -129,7 +118,7 @@ func (s *GameService) GetFullGame(
}
game.Teams = teams
res := mapGame(game, s.domain)
res := game_model.MapGame(game, s.domain)
teamIds := make([]int, 0, len(game.Teams))
for _, team := range game.Teams {
@@ -163,12 +152,12 @@ func (s *GameService) DeleteGame(
func (s *GameService) GetGames(
ctx context.Context,
) ([]*Game, error) {
) ([]*game_model.Game, error) {
games, err := s.gamesRepo.GetGames(ctx)
if err != nil {
return nil, err
}
return mapGames(games, s.domain), nil
return game_model.MapGames(games, s.domain), nil
}
func (s *GameService) UpdateGame(
@@ -213,48 +202,11 @@ func (s *GameService) ResetGame(ctx context.Context, id int) error {
return s.gamesRepo.ResetGame(ctx, id)
}
func (s *GameService) AddTeam(
ctx context.Context,
creatorId int,
gameId int,
name string,
captainEmail string,
) (*Team, error) {
captainEmail = strings.TrimSpace(captainEmail)
if captainEmail != "" && !validCaptainEmail(captainEmail) {
return nil, errors.New("Укажите корректную почту капитана")
}
password, err := s.passwordGenerator.Generate()
if err != nil {
return nil, err
}
game, err := s.gamesRepo.GetGameByID(ctx, gameId)
if err != nil {
return nil, err
}
id, err := s.teamsRepo.AddTeamWithParticipation(ctx, creatorId, gameId, name, password, captainEmail, !time.Now().Before(game.StartAt.Add(-24*time.Hour)))
if err != nil {
return nil, err
}
return &Team{
ID: id,
Password: password,
}, nil
}
func (s *GameService) UpdateTeam(ctx context.Context, teamId int, name string) error {
return s.teamsRepo.UpdateTeam(ctx, teamId, name)
}
func (s *GameService) DeleteTeam(ctx context.Context, teamId int) error {
return s.teamsRepo.DeleteTeam(ctx, teamId)
}
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, []actions_repo.Action, error) {
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
@@ -269,7 +221,7 @@ func (s *GameService) GetTeamActions(ctx context.Context, teamId int, password s
return nil, nil, nil, err
}
if game.Status == "finish" {
return &storytelling.Story{}, mapGame(game, s.domain), nil, nil
return &storytelling.Story{}, game_model.MapGame(game, s.domain), nil, nil
}
actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId)
@@ -288,7 +240,7 @@ func (s *GameService) GetTeamActions(ctx context.Context, teamId int, password s
}
story := s.storyteller.GetStory(scenario.Story, codes)
filterTeamStory(story, game.Format)
return story, mapGame(game, s.domain), actions, nil
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 {
@@ -546,7 +498,7 @@ func (s *GameService) GetGameAnswers(ctx context.Context, gameId int) (*GameAnsw
answers = append(answers, AnswerScore{QuestionCode: question.Code})
}
teamAnswers = append(teamAnswers, TeamAnswers{
Team: mapTeam(team),
Team: game_model.MapTeam(team),
Answers: answers,
})
}
@@ -626,9 +578,9 @@ func (s *GameService) GetGameResults(ctx context.Context, gameId int) (*GameResu
Score: row.Score,
}
}
mappedTeams := make([]*Team, 0, len(teams))
mappedTeams := make([]*game_model.Team, 0, len(teams))
for _, team := range teams {
mapped := mapTeam(team)
mapped := game_model.MapTeam(team)
mapped.ActionsCount = actionsCountByTeamIDs[team.ID]
mappedTeams = append(mappedTeams, mapped)
}