jsonconf.go 1.69 KB
package jsonconf

import (
	"common/logger"
	"encoding/json"

	"io/ioutil"
	"os"
)

var (
	g_jsonconf = new(GameConfig)
)

type WithDrawDesc struct {
	Id    int     `json:"id"`
	Money float32 `json:"money"`
	Coin  int     `json:"coin"`
	Task  int     `json:"task"`
	Level int     `json:"level"`
	Isnew int     `json:"new"`
	Day   int     `json:"day"`
}

type ActiveWithdrawConfigDesc struct {
	Id    int     `json:"id"`
	Money float32 `json:"money"`
	Coin  int     `json:"coin"`
	Task  int     `json:"task"`
	Level int     `json:"level"`
	Isnew int     `json:"new"`
	Day   int     `json:"day"`
}

type GameConfig struct {
	WithDrawConfig       []WithDrawDesc
	ActiveWithdrawConfig []ActiveWithdrawConfigDesc
}

func GetJsonConf() *GameConfig {
	return g_jsonconf
}



func file_get_contents(path string) ([]byte, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	return ioutil.ReadAll(f)
}

func LoadJsonConf() error {
	//pconf := &GameConfig{}
	//加载第一个配置

	path := "../jsonconf/ActiveWithdrawConfig.json"
	content, err := file_get_contents(path)
	if err != nil {
		logger.Info("loadJsonConf failed1,err=%v", err)
		return err
	}

	err = json.Unmarshal([]byte(content), &g_jsonconf.ActiveWithdrawConfig)
	if err != nil {
		logger.Info("loadJsonConf failed1,err=%v", err)
		return err
	}

	path = "../jsonconf/WithdrawConfig.json"
	content, err = file_get_contents(path)
	if err != nil {
		logger.Info("loadJsonConf failed1,err=%v", err)
		return err
	}

	err = json.Unmarshal([]byte(content), &g_jsonconf.WithDrawConfig)
	if err != nil {
		logger.Info("loadJsonConf failed1,err=%v", err)
		return err
	}

	logger.Info("loadJsonConf success pconf=%v,err=%v", *g_jsonconf, err)
	return nil
}