data.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. LegendsDict = make([]Rune, 0)
  110. AntiQueDict = make([]Antique, 0) // 古玩信息
  111. Goods = make([]Good, 0)
  112. AllShopGoods = make(map[string]ShopGoods)
  113. Servers = make([]Server, 0)
  114. Roles []Role // 不同角色分一组
  115. PlayLevel []Level //
  116. Exskills []Exskill
  117. CurrentPath string
  118. lock sync.Mutex
  119. )
  120. // GetEquipmentStartID 获取指定装备的起始ID,1级ID
  121. func GetEquipmentStartID(id int64) int64 {
  122. return id/1000*1000 + 1
  123. }
  124. // 完整天赋
  125. func GetFullTalent() map[int]int {
  126. data := make(map[int]int)
  127. for _, talent := range Talents {
  128. data[int(talent.ID)] = int(talent.MaxLevel)
  129. }
  130. return data
  131. }
  132. // 天赋清空
  133. func GetEmptyTalent() map[int]int {
  134. data := make(map[int]int)
  135. for _, talent := range Talents {
  136. data[int(talent.ID)] = 0
  137. }
  138. return data
  139. }
  140. func GetGoodLabel(value int32) string {
  141. for _, v := range Goods {
  142. if v.Value == value {
  143. return v.Label + "(" + cast.ToString(value) + ")"
  144. }
  145. }
  146. return `未关联商品信息` + "(" + cast.ToString(value) + ")"
  147. }
  148. func GetShopGoods() map[int64]ShopGoods {
  149. data := make(map[int64]ShopGoods)
  150. for _, v := range AllShopGoods {
  151. data[v.Id] = v
  152. }
  153. return data
  154. }
  155. func LoadGraveJson(confPath string) (err error) {
  156. lock.Lock()
  157. defer lock.Unlock()
  158. logrus.Infof("当前加载的json配置路径为:%v", confPath)
  159. /*err = LoadItemData(confPath, "Role.json", &Roles)
  160. if err != nil {
  161. return
  162. }
  163. err = LoadItemData(confPath, "playerLevel.json", &PlayLevel)
  164. if err != nil {
  165. return
  166. }*/
  167. err = LoadItemData(confPath, "item.json", &MaterialsDict)
  168. if err != nil {
  169. return
  170. }
  171. err = LoadItemData(confPath, "rune.json", &RunesDict)
  172. if err != nil {
  173. return
  174. }
  175. err = LoadItemData(confPath, "legend.json", &LegendsDict)
  176. if err != nil {
  177. return
  178. }
  179. CurrentPath = confPath
  180. LoadChapter() //关卡
  181. return
  182. LoadItemData(confPath, "antique.json", &AntiQueDict)
  183. err = LoadItemData(confPath, "talent.json", &Talents) //天赋
  184. if err != nil {
  185. return
  186. }
  187. for _, v := range []string{"weapons.json"} { //"wear.json",
  188. var equi []Equipment
  189. LoadItemData(confPath, v, &equi)
  190. EquipmentDict = append(EquipmentDict, equi...)
  191. }
  192. err = LoadItemData(confPath, "goods.json", &Goods)
  193. if err != nil {
  194. return
  195. }
  196. err = LoadItemData(confPath, "product.json", &AllShopGoods)
  197. if err != nil {
  198. return
  199. }
  200. CurrentPath = confPath
  201. loadNoviceTask(confPath)
  202. LoadLimitGift(confPath)
  203. LoadTreasure(confPath) //宝物
  204. LoadServer(confPath)
  205. LoadKingSeason(confPath)
  206. return
  207. }
  208. func LoadItemData(confPath, file string, value interface{}) (err error) {
  209. if file == "" {
  210. panic("file cannot be blank")
  211. }
  212. fileObj, err := os.Open(fmt.Sprintf("%s/%s", confPath, file))
  213. if err != nil {
  214. panic(err)
  215. }
  216. fileData, err := io.ReadAll(fileObj)
  217. if err != nil {
  218. panic(err)
  219. }
  220. err = json.Unmarshal(fileData, value)
  221. if err != nil {
  222. panic(err)
  223. }
  224. return
  225. }
  226. func SetConfPath(confPath string) (err error) {
  227. ok, err := utility.PathExists(confPath)
  228. if err != nil {
  229. return
  230. }
  231. if !ok {
  232. return errors.New("路径未找到")
  233. }
  234. err = LoadGraveJson(confPath)
  235. if err != nil {
  236. return
  237. }
  238. os.WriteFile("json_path.txt", []byte(confPath), 0600)
  239. CurrentPath = confPath
  240. return
  241. }
  242. func loadJsonFile(filePath string, val interface{}) {
  243. ptrFile, err := os.Open(filePath)
  244. if err != nil {
  245. logrus.Fatalln("读取json文件失败", err)
  246. }
  247. defer ptrFile.Close()
  248. decoder := json.NewDecoder(ptrFile)
  249. if err = decoder.Decode(&val); err != nil {
  250. logrus.Fatalln("loadJsonFile Decoder failed ", filePath, err.Error())
  251. }
  252. }
  253. func LoadCurrentFromDisk() {
  254. data, err := os.ReadFile("json_path.txt")
  255. if err != nil {
  256. return
  257. }
  258. CurrentPath = string(data)
  259. LoadGraveJson(CurrentPath)
  260. }
  261. func IsMaterial(id int) bool {
  262. for _, v := range MaterialsDict {
  263. if v.ID == id {
  264. return true
  265. }
  266. }
  267. return false
  268. }
  269. func IsEquipment(id int64) bool {
  270. for _, v := range EquipmentDict {
  271. if v.LevelId == id {
  272. return true
  273. }
  274. }
  275. return false
  276. }
  277. func GetEquipment(id int64) Equipment {
  278. for _, v := range EquipmentDict {
  279. if v.LevelId == id {
  280. return v
  281. }
  282. }
  283. var v2 Equipment
  284. return v2
  285. }
  286. func GetRoleNameByType(typ int64) string {
  287. for _, v := range Roles {
  288. if v.Type == typ {
  289. return v.Name
  290. }
  291. }
  292. return ""
  293. }