-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
114 lines (100 loc) · 2.78 KB
/
base.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
package culture
import (
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"path"
"runtime"
"sync"
"github.com/olehmushka/golang-toolkit/either"
mapTools "github.com/olehmushka/golang-toolkit/map_tools"
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/tools"
"github.com/olehmushka/world-generator-engine/types"
)
func RandomBase(in []string) (string, error) {
if len(in) == 0 {
return "", wrapped_error.NewBadRequestError(nil, "can not get random base of empty slice of bases")
}
if len(in) == 1 {
return in[0], nil
}
out, err := sliceTools.RandomValueOfSlice(randomTools.RandFloat64, in)
if err != nil {
return "", wrapped_error.NewInternalServerError(err, "can not get random base")
}
return out, nil
}
func ExtractBases(cultures []*Culture) []string {
out := make([]string, len(cultures))
for i := range out {
out[i] = cultures[i].BaseSlug
}
return out
}
func SelectBaseByMostRecent(in []string) (string, error) {
m := make(map[string]int, 3)
for _, baseSlug := range in {
if count, ok := m[baseSlug]; ok {
m[baseSlug] = count + 1
} else {
m[baseSlug] = 1
}
}
probs := make(map[string]float64, 3)
total := float64(len(in))
for baseSlug, count := range m {
probs[baseSlug] = float64(count) / total
}
baseSlug, err := mapTools.PickOneByProb(probs)
if err != nil {
return "", wrapped_error.NewInternalServerError(err, "can not select base slug by most recent")
}
return baseSlug, nil
}
func LoadAllBases(opts ...types.ChangeStringFunc) chan either.Either[[]string] {
_, filename, _, _ := runtime.Caller(1)
currDirname := tools.PreparePath(path.Dir(filename), "culture")
dirname := currDirname + "data/bases/"
for _, fn := range opts {
dirname = fn(dirname)
}
ch := make(chan either.Either[[]string], MaxLoadDataConcurrency)
go func() {
files, err := ioutil.ReadDir(dirname)
if err != nil {
ch <- either.Either[[]string]{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) {
if file.IsDir() {
return
}
filename := dirname + file.Name()
b, err := ioutil.ReadFile(filename)
if err != nil {
ch <- either.Either[[]string]{Err: err}
return
}
var bs []string
if err := json.Unmarshal(b, &bs); err != nil {
ch <- either.Either[[]string]{Err: err}
return
}
for _, chunk := range sliceTools.Chunk(MaxLoadDataChunkSize, bs) {
ch <- either.Either[[]string]{Value: chunk}
}
wg.Done()
}(file)
}
wg.Wait()
close(ch)
}()
return ch
}