generated from VLADIMIR/template
add game
This commit is contained in:
@@ -2,10 +2,15 @@ package game_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"evening_detective_server/internal/modules/cleaner"
|
||||
"evening_detective_server/internal/modules/password_generator"
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -13,23 +18,38 @@ 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
|
||||
cleaner cleaner.ICleaner
|
||||
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,
|
||||
passwordGenerator password_generator.IPasswordGenerator,
|
||||
storyteller storytelling.IStory,
|
||||
cleaner cleaner.ICleaner,
|
||||
actionsRepo *actions_repo.ActionsRepo,
|
||||
applicationsRepo *applications_repo.ApplicationsRepo,
|
||||
) *GameService {
|
||||
return &GameService{
|
||||
gamesRepo: gamesRepo,
|
||||
teamsRepo: teamsRepo,
|
||||
scenariosRepo: scenariosRepo,
|
||||
scenarioService: scenarioService,
|
||||
domain: domain,
|
||||
passwordGenerator: passwordGenerator,
|
||||
storyteller: storyteller,
|
||||
cleaner: cleaner,
|
||||
actionsRepo: actionsRepo,
|
||||
applicationsRepo: applicationsRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +85,30 @@ func (s *GameService) GetGame(
|
||||
return nil, err
|
||||
}
|
||||
game.Teams = teams
|
||||
return mapGame(game, s.domain), nil
|
||||
|
||||
res := 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])
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *GameService) DeleteGame(
|
||||
@@ -121,3 +164,74 @@ func (s *GameService) UpdateTeam(ctx context.Context, teamId int, name string) e
|
||||
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 {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
func (s *GameService) GetTeamActions(ctx context.Context, teamId int, password string) (*storytelling.Story, error) {
|
||||
team, err := s.teamsRepo.GetTeamByID(ctx, teamId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if team.Password != password {
|
||||
return nil, teams_repo.ErrTeamNotFound
|
||||
}
|
||||
|
||||
actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, team.ScenarioId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.storyteller.GetStory(scenario.Story, actions), nil
|
||||
}
|
||||
|
||||
func (s *GameService) AddTeamAction(ctx context.Context, teamId int, password string, actionCode string) error {
|
||||
team, err := s.teamsRepo.GetTeamByID(ctx, teamId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if team.Password != password {
|
||||
return teams_repo.ErrTeamNotFound
|
||||
}
|
||||
|
||||
code := s.cleaner.ClearCode(actionCode)
|
||||
_, err = s.actionsRepo.AddAction(ctx, teamId, code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, team.ScenarioId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
teamStory := s.storyteller.GetStory(scenario.Story, actions)
|
||||
|
||||
lastPlace := teamStory.Places[len(teamStory.Places)-1]
|
||||
|
||||
for _, application := range lastPlace.Applications {
|
||||
_, err := s.applicationsRepo.AddApplication(ctx, teamId, application.Name, application.Image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *GameService) DeleteLastTeamAction(ctx context.Context, teamId int) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user