generated from VLADIMIR/template
487 lines
14 KiB
Go
487 lines
14 KiB
Go
package game_service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"evening_detective_server/internal/modules/cleaner"
|
|
"evening_detective_server/internal/modules/password_generator"
|
|
"evening_detective_server/internal/modules/storytelling"
|
|
"evening_detective_server/internal/repos"
|
|
"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/team_answers_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
|
|
passwordGenerator password_generator.IPasswordGenerator
|
|
storyteller storytelling.IStory
|
|
actionsRepo *actions_repo.ActionsRepo
|
|
applicationsRepo *applications_repo.ApplicationsRepo
|
|
answersRepo *team_answers_repo.TeamAnswersRepo
|
|
cleaner cleaner.ICleaner
|
|
}
|
|
|
|
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,
|
|
actionsRepo *actions_repo.ActionsRepo,
|
|
applicationsRepo *applications_repo.ApplicationsRepo,
|
|
answersRepo *team_answers_repo.TeamAnswersRepo,
|
|
cleaner cleaner.ICleaner,
|
|
) *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,
|
|
}
|
|
}
|
|
|
|
func (s *GameService) AddGame(
|
|
ctx context.Context,
|
|
name string,
|
|
description string,
|
|
startAt time.Time,
|
|
scenarioId int32,
|
|
) (int, error) {
|
|
id, err := s.gamesRepo.AddGame(ctx, name, description, startAt, scenarioId)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
func (s *GameService) GetGame(ctx context.Context, id int) (*Game, error) {
|
|
game, err := s.gamesRepo.GetGameByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res := mapGame(game, s.domain)
|
|
return res, nil
|
|
}
|
|
|
|
func (s *GameService) GetFullGame(
|
|
ctx context.Context,
|
|
id int,
|
|
) (*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 := 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, error) {
|
|
games, err := s.gamesRepo.GetGames(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return mapGames(games, s.domain), nil
|
|
}
|
|
|
|
func (s *GameService) UpdateGame(
|
|
ctx context.Context,
|
|
id int,
|
|
name string,
|
|
description string,
|
|
startAt time.Time,
|
|
scenarioId int32,
|
|
) error {
|
|
return s.gamesRepo.UpdateGame(ctx, id, name, description, startAt, scenarioId)
|
|
}
|
|
|
|
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) AddTeam(
|
|
ctx context.Context,
|
|
creatorId int,
|
|
gameId int,
|
|
name string,
|
|
) (*Team, error) {
|
|
password, err := s.passwordGenerator.Generate()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
id, err := s.teamsRepo.AddTeam(ctx, creatorId, gameId, name, password)
|
|
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, error) {
|
|
team, err := s.teamsRepo.GetTeamByID(ctx, teamId)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
if team.Password != password {
|
|
return nil, nil, teams_repo.ErrTeamNotFound
|
|
}
|
|
|
|
game, err := s.gamesRepo.GetGameByID(ctx, team.GameId)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.ScenarioId)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return s.storyteller.GetStory(scenario.Story, actions), mapGame(game, s.domain), 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
|
|
}
|
|
|
|
_, err = s.actionsRepo.AddAction(ctx, teamId, actionCode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
game, err := s.gamesRepo.GetGameByID(ctx, team.GameId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.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 {
|
|
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
|
|
}
|
|
|
|
actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, game.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 {
|
|
if err := s.applicationsRepo.DeleteApplicationByTeamIdAndName(ctx, teamId, application.Name); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return s.actionsRepo.DeleteLastActionByTeamId(ctx, teamId)
|
|
}
|
|
|
|
var (
|
|
// ErrGameFinished — после завершения игры ответы команд больше нельзя
|
|
// изменять: организатор оценивает их в панели игры.
|
|
ErrGameFinished = errors.New("Игра завершена, ответы больше нельзя изменять")
|
|
// ErrGameNotStarted — игра ещё не начата, отвечать на вопросы нельзя.
|
|
ErrGameNotStarted = errors.New("Игра не начата, отвечать на вопросы нельзя")
|
|
// ErrQuestionNotFound — вопрос с таким кодом отсутствует в сценарии игры.
|
|
ErrQuestionNotFound = errors.New("Вопрос не найден")
|
|
)
|
|
|
|
// getTeamAndGame проверяет команду по паролю и возвращает её вместе с игрой.
|
|
func (s *GameService) getTeamAndGame(ctx context.Context, teamId int, password string) (*repos.Team, *repos.Game, error) {
|
|
team, err := s.teamsRepo.GetTeamByID(ctx, teamId)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if team.Password != password {
|
|
return nil, nil, teams_repo.ErrTeamNotFound
|
|
}
|
|
game, err := s.gamesRepo.GetGameByID(ctx, team.GameId)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return team, game, nil
|
|
}
|
|
|
|
// getScenarioQuestionsByCode возвращает вопросы сценария игры с индексацией по коду.
|
|
func (s *GameService) getScenarioQuestionsByCode(ctx context.Context, scenarioId int) ([]*storytelling.Question, map[string]*storytelling.Question, error) {
|
|
scenario, err := s.scenarioService.GetFullScenarioByID(ctx, scenarioId)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
byCode := make(map[string]*storytelling.Question, len(scenario.Story.Questions))
|
|
for _, question := range scenario.Story.Questions {
|
|
byCode[question.Code] = question
|
|
}
|
|
return scenario.Story.Questions, byCode, nil
|
|
}
|
|
|
|
// GetTeamAnswers возвращает ответы команды на вопросы дела (без баллов:
|
|
// баллы видны только организаторам). Ответы на удалённые из сценария
|
|
// вопросы не возвращаются.
|
|
func (s *GameService) GetTeamAnswers(ctx context.Context, teamId int, password string) ([]Answer, error) {
|
|
_, game, err := s.getTeamAndGame(ctx, teamId, password)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, byCode, err := s.getScenarioQuestionsByCode(ctx, game.ScenarioId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
teamAnswers, err := s.answersRepo.GetAnswersByTeamID(ctx, teamId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
answers := make([]Answer, 0, len(teamAnswers))
|
|
for _, teamAnswer := range teamAnswers {
|
|
if _, ok := byCode[teamAnswer.QuestionCode]; !ok {
|
|
continue
|
|
}
|
|
answers = append(answers, Answer{
|
|
QuestionCode: teamAnswer.QuestionCode,
|
|
Text: teamAnswer.Text,
|
|
})
|
|
}
|
|
return answers, nil
|
|
}
|
|
|
|
// SubmitTeamAnswers сохраняет ответы команды на вопросы дела (upsert).
|
|
// Доступно только пока игра идёт (статусы run/pause): после завершения
|
|
// ответы фиксируются для оценки организатором. Переданные коды должны
|
|
// существовать в сценарии игры; пустая строка очищает ответ.
|
|
func (s *GameService) SubmitTeamAnswers(ctx context.Context, teamId int, password string, answers []Answer) error {
|
|
_, game, err := s.getTeamAndGame(ctx, teamId, password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch game.Status {
|
|
case "run", "pause":
|
|
// игра идёт — можно отвечать
|
|
default:
|
|
if game.Status == "finish" {
|
|
return ErrGameFinished
|
|
}
|
|
return ErrGameNotStarted
|
|
}
|
|
_, byCode, err := s.getScenarioQuestionsByCode(ctx, game.ScenarioId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rows := make([]repos.TeamAnswer, 0, len(answers))
|
|
for _, answer := range answers {
|
|
if _, ok := byCode[answer.QuestionCode]; !ok {
|
|
return ErrQuestionNotFound
|
|
}
|
|
rows = append(rows, repos.TeamAnswer{
|
|
QuestionCode: answer.QuestionCode,
|
|
Text: answer.Text,
|
|
})
|
|
}
|
|
return s.answersRepo.UpsertAnswers(ctx, teamId, rows)
|
|
}
|
|
|
|
// GetGameAnswers возвращает полную картину для оценки организатором:
|
|
// вопросы сценария с правильными ответами и ответы всех команд с баллами.
|
|
// Ответы на удалённые из сценария вопросы не возвращаются (как в
|
|
// GetTeamAnswers).
|
|
func (s *GameService) GetGameAnswers(ctx context.Context, gameId int) (*GameAnswers, error) {
|
|
game, err := s.gamesRepo.GetGameByID(ctx, gameId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
questions, byCode, err := s.getScenarioQuestionsByCode(ctx, game.ScenarioId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
teams, err := s.teamsRepo.GetTeamsByGameID(ctx, gameId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rows, err := s.answersRepo.GetAnswersByGameID(ctx, gameId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
answersByTeam := make(map[int][]AnswerScore, len(teams))
|
|
for _, row := range rows {
|
|
if _, ok := byCode[row.QuestionCode]; !ok {
|
|
continue
|
|
}
|
|
score := row.Score
|
|
answersByTeam[row.TeamId] = append(answersByTeam[row.TeamId], AnswerScore{
|
|
QuestionCode: row.QuestionCode,
|
|
Text: row.Text,
|
|
Score: score,
|
|
})
|
|
}
|
|
teamAnswers := make([]TeamAnswers, 0, len(teams))
|
|
for _, team := range teams {
|
|
teamAnswers = append(teamAnswers, TeamAnswers{
|
|
Team: mapTeam(team),
|
|
Answers: answersByTeam[team.ID],
|
|
})
|
|
}
|
|
return &GameAnswers{
|
|
Questions: questions,
|
|
Teams: teamAnswers,
|
|
}, nil
|
|
}
|
|
|
|
// SetTeamAnswerScore выставляет балл команде за ответ на вопрос дела;
|
|
// score == nil сбрасывает балл. Балл может быть любым целым числом
|
|
// (в т.ч. отрицательным — штрафные баллы). Вопрос должен существовать
|
|
// в сценарии игры.
|
|
func (s *GameService) SetTeamAnswerScore(ctx context.Context, teamId int, questionCode string, score *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
|
|
}
|
|
_, byCode, err := s.getScenarioQuestionsByCode(ctx, game.ScenarioId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, ok := byCode[questionCode]; !ok {
|
|
return ErrQuestionNotFound
|
|
}
|
|
return s.answersRepo.SetAnswerScore(ctx, teamId, questionCode, score)
|
|
}
|