This commit is contained in:
Владимир Фёдоров 2026-03-02 02:25:22 +07:00
parent caaed14ebc
commit 5ab7ae0fcd
6 changed files with 94 additions and 51 deletions

View File

@ -5,6 +5,8 @@ import (
"embed"
"evening_detective/internal/app"
"evening_detective/internal/config"
"evening_detective/internal/modules/cleaner"
"evening_detective/internal/modules/formatter"
"evening_detective/internal/modules/link"
"evening_detective/internal/modules/password"
"evening_detective/internal/modules/pdf"
@ -44,8 +46,13 @@ func main() {
panic(err)
}
cleaner := cleaner.NewCleaner()
formatter := formatter.NewFormatter()
storyFilepath := config.GetStoryFilepath()
storyService, err := story_service.NewStoryService(storyFilepath)
storyService, err := story_service.NewStoryService(cleaner, formatter, storyFilepath)
if err != nil {
log.Fatalln(err)
}

View File

@ -0,0 +1,6 @@
package cleaner
type ICleaner interface {
// ([Ы-1]) -> ы1, Ы-1 -> ы1
ClearCode(code string) string
}

View File

@ -1,4 +1,4 @@
package story_service
package cleaner
import "strings"
@ -20,15 +20,17 @@ var (
}
)
func clearMatch(s string) string {
s = strings.TrimPrefix(s, "(")
s = strings.TrimPrefix(s, "[")
s = strings.TrimSuffix(s, ")")
s = strings.TrimSuffix(s, "]")
return clearCode(s)
type service struct{}
func NewCleaner() ICleaner {
return &service{}
}
func clearCode(code string) string {
func (s *service) ClearCode(code string) string {
code = strings.TrimPrefix(code, "(")
code = strings.TrimPrefix(code, "[")
code = strings.TrimSuffix(code, ")")
code = strings.TrimSuffix(code, "]")
code = strings.ToLower(code)
code = strings.TrimSpace(code)
code = strings.ReplaceAll(code, "-", "")

View File

@ -0,0 +1,5 @@
package formatter
type IFormatter interface {
FormatText(text string) string
}

View File

@ -0,0 +1,39 @@
package formatter
import (
"bufio"
"strings"
)
type service struct{}
func NewFormatter() IFormatter {
return &service{}
}
func (s *service) FormatText(text string) string {
scanner := bufio.NewScanner(strings.NewReader(text))
scanner.Split(bufio.ScanLines)
lines := []string{}
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
var res strings.Builder
for i, line := range lines {
l := strings.TrimSpace(line)
if i == 0 && strings.HasPrefix(l, "—") {
res.WriteString(" ")
}
if i > 0 {
res.WriteString("\n")
if len(l) > 0 {
res.WriteString(" ")
}
}
res.WriteString(l)
}
return res.String()
}

View File

@ -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
}