Merge branch 'develop'

This commit is contained in:
kercylan98 2023-07-03 12:05:02 +08:00
commit a96d5740de
6 changed files with 117 additions and 59 deletions

View File

@ -6,8 +6,8 @@
}, },
"Other": { "Other": {
"0": { "0": {
"id": 1, "name": "张飞",
"name": "张飞" "id": 1
}, },
"1": { "1": {
"id": 2, "id": 2,

View File

@ -1,58 +1,58 @@
{ {
"1": { "1": {
"b": { "b": {
"Award": {
"0": "asd",
"1": "12"
},
"Other": {
"0": {
"name": "张飞",
"id": 1
},
"1": {
"name": "刘备",
"id": 2
}
},
"Id": 1, "Id": 1,
"Count": "b" "Count": "b",
}
},
"2": {
"c": {
"Award": { "Award": {
"0": "asd", "0": "asd",
"1": "12" "1": "12"
}, },
"Other": {
"0": {
"name": "张飞",
"id": 1
},
"1": {
"id": 2,
"name": "刘备"
}
},
"Id": 2,
"Count": "c"
},
"d": {
"Id": 2,
"Count": "d",
"Award": {
"1": "12",
"0": "asd"
},
"Other": { "Other": {
"0": { "0": {
"id": 1, "id": 1,
"name": "张飞" "name": "张飞"
}, },
"1": {
"name": "刘备",
"id": 2
}
}
}
},
"2": {
"c": {
"Other": {
"0": {
"id": 1,
"name": "张飞"
},
"1": {
"name": "刘备",
"id": 2
}
},
"Id": 2,
"Count": "c",
"Award": {
"0": "asd",
"1": "12"
}
},
"d": {
"Id": 2,
"Count": "d",
"Award": {
"0": "asd",
"1": "12"
},
"Other": {
"1": { "1": {
"id": 2, "id": 2,
"name": "刘备" "name": "刘备"
},
"0": {
"id": 1,
"name": "张飞"
} }
} }
} }

View File

@ -9,6 +9,7 @@ import (
) )
var json = jsonIter.ConfigCompatibleWithStandardLibrary var json = jsonIter.ConfigCompatibleWithStandardLibrary
var full map[string]any
var ( var (
// IndexConfig 有索引 // IndexConfig 有索引
IndexConfig map[int]map[string]*IndexConfigDefine IndexConfig map[int]map[string]*IndexConfigDefine
@ -32,11 +33,16 @@ func LoadConfig(handle func(filename string, config any) error) {
} }
// Refresh 将加载后的配置刷新到线上
func Refresh() { func Refresh() {
full = make(map[string]any)
IndexConfig = _IndexConfig IndexConfig = _IndexConfig
full["IndexConfig"] = IndexConfig
EasyConfig = _EasyConfig EasyConfig = _EasyConfig
full["EasyConfig"] = EasyConfig
} }
// DefaultLoad 默认提供的配置加载函数
func DefaultLoad(filepath string) { func DefaultLoad(filepath string) {
LoadConfig(func(filename string, config any) error { LoadConfig(func(filename string, config any) error {
bytes, err := os.ReadFile(filepath) bytes, err := os.ReadFile(filepath)
@ -47,3 +53,9 @@ func DefaultLoad(filepath string) {
return json.Unmarshal(bytes, &config) return json.Unmarshal(bytes, &config)
}) })
} }
// GetFull 获取所有配置的 map 集合
// - 通常用于前端配置通过后端接口获取的情况
func GetFull() map[string]any {
return full
}

View File

@ -41,6 +41,7 @@ import (
) )
var json = jsonIter.ConfigCompatibleWithStandardLibrary var json = jsonIter.ConfigCompatibleWithStandardLibrary
var full map[string]any
var ( var (
{{range $index, $config := .Configs}} {{range $index, $config := .Configs}}
@ -66,13 +67,16 @@ func LoadConfig(handle func(filename string, config any) error) {
{{end}} {{end}}
} }
// Refresh 将加载后的配置刷新到线上
func Refresh() { func Refresh() {
full = make(map[string]any)
{{range $index, $config := .Configs}} {{range $index, $config := .Configs}}
{{$config.Name}} = _{{$config.Name}} {{$config.Name}} = _{{$config.Name}}
full["{{$config.Name}}"] = {{$config.Name}}
{{end}} {{end}}
} }
// DefaultLoad 默认提供的配置加载函数
func DefaultLoad(filepath string) { func DefaultLoad(filepath string) {
LoadConfig(func(filename string, config any) error { LoadConfig(func(filename string, config any) error {
bytes, err := os.ReadFile(filepath) bytes, err := os.ReadFile(filepath)
@ -82,6 +86,12 @@ func DefaultLoad(filepath string) {
return json.Unmarshal(bytes, &config) return json.Unmarshal(bytes, &config)
}) })
} }
// GetFull 获取所有配置的 map 集合
// - 通常用于前端配置通过后端接口获取的情况
func GetFull() map[string]any {
return full
}
` `
GenerateGoDefineTemplate = `// Code generated by minotaur-config-export. DO NOT EDIT. GenerateGoDefineTemplate = `// Code generated by minotaur-config-export. DO NOT EDIT.

View File

@ -1,17 +0,0 @@
package network
import (
"fmt"
"github.com/kercylan98/minotaur/utils/generic"
"github.com/kercylan98/minotaur/utils/maths"
"strings"
)
// FloatEnlarge 用于将浮点型放大后进行网络传输,返回放大后的值和放大的倍率
// - 存在精度丢失问题如1.13
func FloatEnlarge[F generic.Float](f F) (value int64, multi int64) {
str := fmt.Sprint(f)
multi = maths.PowInt64(10, int64(len(str[strings.Index(str, ".")+1:])))
value = int64(f * F(multi))
return
}

View File

@ -1,5 +1,7 @@
package slice package slice
import "math/rand"
// Del 删除特定索引的元素 // Del 删除特定索引的元素
func Del[V any](slice *[]V, index int) { func Del[V any](slice *[]V, index int) {
s := *slice s := *slice
@ -67,3 +69,54 @@ func NextLoop[V any](slice []V, i int) (next int, value V) {
} }
return next, slice[next] return next, slice[next]
} }
// PrevLoop 返回 i 的上一个数组成员,当 i 为 0 时从数组末尾开始
// - 当 i 为 -1 时将返回最后一个元素
func PrevLoop[V any](slice []V, i int) (prev int, value V) {
if i == -1 {
return len(slice) - 1, slice[len(slice)-1]
}
prev = i - 1
if prev == -1 {
prev = len(slice) - 1
}
return prev, slice[prev]
}
// Reverse 反转数组
func Reverse[V any](slice []V) {
for i := 0; i < len(slice)/2; i++ {
slice[i], slice[len(slice)-1-i] = slice[len(slice)-1-i], slice[i]
}
}
// Shuffle 随机打乱数组
func Shuffle[V any](slice []V) {
for i := 0; i < len(slice); i++ {
j := rand.Intn(len(slice))
slice[i], slice[j] = slice[j], slice[i]
}
}
// Swap 交换数组中的两个元素
func Swap[V any](slice []V, i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
// ToMap 将数组转换为 map
func ToMap[K comparable, V any](slice []V, key func(V) K) map[K]V {
m := make(map[K]V)
for _, v := range slice {
m[key(v)] = v
}
return m
}
// ToSet 将数组转换为 set
func ToSet[V comparable](slice []V) map[V]struct{} {
m := make(map[V]struct{})
for _, v := range slice {
m[v] = struct{}{}
}
return m
}