256 lines
4.9 KiB
Go
256 lines
4.9 KiB
Go
package data
|
|
|
|
import (
|
|
json "github.com/json-iterator/go"
|
|
"github.com/oylshe1314/framework/errors"
|
|
"github.com/oylshe1314/framework/util"
|
|
"strings"
|
|
)
|
|
|
|
type ServerConfig struct {
|
|
Id string `json:"id"`
|
|
Value1 string `json:"value1"`
|
|
Value2 string `json:"value2"`
|
|
Value3 string `json:"value3"`
|
|
Value4 string `json:"value4"`
|
|
}
|
|
|
|
type initRole struct {
|
|
male int
|
|
female int
|
|
}
|
|
|
|
func (this *initRole) init(value1, value2 string) error {
|
|
var err error
|
|
err = util.StringToInteger2(value1, &this.male)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = util.StringToInteger2(value2, &this.female)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type initHeroes struct {
|
|
ids []int
|
|
}
|
|
|
|
func (this *initHeroes) init(value string) error {
|
|
return util.SplitToIntegers2(value, ",", &this.ids)
|
|
}
|
|
|
|
type initItems struct {
|
|
itemIds []int
|
|
itemNums []int
|
|
}
|
|
|
|
func (this *initItems) init(value1, value2 string) error {
|
|
var err error
|
|
err = util.SplitToIntegers2(value1, ",", &this.itemIds)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = util.SplitToIntegers2(value2, ",", &this.itemNums)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for i := range this.itemIds {
|
|
if i >= len(this.itemNums) {
|
|
this.itemNums = append(this.itemNums, 1)
|
|
} else if this.itemNums[i] == 0 {
|
|
this.itemNums[i] = 1
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type initMail struct {
|
|
titleId int
|
|
contentId int
|
|
itemIds []int
|
|
itemNums []int
|
|
}
|
|
|
|
func (this *initMail) init(value3, value14 string) error {
|
|
var err error
|
|
err = util.SplitToIntegers2(value3, ",", &this.itemIds)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = util.SplitToIntegers2(value14, ",", &this.itemNums)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for i := range this.itemIds {
|
|
if i >= len(this.itemNums) {
|
|
this.itemNums = append(this.itemNums, 1)
|
|
} else if this.itemNums[i] < 1 {
|
|
this.itemNums[i] = 1
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type initMails struct {
|
|
initMails []initMail
|
|
}
|
|
|
|
func (this *initMails) init(value1, value2, value3, value4 string) error {
|
|
var err error
|
|
var titleIds, contentIds []int
|
|
err = util.SplitToIntegers2(value1, "|", &titleIds)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = util.SplitToIntegers2(value2, "|", &contentIds)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(titleIds) != len(contentIds) {
|
|
return errors.Errorf("read 'init_mails' len(titleIds) != len(contentIds), value2:%s, value2: %s ", value1, value2)
|
|
}
|
|
|
|
var vs3 = strings.Split(value3, "|")
|
|
var vs4 = strings.Split(value4, "|")
|
|
|
|
this.initMails = make([]initMail, len(titleIds))
|
|
for i := range this.initMails {
|
|
this.initMails[i].titleId = titleIds[i]
|
|
this.initMails[i].contentId = contentIds[i]
|
|
if i >= len(vs3) {
|
|
break
|
|
}
|
|
|
|
var v4 = ""
|
|
if len(vs4) > i {
|
|
v4 = vs4[i]
|
|
}
|
|
err = this.initMails[i].init(vs3[i], v4)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type initTasks struct {
|
|
ids []int
|
|
}
|
|
|
|
func (this *initTasks) init(value string) error {
|
|
return util.SplitToIntegers2(value, ",", &this.ids)
|
|
}
|
|
|
|
type ServerConfigTable struct {
|
|
l []*ServerConfig
|
|
m map[string]*ServerConfig
|
|
|
|
initRole initRole
|
|
initHeroes initHeroes
|
|
initItems initItems
|
|
initMails initMails
|
|
initTasks initTasks
|
|
}
|
|
|
|
func (this *ServerConfigTable) load(buf []byte) error {
|
|
err := json.Unmarshal(buf, &this.l)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
this.m = make(map[string]*ServerConfig)
|
|
for i := range this.l {
|
|
this.m[this.l[i].Id] = this.l[i]
|
|
}
|
|
|
|
return this.init()
|
|
}
|
|
|
|
func (this *ServerConfigTable) init() error {
|
|
var err error
|
|
sfRole, ok := this.m["init_role"]
|
|
if ok {
|
|
err = this.initRole.init(sfRole.Value1, sfRole.Value2)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
sfHeroes, ok := this.m["init_heroes"]
|
|
if ok {
|
|
err = this.initHeroes.init(sfHeroes.Value1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
sfItems, ok := this.m["init_items"]
|
|
if ok {
|
|
err = this.initItems.init(sfItems.Value1, sfItems.Value2)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
sfMails, ok := this.m["init_mails"]
|
|
if ok {
|
|
err = this.initMails.init(sfMails.Value1, sfMails.Value2, sfMails.Value3, sfMails.Value4)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
sfTasks, ok := this.m["init_tasks"]
|
|
if ok {
|
|
err = this.initTasks.init(sfTasks.Value1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *ServerConfigTable) GetInitRole(gender int) int {
|
|
if gender == 0 {
|
|
return this.initRole.female
|
|
} else {
|
|
return this.initRole.male
|
|
}
|
|
}
|
|
|
|
func (this *ServerConfigTable) GetInitHeroes() []int {
|
|
return this.initHeroes.ids
|
|
}
|
|
|
|
func (this *ServerConfigTable) GetInitItems() ([]int, []int) {
|
|
return this.initItems.itemIds, this.initItems.itemNums
|
|
}
|
|
|
|
func (this *ServerConfigTable) GetInitMails() int {
|
|
return len(this.initMails.initMails)
|
|
}
|
|
|
|
func (this *ServerConfigTable) GetInitMail(i int) (int, int, []int, []int) {
|
|
if i >= this.GetInitMails() {
|
|
return 0, 0, nil, nil
|
|
}
|
|
return this.initMails.initMails[i].titleId, this.initMails.initMails[i].contentId, this.initMails.initMails[i].itemIds, this.initMails.initMails[i].itemNums
|
|
}
|
|
|
|
func (this *ServerConfigTable) GetInitTask() []int {
|
|
return this.initTasks.ids
|
|
}
|