generated from VLADIMIR/template
clear
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
package story_service
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"evening_detective/internal/modules/cleaner"
|
||||
"evening_detective/internal/modules/formatter"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
@@ -12,12 +13,22 @@ import (
|
||||
)
|
||||
|
||||
type StoryService struct {
|
||||
filepath string
|
||||
story *Story
|
||||
cleaner cleaner.ICleaner
|
||||
formatter formatter.IFormatter
|
||||
filepath string
|
||||
story *Story
|
||||
}
|
||||
|
||||
func NewStoryService(filepath string) (*StoryService, error) {
|
||||
s := &StoryService{filepath: filepath}
|
||||
func NewStoryService(
|
||||
cleaner cleaner.ICleaner,
|
||||
formatter formatter.IFormatter,
|
||||
filepath string,
|
||||
) (*StoryService, error) {
|
||||
s := &StoryService{
|
||||
cleaner: cleaner,
|
||||
formatter: formatter,
|
||||
filepath: filepath,
|
||||
}
|
||||
if err := s.Load(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -66,9 +77,9 @@ func (s *StoryService) GetPlace(code string) *Place {
|
||||
Text: "Уважаемые детективы внимательно прочитайте правила.",
|
||||
}
|
||||
}
|
||||
code = clearCode(code)
|
||||
clearCode := s.cleaner.ClearCode(code)
|
||||
for _, place := range s.story.Places {
|
||||
if clearCode(place.Code) == code {
|
||||
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 {
|
||||
@@ -122,7 +133,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error {
|
||||
&Place{
|
||||
Code: node.Code,
|
||||
Name: node.Name,
|
||||
Text: formatText(node.Text),
|
||||
Text: s.formatter.FormatText(node.Text),
|
||||
Applications: nodeApplications,
|
||||
},
|
||||
)
|
||||
@@ -138,7 +149,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error {
|
||||
s.story.Places[i] = &Place{
|
||||
Code: node.Code,
|
||||
Name: node.Name,
|
||||
Text: formatText(node.Text),
|
||||
Text: s.formatter.FormatText(node.Text),
|
||||
Applications: nodeApplications,
|
||||
}
|
||||
update = true
|
||||
@@ -151,7 +162,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error {
|
||||
s.story.Places[i] = &Place{
|
||||
Code: code,
|
||||
Name: node.Name,
|
||||
Text: formatText(node.Text),
|
||||
Text: s.formatter.FormatText(node.Text),
|
||||
Applications: nodeApplications,
|
||||
}
|
||||
update = true
|
||||
@@ -167,7 +178,7 @@ 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[clearCode(place.Code)] = place.Code
|
||||
m[s.cleaner.ClearCode(place.Code)] = place.Code
|
||||
applications := make([]*GraphApplication, 0, len(place.Applications))
|
||||
for _, application := range place.Applications {
|
||||
applications = append(
|
||||
@@ -196,8 +207,8 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
||||
edges = append(
|
||||
edges,
|
||||
&GraphEdge{
|
||||
From: m[clearCode(place.Code)],
|
||||
To: m[clearMatch(match)],
|
||||
From: m[s.cleaner.ClearCode(place.Code)],
|
||||
To: m[s.cleaner.ClearCode(match)],
|
||||
Type: "node",
|
||||
},
|
||||
)
|
||||
@@ -209,8 +220,8 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
||||
edges = append(
|
||||
edges,
|
||||
&GraphEdge{
|
||||
From: m[clearCode(place.Code)],
|
||||
To: m[clearMatch(match)],
|
||||
From: m[s.cleaner.ClearCode(place.Code)],
|
||||
To: m[s.cleaner.ClearCode(match)],
|
||||
Type: "application",
|
||||
},
|
||||
)
|
||||
@@ -223,30 +234,3 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
||||
Edges: edges,
|
||||
}
|
||||
}
|
||||
|
||||
func formatText(text string) string {
|
||||
scanner := bufio.NewScanner(strings.NewReader(text))
|
||||
|
||||
scanner.Split(bufio.ScanLines)
|
||||
|
||||
lines := []string{}
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
res := ""
|
||||
for i, line := range lines {
|
||||
l := strings.TrimSpace(line)
|
||||
if i == 0 && strings.HasPrefix(l, "—") {
|
||||
res += " "
|
||||
}
|
||||
if i > 0 {
|
||||
res += "\n"
|
||||
if len(l) > 0 {
|
||||
res += " "
|
||||
}
|
||||
}
|
||||
res += l
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user