generated from VLADIMIR/template
213 lines
5.0 KiB
Go
213 lines
5.0 KiB
Go
package story_service
|
||
|
||
import (
|
||
"context"
|
||
"evening_detective/internal/modules/cleaner"
|
||
"evening_detective/internal/modules/formatter"
|
||
"regexp"
|
||
"strings"
|
||
)
|
||
|
||
type StoryService struct {
|
||
cleaner cleaner.ICleaner
|
||
formatter formatter.IFormatter
|
||
story *Story
|
||
storyStorage IStoryStorage
|
||
}
|
||
|
||
func NewStoryService(
|
||
cleaner cleaner.ICleaner,
|
||
formatter formatter.IFormatter,
|
||
storyStorage IStoryStorage,
|
||
) (*StoryService, error) {
|
||
s := &StoryService{
|
||
cleaner: cleaner,
|
||
formatter: formatter,
|
||
storyStorage: storyStorage,
|
||
}
|
||
story, err := s.storyStorage.Load(context.Background())
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
s.story = story
|
||
return s, nil
|
||
}
|
||
|
||
func (s *StoryService) Update(ctx context.Context) error {
|
||
if err := s.storyStorage.Save(ctx, s.story); err != nil {
|
||
return err
|
||
}
|
||
story, err := s.storyStorage.Load(ctx)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
s.story = story
|
||
return nil
|
||
}
|
||
|
||
func (s *StoryService) GetPlace(code string) *Place {
|
||
if strings.HasPrefix(code, "[") || strings.HasSuffix(code, "]") {
|
||
return &Place{
|
||
Code: code,
|
||
Name: "Не найдено",
|
||
Text: "Уважаемые детективы внимательно прочитайте правила.",
|
||
}
|
||
}
|
||
clearCode := s.cleaner.ClearCode(code)
|
||
for _, place := range s.story.Places {
|
||
if s.cleaner.ClearCode(place.Code) == clearCode {
|
||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||
applications := make([]*Application, 0, len(place.Applications))
|
||
for _, application := range place.Applications {
|
||
name := re.ReplaceAllString(application.Name, "")
|
||
applications = append(
|
||
applications,
|
||
&Application{
|
||
Name: name,
|
||
},
|
||
)
|
||
}
|
||
text := re.ReplaceAllString(place.Text, "")
|
||
return &Place{
|
||
Code: place.Code,
|
||
Name: place.Name,
|
||
Text: text,
|
||
Applications: applications,
|
||
}
|
||
}
|
||
}
|
||
return &Place{
|
||
Code: code,
|
||
Name: "Не найдено",
|
||
Text: "Такой точки не существует.",
|
||
}
|
||
}
|
||
|
||
func (s *StoryService) UpdatePlace(ctx context.Context, code string, node *GraphNode) error {
|
||
if code != "" && node.Code == "" {
|
||
for i := range s.story.Places {
|
||
if s.story.Places[i].Code == code {
|
||
s.story.Places = append(s.story.Places[:i], s.story.Places[i+1:]...)
|
||
break
|
||
}
|
||
}
|
||
s.Update(ctx)
|
||
return nil
|
||
}
|
||
nodeApplications := make([]*Application, 0, len(node.Applications))
|
||
for _, application := range node.Applications {
|
||
nodeApplications = append(
|
||
nodeApplications,
|
||
&Application{
|
||
Name: application.Name,
|
||
},
|
||
)
|
||
}
|
||
if code == "" && node.Code != "" {
|
||
s.story.Places = append(
|
||
s.story.Places,
|
||
&Place{
|
||
Code: node.Code,
|
||
Name: node.Name,
|
||
Text: s.formatter.FormatText(node.Text),
|
||
Applications: nodeApplications,
|
||
},
|
||
)
|
||
s.Update(ctx)
|
||
return nil
|
||
}
|
||
if code == "" || node.Code == "" {
|
||
return nil
|
||
}
|
||
update := false
|
||
for i := range s.story.Places {
|
||
if s.story.Places[i].Code == code {
|
||
s.story.Places[i] = &Place{
|
||
Code: node.Code,
|
||
Name: node.Name,
|
||
Text: s.formatter.FormatText(node.Text),
|
||
Applications: nodeApplications,
|
||
}
|
||
update = true
|
||
break
|
||
}
|
||
}
|
||
if !update {
|
||
for i := range s.story.Places {
|
||
if s.story.Places[i].Code == node.Code {
|
||
s.story.Places[i] = &Place{
|
||
Code: code,
|
||
Name: node.Name,
|
||
Text: s.formatter.FormatText(node.Text),
|
||
Applications: nodeApplications,
|
||
}
|
||
update = true
|
||
break
|
||
}
|
||
}
|
||
}
|
||
s.Update(ctx)
|
||
return nil
|
||
}
|
||
|
||
func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
||
m := make(map[string]string, len(s.story.Places))
|
||
nodes := make([]*GraphNode, 0, len(s.story.Places))
|
||
for _, place := range s.story.Places {
|
||
m[s.cleaner.ClearCode(place.Code)] = place.Code
|
||
applications := make([]*GraphApplication, 0, len(place.Applications))
|
||
for _, application := range place.Applications {
|
||
applications = append(
|
||
applications,
|
||
&GraphApplication{
|
||
Name: application.Name,
|
||
},
|
||
)
|
||
}
|
||
nodes = append(
|
||
nodes, &GraphNode{
|
||
Code: place.Code,
|
||
Name: place.Name,
|
||
Text: place.Text,
|
||
Applications: applications,
|
||
},
|
||
)
|
||
}
|
||
|
||
edges := make([]*GraphEdge, 0, len(s.story.Places)*3)
|
||
for _, place := range s.story.Places {
|
||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||
matches := re.FindAllString(place.Text, -1)
|
||
|
||
for _, match := range matches {
|
||
edges = append(
|
||
edges,
|
||
&GraphEdge{
|
||
From: m[s.cleaner.ClearCode(place.Code)],
|
||
To: m[s.cleaner.ClearCode(match)],
|
||
Type: "node",
|
||
},
|
||
)
|
||
}
|
||
for _, application := range place.Applications {
|
||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||
matches := re.FindAllString(application.Name, -1)
|
||
for _, match := range matches {
|
||
edges = append(
|
||
edges,
|
||
&GraphEdge{
|
||
From: m[s.cleaner.ClearCode(place.Code)],
|
||
To: m[s.cleaner.ClearCode(match)],
|
||
Type: "application",
|
||
},
|
||
)
|
||
}
|
||
}
|
||
}
|
||
|
||
return &Graph{
|
||
Nodes: nodes,
|
||
Edges: edges,
|
||
}
|
||
}
|