data.go 6.9 KB

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