data.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package gmdata
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "gadmin/utility"
  7. "io"
  8. "os"
  9. "sync"
  10. "github.com/sirupsen/logrus"
  11. "github.com/spf13/cast"
  12. )
  13. // 从json中读取配置
  14. type GoodsItem struct {
  15. Id int `json:"goods_id"`
  16. Cd int `json:"cd"`
  17. Price int `json:"price"`
  18. }
  19. type ShopItem struct {
  20. GoodId int `json:"good_id"`
  21. Price string `json:"price"`
  22. }
  23. type Equipment struct {
  24. BodyId int64 `json:"bodyId"` //穿戴部位
  25. Id int64 `json:"id"`
  26. LevelId int64 `json:"level_id"`
  27. Name string `json:"name"`
  28. Star int `json:"star"`
  29. DecoMaterial [][]int64 `json:"deco_material"` //分解后获得
  30. Equiplev int `json:"equiplev"`
  31. }
  32. type Material struct {
  33. ID int `json:"Uid"`
  34. Type int `json:"Type"`
  35. Name string `json:"Name"`
  36. }
  37. // 天赋
  38. type Talent struct {
  39. ID int64 `json:"talent_id"`
  40. Name string `json:"talent_name"`
  41. MaxLevel int64 `json:"max_level"`
  42. BaseValue string `json:"base_value"`
  43. }
  44. type Role struct {
  45. Id int64 `json:"Role_id"`
  46. Name string `json:"Role_name"`
  47. Type int64 `json:"Role_type"`
  48. Level int64 `json:"Role_level"`
  49. Evolution int `json:"Role_evolution"`
  50. SkillUnlockLevel []int `json:"skill_unlock_level"`
  51. Skill []int `json:"Role_skill"`
  52. }
  53. type Level struct {
  54. Level int64 `json:"level"`
  55. Exp int64 `json:"exp"`
  56. }
  57. type Antique struct {
  58. ID int `json:"antiques_id"`
  59. Type int `json:"antiques_type"`
  60. Name string `json:"antiques_name"`
  61. }
  62. type Good struct {
  63. Value int32 `json:"value"`
  64. Label string `json:"label"`
  65. }
  66. // 商品价格与类型
  67. type Cost struct {
  68. Id int64 `json:"id"`
  69. Count int64 `json:"count"`
  70. }
  71. type ShopGoods struct {
  72. Id int64 `json:"ID"`
  73. Name string `json:"Name"`
  74. Cost []Cost `json:"Cost"`
  75. GoodsType int `json:"GoodsType"`
  76. }
  77. type Server struct {
  78. ServerId int `json:"serverId"`
  79. Page int `json:"page"`
  80. ServerName string `json:"serverName"`
  81. MaxPlayerNumber int `json:"maxPlayerNumber"`
  82. }
  83. type Exskill struct {
  84. SkillID int `json:"skill_id,omitempty"`
  85. SkillGroup int `json:"skill_group,omitempty"`
  86. SkillType int `json:"skill_type,omitempty"`
  87. SkillName string `json:"skill_name,omitempty"`
  88. SkillLevel int `json:"skill_level,omitempty"`
  89. SkillUnlock int `json:"skill_unlock,omitempty"`
  90. SkillIcon string `json:"skill_icon,omitempty"`
  91. IsAll int `json:"isall,omitempty"`
  92. SkillDes string `json:"skill_des,omitempty"`
  93. SkillValue []float64 `json:"skill_value,omitempty"`
  94. CombatEffe int `json:"combat_effe,omitempty"`
  95. }
  96. // json配置列表
  97. var (
  98. GoodsInfo []GoodsItem // 商品信息
  99. Talents []Talent // 天赋信息
  100. EquipmentDict = make([]Equipment, 0)
  101. MaterialsDict = make([]Material, 0)
  102. AntiQueDict = make([]Antique, 0) // 古玩信息
  103. Goods = make([]Good, 0)
  104. AllShopGoods = make(map[string]ShopGoods)
  105. Servers = make([]Server, 0)
  106. Roles []Role // 不同角色分一组
  107. PlayLevel []Level //
  108. Exskills []Exskill
  109. CurrentPath string
  110. lock sync.Mutex
  111. )
  112. // GetEquipmentStartID 获取指定装备的起始ID,1级ID
  113. func GetEquipmentStartID(id int64) int64 {
  114. return id/1000*1000 + 1
  115. }
  116. // 完整天赋
  117. func GetFullTalent() map[int]int {
  118. data := make(map[int]int)
  119. for _, talent := range Talents {
  120. data[int(talent.ID)] = int(talent.MaxLevel)
  121. }
  122. return data
  123. }
  124. // 天赋清空
  125. func GetEmptyTalent() map[int]int {
  126. data := make(map[int]int)
  127. for _, talent := range Talents {
  128. data[int(talent.ID)] = 0
  129. }
  130. return data
  131. }
  132. func GetGoodLabel(value int32) string {
  133. for _, v := range Goods {
  134. if v.Value == value {
  135. return v.Label + "(" + cast.ToString(value) + ")"
  136. }
  137. }
  138. return `未关联商品信息` + "(" + cast.ToString(value) + ")"
  139. }
  140. func GetShopGoods() map[int64]ShopGoods {
  141. data := make(map[int64]ShopGoods)
  142. for _, v := range AllShopGoods {
  143. data[v.Id] = v
  144. }
  145. return data
  146. }
  147. func LoadGraveJson(confPath string) (err error) {
  148. lock.Lock()
  149. defer lock.Unlock()
  150. logrus.Infof("当前加载的json配置路径为:%v", confPath)
  151. /*err = LoadItemData(confPath, "Role.json", &Roles)
  152. if err != nil {
  153. return
  154. }
  155. err = LoadItemData(confPath, "playerLevel.json", &PlayLevel)
  156. if err != nil {
  157. return
  158. }*/
  159. err = LoadItemData(confPath, "item.json", &MaterialsDict)
  160. if err != nil {
  161. return
  162. }
  163. return
  164. LoadItemData(confPath, "antique.json", &AntiQueDict)
  165. err = LoadItemData(confPath, "talent.json", &Talents) //天赋
  166. if err != nil {
  167. return
  168. }
  169. for _, v := range []string{"weapons.json"} { //"wear.json",
  170. var equi []Equipment
  171. LoadItemData(confPath, v, &equi)
  172. EquipmentDict = append(EquipmentDict, equi...)
  173. }
  174. err = LoadItemData(confPath, "goods.json", &Goods)
  175. if err != nil {
  176. return
  177. }
  178. err = LoadItemData(confPath, "product.json", &AllShopGoods)
  179. if err != nil {
  180. return
  181. }
  182. CurrentPath = confPath
  183. loadNoviceTask(confPath)
  184. LoadLimitGift(confPath)
  185. LoadTreasure(confPath) //宝物
  186. LoadServer(confPath)
  187. LoadKingSeason(confPath)
  188. LoadChapter() //关卡
  189. return
  190. }
  191. func LoadItemData(confPath, file string, value interface{}) (err error) {
  192. if file == "" {
  193. panic("file cannot be blank")
  194. }
  195. fileObj, err := os.Open(fmt.Sprintf("%s/%s", confPath, file))
  196. if err != nil {
  197. panic(err)
  198. }
  199. fileData, err := io.ReadAll(fileObj)
  200. if err != nil {
  201. panic(err)
  202. }
  203. err = json.Unmarshal(fileData, value)
  204. if err != nil {
  205. panic(err)
  206. }
  207. return
  208. }
  209. func SetConfPath(confPath string) (err error) {
  210. ok, err := utility.PathExists(confPath)
  211. if err != nil {
  212. return
  213. }
  214. if !ok {
  215. return errors.New("路径未找到")
  216. }
  217. err = LoadGraveJson(confPath)
  218. if err != nil {
  219. return
  220. }
  221. os.WriteFile("json_path.txt", []byte(confPath), 0600)
  222. CurrentPath = confPath
  223. return
  224. }
  225. func loadJsonFile(filePath string, val interface{}) {
  226. ptrFile, err := os.Open(filePath)
  227. if err != nil {
  228. logrus.Fatalln("读取json文件失败", err)
  229. }
  230. defer ptrFile.Close()
  231. decoder := json.NewDecoder(ptrFile)
  232. if err = decoder.Decode(&val); err != nil {
  233. logrus.Fatalln("loadJsonFile Decoder failed ", filePath, err.Error())
  234. }
  235. }
  236. func LoadCurrentFromDisk() {
  237. data, err := os.ReadFile("json_path.txt")
  238. if err != nil {
  239. return
  240. }
  241. CurrentPath = string(data)
  242. LoadGraveJson(CurrentPath)
  243. }
  244. func IsMaterial(id int) bool {
  245. for _, v := range MaterialsDict {
  246. if v.ID == id {
  247. return true
  248. }
  249. }
  250. return false
  251. }
  252. func IsEquipment(id int64) bool {
  253. for _, v := range EquipmentDict {
  254. if v.LevelId == id {
  255. return true
  256. }
  257. }
  258. return false
  259. }
  260. func GetEquipment(id int64) Equipment {
  261. for _, v := range EquipmentDict {
  262. if v.LevelId == id {
  263. return v
  264. }
  265. }
  266. var v2 Equipment
  267. return v2
  268. }
  269. func GetRoleNameByType(typ int64) string {
  270. for _, v := range Roles {
  271. if v.Type == typ {
  272. return v.Name
  273. }
  274. }
  275. return ""
  276. }