-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
178 lines (154 loc) · 4.96 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package story
import (
"fmt"
"github.com/flandiayingman/arkwaifu/internal/pkg/ark"
arkparser2 "github.com/flandiayingman/arkwaifu/internal/pkg/arkparser"
cols "github.com/flandiayingman/arkwaifu/internal/pkg/cols"
"strings"
)
type Service struct {
r *repo
}
func newService(r *repo) *Service {
return &Service{r: r}
}
type GroupFilter struct {
Type string
}
func (s *Service) GetStories(server ark.Server) ([]*Story, error) {
return s.r.SelectStories(server)
}
func (s *Service) GetStory(server ark.Server, id string) (*Story, error) {
return s.r.SelectStory(id, server)
}
func (s *Service) GetStoryGroups(server ark.Server, filter GroupFilter) ([]*Group, error) {
if filter.Type != "" {
return s.r.SelectStoryGroupsByType(server, filter.Type)
}
return s.r.SelectStoryGroups(server)
}
func (s *Service) GetStoryGroup(server ark.Server, id string) (*Group, error) {
return s.r.SelectStoryGroup(id, server)
}
func (s *Service) GetPictureArts(server ark.Server) ([]*PictureArt, error) {
return s.r.SelectPictureArts(server)
}
func (s *Service) GetAggregatedPictureArt(server ark.Server, id string) (*AggregatedPictureArt, error) {
return s.r.SelectAggregatedPictureArtByID(server, id)
}
func (s *Service) GetCharacterArts(server ark.Server) ([]*CharacterArt, error) {
return s.r.SelectCharacterArts(server)
}
func (s *Service) GetAggregatedCharacterArt(server ark.Server, id string) (*AggregatedCharacterArt, error) {
return s.r.SelectAggregatedCharacterArtByID(server, id)
}
func (s *Service) PopulateFrom(rawTree *arkparser2.StoryTree, server ark.Server) error {
converter := objectConverter{server: server}
tree, err := converter.convertStoryTree(rawTree)
if err != nil {
return fmt.Errorf("converter error when populating story service: %w", err)
}
for _, group := range tree {
// Prevent error: extended protocol limited to 65535 parameters
// TODO: Find a more efficient way to prevent error.
err = s.r.UpsertStoryGroups([]Group{group})
if err != nil {
return err
}
}
return nil
}
type objectConverter struct {
server ark.Server
}
func (c *objectConverter) convertStoryTree(tree *arkparser2.StoryTree) (Tree, error) {
return cols.MapErr(tree.StoryGroups, c.convertStoryGroup)
}
func (c *objectConverter) convertStoryGroup(rawGroup *arkparser2.StoryGroup) (Group, error) {
group := Group{
Server: c.server,
ID: strings.ToLower(rawGroup.ID),
Name: rawGroup.Name,
Type: "",
Stories: nil,
SortID: nil, // SortID is auto-increment, no need to handle it.
}
var err error
group.Type, err = c.convertGroupType(rawGroup.Type)
if err != nil {
return Group{}, err
}
group.Stories, err = cols.MapErr(rawGroup.Stories, c.convertStory)
if err != nil {
return Group{}, err
}
return group, nil
}
func (c *objectConverter) convertGroupType(rawType string) (GroupType, error) {
switch rawType {
case arkparser2.TypeMain:
return GroupTypeMainStory, nil
case arkparser2.TypeActivity:
return GroupTypeMajorEvent, nil
case arkparser2.TypeMiniActivity:
return GroupTypeMinorEvent, nil
case arkparser2.TypeNone:
return GroupTypeOther, nil
default:
return "", fmt.Errorf("unknown story group type: %v", rawType)
}
}
func (c *objectConverter) convertStory(rawStory *arkparser2.Story) (Story, error) {
story := Story{
Server: c.server,
ID: strings.ToLower(rawStory.ID),
Tag: "",
TagText: rawStory.TagText,
Code: rawStory.Code,
Name: rawStory.Name,
Info: rawStory.Info,
GroupID: rawStory.GroupID,
SortID: nil, // SortID is auto-increment, no need to handle it.
PictureArts: cols.Map(rawStory.Pictures, c.convertPictureArt),
CharacterArts: cols.Map(rawStory.Characters, c.convertCharacterArt),
}
var err error
story.Tag, err = c.convertStoryTagType(rawStory.TagType)
if err != nil {
return Story{}, err
}
return story, nil
}
func (c *objectConverter) convertStoryTagType(rawType string) (Tag, error) {
switch rawType {
case arkparser2.TagBefore:
return TagBefore, nil
case arkparser2.TagAfter:
return TagAfter, nil
case arkparser2.TagInterlude:
return TagInterlude, nil
default:
return "", fmt.Errorf("unknown arkparser story group type: %v", rawType)
}
}
func (c *objectConverter) convertPictureArt(rawPicture *arkparser2.StoryPicture) PictureArt {
return PictureArt{
Server: c.server,
ID: strings.ToLower(rawPicture.ID),
StoryID: "", // This will be auto-generated by the ORM framework
Category: rawPicture.Category,
Title: rawPicture.Title,
Subtitle: rawPicture.Subtitle,
SortID: nil, // SortID is auto-increment, no need to handle it.
}
}
func (c *objectConverter) convertCharacterArt(rawCharacter *arkparser2.StoryCharacter) CharacterArt {
return CharacterArt{
Server: c.server,
ID: strings.ToLower(rawCharacter.ID),
StoryID: "", // This will be auto-generated by the ORM framework
Category: "character",
Names: rawCharacter.Names,
SortID: nil, // SortID is auto-increment, no need to handle it.
}
}