Initial commit
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package generators
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"os"
|
||||
)
|
||||
|
||||
type HalfElfArrays struct {
|
||||
Nm1 []string `json:"nm1"`
|
||||
Nm2 []string `json:"nm2"`
|
||||
Nm3 []string `json:"nm3"`
|
||||
Nm4 []string `json:"nm4"`
|
||||
}
|
||||
|
||||
func LoadHalfElfArrays(path string) (HalfElfArrays, error) {
|
||||
var arrays HalfElfArrays
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return arrays, err
|
||||
}
|
||||
defer file.Close()
|
||||
decoder := json.NewDecoder(file)
|
||||
err = decoder.Decode(&arrays)
|
||||
return arrays, err
|
||||
}
|
||||
|
||||
func HalfElfNameFem(arr *HalfElfArrays, swearFilter func(string) bool) string {
|
||||
rnd := rand.Intn(len(arr.Nm3))
|
||||
rnd2 := rand.Intn(len(arr.Nm4))
|
||||
nMs := arr.Nm3[rnd] + arr.Nm4[rnd2]
|
||||
if swearFilter == nil || !swearFilter(nMs) {
|
||||
return nMs
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func HalfElfNameMas(arr *HalfElfArrays, swearFilter func(string) bool) string {
|
||||
rnd := rand.Intn(len(arr.Nm1))
|
||||
rnd2 := rand.Intn(len(arr.Nm2))
|
||||
nMs := arr.Nm1[rnd] + arr.Nm2[rnd2]
|
||||
if swearFilter == nil || !swearFilter(nMs) {
|
||||
return nMs
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func GenerateHalfElfNames(arr *HalfElfArrays, gender string, swearFilter func(string) bool) []string {
|
||||
names := make([]string, 0, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
var nMs string
|
||||
if gender == "female" {
|
||||
nMs = HalfElfNameFem(arr, swearFilter)
|
||||
for nMs == "" {
|
||||
nMs = HalfElfNameFem(arr, swearFilter)
|
||||
}
|
||||
} else {
|
||||
nMs = HalfElfNameMas(arr, swearFilter)
|
||||
for nMs == "" {
|
||||
nMs = HalfElfNameMas(arr, swearFilter)
|
||||
}
|
||||
}
|
||||
names = append(names, nMs)
|
||||
}
|
||||
return names
|
||||
}
|
||||
Reference in New Issue
Block a user