-
Notifications
You must be signed in to change notification settings - Fork 0
/
tradition.go
276 lines (248 loc) · 6.96 KB
/
tradition.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package culture
import (
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"path"
"runtime"
"sync"
"github.com/olehmushka/golang-toolkit/either"
randomTools "github.com/olehmushka/golang-toolkit/random_tools"
sliceTools "github.com/olehmushka/golang-toolkit/slice_tools"
"github.com/olehmushka/golang-toolkit/wrapped_error"
"github.com/olehmushka/world-generator-engine/gender"
"github.com/olehmushka/world-generator-engine/tools"
"github.com/olehmushka/world-generator-engine/types"
)
type Tradition struct {
Slug string `json:"slug" bson:"slug"`
Description string `json:"description" bson:"description"`
PreferredEthosSlugs []string `json:"preferred_ethos_slugs" bson:"preferred_ethos_slugs"`
Type TraditionType `json:"type" bson:"type"`
OmitTraditionSlugs []string `json:"omit_tradition_slugs" bson:"omit_tradition_slugs"`
OmitGenderDominance []gender.Sex `json:"omit_gender_dominance" bson:"omit_gender_dominance"`
OmitEthosSlugs []string `json:"omit_ethos_slugs" bson:"omit_ethos_slugs"`
}
func RandomTraditions(ts []*Tradition, min, max int, e Ethos, ds gender.Sex) ([]*Tradition, error) {
if len(ts) == 0 {
return nil, wrapped_error.NewBadRequestError(nil, "can not get random traditions of empty slice of traditions")
}
if len(ts) < min {
return nil, wrapped_error.NewBadRequestError(nil, "can not get random traditions from less slice of traditions than min expected traditions number")
}
size, err := randomTools.RandIntInRange(min, max)
if err != nil {
return nil, wrapped_error.NewInternalServerError(err, "can not generate size of random traditions slise")
}
out := make([]*Tradition, 0, size)
for _, t := range FilterTraditionsByDomitatedSex(FilterTraditionsByEthos(sliceTools.Shuffle(ts), e), ds) {
if len(out) >= max {
break
}
isOk := true
for _, traditionToOmit := range t.OmitTraditionSlugs {
for _, selected := range out {
if selected.Slug == traditionToOmit {
isOk = false
break
}
}
}
if isOk {
out = append(out, t)
}
}
if len(out) < min {
return nil, wrapped_error.NewBadRequestError(nil, fmt.Sprintf("can not generate expected number of traditions (expected=%d, actual=%d)", min, len(out)))
}
return out, nil
}
func FilterTraditionsByEthos(in []*Tradition, e Ethos) []*Tradition {
if e.IsZero() {
return in
}
out := make([]*Tradition, 0, len(in))
for _, t := range in {
isOk := true
for _, ethosToOmit := range t.OmitEthosSlugs {
if ethosToOmit == e.Slug {
isOk = false
break
}
}
if isOk {
out = append(out, t)
}
}
return out
}
func FilterTraditionsByDomitatedSex(in []*Tradition, ds gender.Sex) []*Tradition {
out := make([]*Tradition, 0, len(in))
for _, t := range in {
isOk := true
for _, sexToOmit := range t.OmitGenderDominance {
if sexToOmit == ds {
isOk = false
break
}
}
if isOk {
out = append(out, t)
}
}
return out
}
func ExtractTraditions(cultures []*Culture) []*Tradition {
out := make([]*Tradition, 0, len(cultures))
for _, c := range cultures {
out = append(out, c.Traditions...)
}
return out
}
func UniqueTraditions(in []*Tradition) []*Tradition {
if len(in) <= 1 {
return in
}
preOut := make(map[string]*Tradition)
for _, e := range in {
preOut[e.Slug] = e
}
out := make([]*Tradition, 0, len(preOut))
for _, e := range preOut {
out = append(out, e)
}
return out
}
func SepareteTraditionsByPresent(present []*Tradition, ts []string) ([]string, []string) {
if len(present) == 0 {
return []string{}, []string{}
}
included := make([]string, 0, len(present)/2)
notIncluded := make([]string, 0, len(present)/2)
for _, t := range ts {
var isFound bool
for _, pr := range present {
if pr.Slug == t {
isFound = true
break
}
}
if isFound {
included = append(included, t)
continue
}
notIncluded = append(notIncluded, t)
}
return included, notIncluded
}
func LoadAllTraditions(opts ...types.ChangeStringFunc) chan either.Either[[]*Tradition] {
_, filename, _, _ := runtime.Caller(1)
currDirname := tools.PreparePath(path.Dir(filename), "culture")
dirname := currDirname + "data/traditions/"
for _, fn := range opts {
dirname = fn(dirname)
}
ch := make(chan either.Either[[]*Tradition], MaxLoadDataConcurrency)
go func() {
files, err := ioutil.ReadDir(dirname)
if err != nil {
ch <- either.Either[[]*Tradition]{Err: wrapped_error.NewInternalServerError(err, fmt.Sprintf("can not read dir by dirname (dirname=%s)", dirname))}
return
}
var wg sync.WaitGroup
wg.Add(len(files))
for _, file := range files {
go func(file fs.FileInfo) {
defer wg.Done()
if file.IsDir() {
return
}
filename := dirname + file.Name()
b, err := ioutil.ReadFile(filename)
if err != nil {
ch <- either.Either[[]*Tradition]{Err: err}
return
}
var ts []*Tradition
if err := json.Unmarshal(b, &ts); err != nil {
ch <- either.Either[[]*Tradition]{Err: err}
return
}
for _, chunk := range sliceTools.Chunk(MaxLoadDataChunkSize, ts) {
ch <- either.Either[[]*Tradition]{Value: chunk}
}
}(file)
}
wg.Wait()
close(ch)
}()
return ch
}
func SearchTradition(slug string, opts ...types.ChangeStringFunc) (*Tradition, error) {
_, filename, _, _ := runtime.Caller(1)
currDirname := tools.PreparePath(path.Dir(filename), "culture")
dirname := currDirname + "data/traditions/"
for _, fn := range opts {
dirname = fn(dirname)
}
files, err := ioutil.ReadDir(dirname)
if err != nil {
return nil, wrapped_error.NewInternalServerError(err, fmt.Sprintf("can not read dir by dirname (dirname=%s)", dirname))
}
for _, file := range files {
if file.IsDir() {
continue
}
filename := dirname + file.Name()
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var ts []*Tradition
if err := json.Unmarshal(b, &ts); err != nil {
return nil, err
}
for _, t := range ts {
if t.Slug == slug {
return t, nil
}
}
}
return nil, nil
}
func SearchTraditions(slugs []string, opts ...types.ChangeStringFunc) ([]*Tradition, error) {
_, filename, _, _ := runtime.Caller(1)
currDirname := tools.PreparePath(path.Dir(filename), "culture")
dirname := currDirname + "data/traditions/"
for _, fn := range opts {
dirname = fn(dirname)
}
files, err := ioutil.ReadDir(dirname)
if err != nil {
return nil, wrapped_error.NewInternalServerError(err, fmt.Sprintf("can not read dir by dirname (dirname=%s)", dirname))
}
out := make([]*Tradition, 0, len(slugs))
picked := make(map[string]bool, len(slugs))
for _, file := range files {
if file.IsDir() {
continue
}
filename := dirname + file.Name()
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var ts []*Tradition
if err := json.Unmarshal(b, &ts); err != nil {
return nil, err
}
for _, t := range ts {
if _, found := picked[t.Slug]; sliceTools.Includes(slugs, t.Slug) && !found {
out = append(out, t)
picked[t.Slug] = true
}
}
}
return out, nil
}