This commit is contained in:
2026-03-07 19:23:00 +07:00
parent 6ad47cbc38
commit 856f12f2e3
12 changed files with 116 additions and 177 deletions
@@ -2,9 +2,10 @@ package story_service
import (
"context"
"evening_detective/internal/services/story_service/models"
)
type IStoryStorage interface {
Load(ctx context.Context) (*Story, error)
Save(ctx context.Context, story *Story) error
Load(ctx context.Context) (*models.Story, error)
Save(ctx context.Context, story *models.Story) error
}
@@ -1,4 +1,4 @@
package story_service
package models
type Application struct {
Name string `json:"name"`
@@ -1,4 +1,4 @@
package story_service
package models
type Door struct {
Code string `json:"code"`
@@ -1,4 +1,4 @@
package story_service
package models
type Place struct {
Code string `json:"code"`
@@ -1,4 +1,4 @@
package story_service
package models
type Story struct {
Places []*Place `json:"places"`
+23 -22
View File
@@ -4,6 +4,7 @@ import (
"context"
"evening_detective/internal/modules/cleaner"
"evening_detective/internal/modules/formatter"
"evening_detective/internal/services/story_service/models"
"regexp"
"strings"
)
@@ -11,7 +12,7 @@ import (
type StoryService struct {
cleaner cleaner.ICleaner
formatter formatter.IFormatter
story *Story
story *models.Story
storyStorage IStoryStorage
}
@@ -45,44 +46,44 @@ func (s *StoryService) Update(ctx context.Context) error {
return nil
}
func (s *StoryService) GetPlace(code string) *Place {
func (s *StoryService) GetPlace(code string) *models.Place {
if strings.HasPrefix(code, "[") || strings.HasSuffix(code, "]") {
return NewClientErrorPlace(code)
return models.NewClientErrorPlace(code)
}
clearCode := s.cleaner.ClearCode(code)
for _, place := range s.story.Places {
if s.cleaner.ClearCode(place.Code) == clearCode {
applications := make([]*Application, 0, len(place.Applications))
applications := make([]*models.Application, 0, len(place.Applications))
for _, application := range place.Applications {
name := s.cleaner.ClearText(application.Name)
applications = append(
applications,
&Application{
&models.Application{
Name: name,
},
)
}
return NewPlace(
return models.NewPlace(
place.Code,
place.Name,
s.cleaner.ClearText(place.Text),
WithPlaceApplication(applications...),
WithPlaceHidden(place.Hidden),
WithPlaceDoors(place.Doors...),
models.WithPlaceApplication(applications...),
models.WithPlaceHidden(place.Hidden),
models.WithPlaceDoors(place.Doors...),
)
}
}
return NewNotFoundPlace(code)
return models.NewNotFoundPlace(code)
}
func (s *StoryService) GetPlaces(codes []string) []*Place {
places := make([]*Place, 0, 100)
func (s *StoryService) GetPlaces(codes []string) []*models.Place {
places := make([]*models.Place, 0, 100)
m := map[string]any{}
for _, code := range codes {
place := s.GetPlace(code)
_, ok := m[place.Code]
if place.Hidden && !ok {
place = NewNotFoundPlace(place.Code)
place = models.NewNotFoundPlace(place.Code)
}
places = append(places, place)
for _, door := range place.Doors {
@@ -118,11 +119,11 @@ func (s *StoryService) deletePlace(ctx context.Context, code string) error {
func (s *StoryService) addPlace(ctx context.Context, node *GraphNode) error {
s.story.Places = append(
s.story.Places,
NewPlace(
models.NewPlace(
node.Code,
node.Name,
s.formatter.FormatText(node.Text),
WithPlaceApplication(s.getApplications(node)...),
models.WithPlaceApplication(s.getApplications(node)...),
),
)
return s.Update(ctx)
@@ -131,22 +132,22 @@ func (s *StoryService) addPlace(ctx context.Context, node *GraphNode) error {
func (s *StoryService) updatePlace(ctx context.Context, code string, node *GraphNode) error {
for i := range s.story.Places {
if s.story.Places[i].Code == code {
s.story.Places[i] = NewPlace(
s.story.Places[i] = models.NewPlace(
node.Code,
node.Name,
s.formatter.FormatText(node.Text),
WithPlaceApplication(s.getApplications(node)...),
models.WithPlaceApplication(s.getApplications(node)...),
)
return s.Update(ctx)
}
}
for i := range s.story.Places {
if s.story.Places[i].Code == node.Code {
s.story.Places[i] = NewPlace(
s.story.Places[i] = models.NewPlace(
code,
node.Name,
s.formatter.FormatText(node.Text),
WithPlaceApplication(s.getApplications(node)...),
models.WithPlaceApplication(s.getApplications(node)...),
)
break
}
@@ -154,12 +155,12 @@ func (s *StoryService) updatePlace(ctx context.Context, code string, node *Graph
return s.Update(ctx)
}
func (s *StoryService) getApplications(node *GraphNode) []*Application {
nodeApplications := make([]*Application, 0, len(node.Applications))
func (s *StoryService) getApplications(node *GraphNode) []*models.Application {
nodeApplications := make([]*models.Application, 0, len(node.Applications))
for _, application := range node.Applications {
nodeApplications = append(
nodeApplications,
&Application{
&models.Application{
Name: application.Name,
},
)
+69 -68
View File
@@ -4,6 +4,7 @@ import (
"evening_detective/internal/modules/cleaner"
"evening_detective/internal/modules/formatter"
"evening_detective/internal/services/story_service"
"evening_detective/internal/services/story_service/models"
"evening_detective/internal/services/story_storage"
"testing"
@@ -13,128 +14,128 @@ import (
func TestStoryService_GetPlace(t *testing.T) {
tests := []struct {
name string
story *story_service.Story
story *models.Story
code string
want *story_service.Place
want *models.Place
}{
{
name: "не корректный ввода",
story: &story_service.Story{},
story: &models.Story{},
code: "[Ы]",
want: story_service.NewClientErrorPlace("[Ы]"),
want: models.NewClientErrorPlace("[Ы]"),
},
{
name: "точка не найдена",
story: &story_service.Story{},
story: &models.Story{},
code: "Ы",
want: story_service.NewNotFoundPlace("Ы"),
want: models.NewNotFoundPlace("Ы"),
},
{
name: "получение точки",
story: &story_service.Story{
Places: []*story_service.Place{
story_service.NewPlace("Ы", "Название", "Текст"),
story: &models.Story{
Places: []*models.Place{
models.NewPlace("Ы", "Название", "Текст"),
},
},
code: "Ы",
want: story_service.NewPlace("Ы", "Название", "Текст"),
want: models.NewPlace("Ы", "Название", "Текст"),
},
{
name: "получение скрытой точки",
story: &story_service.Story{
Places: []*story_service.Place{
story_service.NewPlace(
story: &models.Story{
Places: []*models.Place{
models.NewPlace(
"Ы",
"Название",
"Текст",
story_service.WithPlaceHidden(true),
models.WithPlaceHidden(true),
),
},
},
code: "Ы",
want: story_service.NewPlace(
want: models.NewPlace(
"Ы",
"Название",
"Текст",
story_service.WithPlaceHidden(true),
models.WithPlaceHidden(true),
),
},
{
name: "получение точки с приложением",
story: &story_service.Story{
Places: []*story_service.Place{
story_service.NewPlace(
story: &models.Story{
Places: []*models.Place{
models.NewPlace(
"Ы",
"Название",
"Текст",
story_service.WithPlaceApplication(
story_service.NewApplication("Приложение"),
models.WithPlaceApplication(
models.NewApplication("Приложение"),
),
),
},
},
code: "Ы",
want: story_service.NewPlace(
want: models.NewPlace(
"Ы",
"Название",
"Текст",
story_service.WithPlaceApplication(
story_service.NewApplication("Приложение"),
models.WithPlaceApplication(
models.NewApplication("Приложение"),
),
),
},
{
name: "получение точки с проходом",
story: &story_service.Story{
Places: []*story_service.Place{
story_service.NewPlace(
story: &models.Story{
Places: []*models.Place{
models.NewPlace(
"Ы",
"Название",
"Текст",
story_service.WithPlaceDoors(
story_service.NewDoor("Й", "Приложение"),
models.WithPlaceDoors(
models.NewDoor("Й", "Приложение"),
),
),
},
},
code: "Ы",
want: story_service.NewPlace(
want: models.NewPlace(
"Ы",
"Название",
"Текст",
story_service.WithPlaceDoors(
story_service.NewDoor("Й", "Приложение"),
models.WithPlaceDoors(
models.NewDoor("Й", "Приложение"),
),
),
},
{
name: "получение точки с диалогом",
story: &story_service.Story{
Places: []*story_service.Place{
story_service.NewPlace(
story: &models.Story{
Places: []*models.Place{
models.NewPlace(
"Ы",
"Название",
"Текст",
story_service.WithPlaceDoors(
story_service.NewDoor(
models.WithPlaceDoors(
models.NewDoor(
"Й",
"Приложение",
story_service.WithDoorShow(true),
models.WithDoorShow(true),
),
),
),
},
},
code: "Ы",
want: story_service.NewPlace(
want: models.NewPlace(
"Ы",
"Название",
"Текст",
story_service.WithPlaceDoors(
story_service.NewDoor(
models.WithPlaceDoors(
models.NewDoor(
"Й",
"Приложение",
story_service.WithDoorShow(true),
models.WithDoorShow(true),
),
),
),
@@ -159,74 +160,74 @@ func TestStoryService_GetPlace(t *testing.T) {
func TestStoryService_GetPlaces(t *testing.T) {
tests := []struct {
name string
story *story_service.Story
story *models.Story
codes []string
want []*story_service.Place
want []*models.Place
}{
{
name: "Можно сходить в открытую точку",
story: &story_service.Story{
Places: []*story_service.Place{
story_service.NewPlace("Ы", "Название", "Текст"),
story: &models.Story{
Places: []*models.Place{
models.NewPlace("Ы", "Название", "Текст"),
},
},
codes: []string{"Ы"},
want: []*story_service.Place{
story_service.NewPlace("Ы", "Название", "Текст"),
want: []*models.Place{
models.NewPlace("Ы", "Название", "Текст"),
},
},
{
name: "Нельзя открыть скрытую точку",
story: &story_service.Story{
Places: []*story_service.Place{
story_service.NewPlace(
story: &models.Story{
Places: []*models.Place{
models.NewPlace(
"Ы",
"Название",
"Текст",
story_service.WithPlaceHidden(true),
models.WithPlaceHidden(true),
),
},
},
codes: []string{"Ы"},
want: []*story_service.Place{
story_service.NewNotFoundPlace("Ы"),
want: []*models.Place{
models.NewNotFoundPlace("Ы"),
},
},
{
name: "Открываем скрытую точку",
story: &story_service.Story{
Places: []*story_service.Place{
story_service.NewPlace(
story: &models.Story{
Places: []*models.Place{
models.NewPlace(
"Ы-1",
"Название",
"Текст",
story_service.WithPlaceDoors(
story_service.NewDoor("Ы-2", "Название"),
models.WithPlaceDoors(
models.NewDoor("Ы-2", "Название"),
),
),
story_service.NewPlace(
models.NewPlace(
"Ы-2",
"Название",
"Текст",
story_service.WithPlaceHidden(true),
models.WithPlaceHidden(true),
),
},
},
codes: []string{"Ы-1", "Ы-2"},
want: []*story_service.Place{
story_service.NewPlace(
want: []*models.Place{
models.NewPlace(
"Ы-1",
"Название",
"Текст",
story_service.WithPlaceDoors(
story_service.NewDoor("Ы-2", "Название"),
models.WithPlaceDoors(
models.NewDoor("Ы-2", "Название"),
),
),
story_service.NewPlace(
models.NewPlace(
"Ы-2",
"Название",
"Текст",
story_service.WithPlaceHidden(true),
models.WithPlaceHidden(true),
),
},
},