221 lines
6.3 KiB
Go
221 lines
6.3 KiB
Go
package dataset
|
|
|
|
import (
|
|
"github.com/mohae/deepcopy"
|
|
"github.com/tomas-qstarrs/boost/cast"
|
|
"github.com/tomas-qstarrs/boost/stringx"
|
|
"mongo.games.com/game/gamesrv/slotspkg/internal/exported/excel2go/storage"
|
|
"mongo.games.com/game/gamesrv/slotspkg/internal/generic/errors"
|
|
"mongo.games.com/game/gamesrv/slotspkg/internal/generic/key"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type SubStorage = map[string]map[string]interface{}
|
|
type Storage = map[string]SubStorage
|
|
|
|
func init() {
|
|
Load(nil)
|
|
}
|
|
|
|
func Load(configMap map[string]string) {
|
|
if configMap != nil {
|
|
storage.StoragesLoading(configMap)
|
|
}
|
|
storage.StoragesMapping()
|
|
storage.LinksMapping()
|
|
storage.CategoriesMapping()
|
|
}
|
|
|
|
func Duplicate() Storage {
|
|
return deepcopy.Copy(storage.Storage).(Storage)
|
|
}
|
|
|
|
func DuplicateOrigin() Storage {
|
|
return deepcopy.Copy(storage.OriginStorage).(Storage)
|
|
}
|
|
|
|
type (
|
|
// DataSet is the type of group data
|
|
DataSet struct {
|
|
Category string
|
|
SubStorage SubStorage
|
|
}
|
|
)
|
|
|
|
// Base returns Base data for data that cannot be group by
|
|
func Base() *DataSet {
|
|
return NewDataset(key.Base, storage.Storage)
|
|
}
|
|
|
|
// NewDataset returns data set by player platform & Group
|
|
func NewDataset(category string, storage Storage) *DataSet {
|
|
subStorage, ok := storage[category]
|
|
if !ok {
|
|
subStorage, ok = storage[key.Base]
|
|
if !ok {
|
|
panic(errors.ConfigCategoryNotFound.ErrorWith(category))
|
|
}
|
|
}
|
|
ds := &DataSet{
|
|
Category: category,
|
|
SubStorage: subStorage,
|
|
}
|
|
return ds
|
|
}
|
|
|
|
// getSheet gets sheet config data by path & sheet
|
|
func (ds *DataSet) getSheet(path string, sheet string) (interface{}, error) {
|
|
excel, ok := ds.SubStorage[path]
|
|
if !ok {
|
|
return nil, errors.ConfigExcelNotFound.Origin()
|
|
}
|
|
v, ok := excel[sheet]
|
|
if !ok {
|
|
return nil, errors.ConfigSheetNotFound.Origin()
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
func (ds *DataSet) ExistSheet(path string, sheet string) bool {
|
|
_, err := ds.getSheet(path, sheet)
|
|
return err == nil
|
|
}
|
|
|
|
// GetSheet gets sheet config data by path & sheet
|
|
func (ds *DataSet) GetSheet(path string, sheet string) interface{} {
|
|
v, err := ds.getSheet(path, sheet)
|
|
if err != nil {
|
|
panic(errors.ConfigSheetNotFound.ErrorWith(path, sheet))
|
|
}
|
|
return v
|
|
}
|
|
|
|
// GetDefaultSheet gets default sheet config data by path & sheet
|
|
func (ds *DataSet) GetDefaultSheet(path string) interface{} {
|
|
v, err := ds.getSheet(path, key.Default)
|
|
if err != nil {
|
|
panic(errors.ConfigSheetNotFound.ErrorWith(path, key.Default))
|
|
}
|
|
return v
|
|
}
|
|
|
|
// GetKeySheet gets sheet config data by path & key sheet
|
|
func (ds *DataSet) GetKeySheet(path string, originSheet string, keyName interface{}) interface{} {
|
|
sheet := stringx.Merge(originSheet, "/", cast.ToString(keyName))
|
|
v, err := ds.getSheet(path, sheet)
|
|
if err != nil {
|
|
panic(errors.ConfigSheetNotFound.ErrorWith(path, keyName))
|
|
}
|
|
return v
|
|
}
|
|
|
|
// GetDefaultKeySheet gets default sheet config data by path & key sheet
|
|
func (ds *DataSet) GetDefaultKeySheet(path string, keyName interface{}) interface{} {
|
|
sheet := stringx.Merge(key.Default, "/", cast.ToString(keyName))
|
|
v, err := ds.getSheet(path, sheet)
|
|
if err != nil {
|
|
panic(errors.ConfigSheetNotFound.ErrorWith(path, keyName))
|
|
}
|
|
return v
|
|
}
|
|
|
|
func (ds *DataSet) ExistMachineSheet(dir string, excel string, originSheet string, class int64) bool {
|
|
path := stringx.Merge(dir, "/", excel)
|
|
sheet := stringx.Merge(originSheet, "/", strconv.FormatInt(class, 10))
|
|
if _, err := ds.getSheet(path, sheet); err == nil {
|
|
return true
|
|
}
|
|
sheet = originSheet
|
|
if _, err := ds.getSheet(path, sheet); err == nil {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// GetMachineSheet returns machine sheet config by class
|
|
func (ds *DataSet) GetMachineSheet(dir string, excel string, originSheet string, class int64) interface{} {
|
|
path := stringx.Merge(dir, "/", excel)
|
|
sheet := stringx.Merge(originSheet, "/", strconv.FormatInt(class, 10))
|
|
if v, err := ds.getSheet(path, sheet); err == nil {
|
|
return v
|
|
}
|
|
sheet = originSheet
|
|
if v, err := ds.getSheet(path, sheet); err == nil {
|
|
return v
|
|
} else {
|
|
panic(errors.ConfigSheetNotFound.ErrorWith(dir, excel, class))
|
|
}
|
|
}
|
|
|
|
// GetMachineDefaultSheet returns machine sheet config by class
|
|
func (ds *DataSet) GetMachineDefaultSheet(dir string, excel string, class int64) interface{} {
|
|
path := stringx.Merge(dir, "/", excel)
|
|
sheet := stringx.Merge(key.Default, "/", strconv.FormatInt(class, 10))
|
|
if v, err := ds.getSheet(path, sheet); err == nil {
|
|
return v
|
|
}
|
|
sheet = key.Default
|
|
if v, err := ds.getSheet(path, sheet); err == nil {
|
|
return v
|
|
} else {
|
|
panic(errors.ConfigSheetNotFound.ErrorWith(dir, excel, class))
|
|
}
|
|
}
|
|
|
|
// GetMachineKeySheet returns machine key sheet config by class
|
|
func (ds *DataSet) GetMachineKeySheet(dir string, excel string, originSheet string, class int64, keyName interface{}) interface{} {
|
|
path := stringx.Merge(dir, "/", excel)
|
|
sheet := stringx.Merge(originSheet, "/", strconv.FormatInt(class, 10), "/", cast.ToString(keyName))
|
|
if v, err := ds.getSheet(path, sheet); err == nil {
|
|
return v
|
|
}
|
|
sheet = stringx.Merge(originSheet, "/", cast.ToString(keyName))
|
|
if v, err := ds.getSheet(path, sheet); err == nil {
|
|
return v
|
|
} else {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// GetMachineDefaultKeySheet returns machine key sheet config by class
|
|
func (ds *DataSet) GetMachineDefaultKeySheet(dir string, excel string, class int64, keyName interface{}) interface{} {
|
|
path := stringx.Merge(dir, "/", excel)
|
|
sheet := stringx.Merge(key.Default, "/", strconv.FormatInt(class, 10), "/", cast.ToString(keyName))
|
|
if v, err := ds.getSheet(path, sheet); err == nil {
|
|
return v
|
|
}
|
|
sheet = stringx.Merge(key.Default, "/", cast.ToString(keyName))
|
|
if v, err := ds.getSheet(path, sheet); err == nil {
|
|
return v
|
|
} else {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// GetMachineExcel returns machine excel config by class
|
|
func (ds *DataSet) GetMachineExcel(dir string, excel string, class int64) map[string]interface{} {
|
|
path := stringx.Merge(dir, "/", excel)
|
|
excelData, ok := ds.SubStorage[path]
|
|
if !ok {
|
|
panic(errors.ConfigExcelNotFound.ErrorWith(path))
|
|
}
|
|
finalExcelList := make(map[string]interface{})
|
|
for sheet, sheetData := range excelData {
|
|
parts := strings.Split(sheet, "/")
|
|
|
|
if len(parts) == 1 {
|
|
if _, ok := finalExcelList[parts[0]]; !ok {
|
|
finalExcelList[parts[0]] = sheetData
|
|
}
|
|
} else if n, err := strconv.ParseInt(parts[1], 10, 64); err == nil {
|
|
if n == class {
|
|
finalExcelList[parts[0]] = sheetData
|
|
}
|
|
} else {
|
|
panic(errors.ConfigSheetNotFound.ErrorWith(path, sheet))
|
|
}
|
|
}
|
|
return finalExcelList
|
|
}
|