game_sync/common/config.go

182 lines
3.4 KiB
Go

package common
import (
"encoding/json"
"strings"
"mongo.games.com/goserver/core"
)
var Config = Configuration{}
type Configuration struct {
AppId string
SrvId string
IsDevMode bool
}
func (this *Configuration) Name() string {
return "common"
}
func (this *Configuration) Init() error {
return nil
}
func (this *Configuration) Close() error {
return nil
}
func init() {
core.RegistePackage(&Config)
core.RegistePackage(&CustomConfig)
}
var CustomConfig = make(CustomConfiguration)
type CustomConfiguration map[string]interface{}
func (this *CustomConfiguration) Name() string {
return "costum"
}
func (this *CustomConfiguration) Init() error {
return nil
}
func (this *CustomConfiguration) Close() error {
return nil
}
func (this *CustomConfiguration) GetString(key string) string {
if v, exist := (*this)[key]; exist {
if str, ok := v.(string); ok {
return str
}
}
if v, exist := (*this)[strings.ToLower(key)]; exist {
if str, ok := v.(string); ok {
return str
}
}
return ""
}
func (this *CustomConfiguration) GetStrings(key string) (strs []string) {
if v, exist := (*this)[key]; exist {
if vals, ok := v.([]interface{}); ok {
for _, s := range vals {
if str, ok := s.(string); ok {
strs = append(strs, str)
}
}
return
}
}
if v, exist := (*this)[strings.ToLower(key)]; exist {
if vals, ok := v.([]interface{}); ok {
for _, s := range vals {
if str, ok := s.(string); ok {
strs = append(strs, str)
}
}
return
}
}
return
}
func (this *CustomConfiguration) GetCustomCfgs(key string) (strs []*CustomConfiguration) {
if v, exist := (*this)[key]; exist {
if vals, ok := v.([]interface{}); ok {
for _, s := range vals {
if data, ok := s.(map[string]interface{}); ok {
var pkg *CustomConfiguration
modelBuff, _ := json.Marshal(data)
err := json.Unmarshal(modelBuff, &pkg)
if err == nil {
strs = append(strs, pkg)
}
}
}
return
}
}
if v, exist := (*this)[strings.ToLower(key)]; exist {
if vals, ok := v.([]interface{}); ok {
for _, s := range vals {
if data, ok := s.(map[string]interface{}); ok {
var pkg *CustomConfiguration
modelBuff, _ := json.Marshal(data)
err := json.Unmarshal(modelBuff, &pkg)
if err == nil {
strs = append(strs, pkg)
}
}
}
return
}
}
return
}
func (this *CustomConfiguration) GetInts(key string) (strs []int) {
if v, exist := (*this)[key]; exist {
if vals, ok := v.([]interface{}); ok {
for _, s := range vals {
if str, ok := s.(float64); ok {
strs = append(strs, int(str))
}
}
return
}
}
if v, exist := (*this)[strings.ToLower(key)]; exist {
if vals, ok := v.([]interface{}); ok {
for _, s := range vals {
if str, ok := s.(float64); ok {
strs = append(strs, int(str))
}
}
return
}
}
return
}
func (this *CustomConfiguration) GetInt(key string) int {
if v, exist := (*this)[key]; exist {
if val, ok := v.(float64); ok {
return int(val)
}
}
if v, exist := (*this)[strings.ToLower(key)]; exist {
if val, ok := v.(float64); ok {
return int(val)
}
}
return 0
}
func (this *CustomConfiguration) GetBool(key string) bool {
if v, exist := (*this)[key]; exist {
if val, ok := v.(bool); ok {
return val
}
}
if v, exist := (*this)[strings.ToLower(key)]; exist {
if val, ok := v.(bool); ok {
return val
}
}
return false
}