treasure.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package gmdata
  2. //宝物
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "leafstalk/log"
  7. "os"
  8. "strconv"
  9. )
  10. // DropedTreasure 拥有的宝物
  11. type DropedTreasure struct {
  12. RoleID int64 `json:"roleId"` // 佩戴角色
  13. TID int64 `json:"tId"` // 宝物id
  14. LuckyValue float64 `json:"luckyValue"` // 幸运值
  15. }
  16. type Treasure struct {
  17. Id int64 `json:"number"`
  18. Quality int `json:"quality"`
  19. Name string `json:"name"`
  20. //Icon string `json:"icon"`
  21. //CombatEffe int `json:"combatEffe"`
  22. //CombatEffeUp string `json:"combatEffe_up"`
  23. //Blood int `json:"blood"`
  24. //BloodLevup string `json:"blood_Levup"`
  25. //ElementType int `json:"elementType"`
  26. //ElementDamage int `json:"elementDamage"`
  27. //ElementDamageLevup string `json:"elementDamage_Levup"`
  28. //GoldReduction int `json:"goldReduction"`
  29. //GoldReductionLevup string `json:"goldReduction_Levup"`
  30. //WoodReduction int `json:"woodReduction"`
  31. //WoodReductionLevup string `json:"woodReduction_Levup"`
  32. //WaterReduction int `json:"waterReduction"`
  33. //WaterReductionLevup string `json:"waterReduction_Levup"`
  34. //FireReduction int `json:"fireReduction"`
  35. //FireReductionLevup string `json:"fireReduction_Levup"`
  36. //SoilReduction int `json:"soilReduction"`
  37. //SoilReductionLevup string `json:"soilReduction_Levup"`
  38. //NowEntries string `json:"Now_Entries"`
  39. }
  40. var (
  41. Treasures []*Treasure
  42. )
  43. // LoadTreasure 加载配置信息
  44. func LoadTreasure(confPath string) {
  45. loadTreasureFile(confPath+"/"+"Treasure.json", &Treasures)
  46. }
  47. func loadTreasureFile(filePath string, val interface{}) {
  48. ptrFile, err := os.Open(filePath)
  49. if err != nil {
  50. log.Fatalln("读取json文件失败", err)
  51. }
  52. defer ptrFile.Close()
  53. decoder := json.NewDecoder(ptrFile)
  54. if err = decoder.Decode(&val); err != nil {
  55. log.Fatalln("loadTreasureFile Decoder failed ", filePath, err.Error())
  56. }
  57. }
  58. // GetTreasure 获取指定宝物
  59. func GetTreasure(id int64) *Treasure {
  60. for _, v := range Treasures {
  61. if v.Id == id {
  62. return v
  63. }
  64. }
  65. return nil
  66. }
  67. // GetTreasureName 获取指定宝物名称
  68. func GetTreasureName(id int64) string {
  69. for _, v := range Treasures {
  70. if v.Id == id {
  71. return v.Name
  72. }
  73. }
  74. return ""
  75. }
  76. // GetTreasureId 获取宝物类型值ID
  77. func GetTreasureId(tid int64) int64 {
  78. // 转换数字为字符串
  79. numStr := strconv.FormatInt(tid, 10)
  80. // 检查数字是否足够长
  81. if len(numStr) < 11 {
  82. return 0
  83. }
  84. // 提取第三位和第四位数字
  85. digitStr := numStr[2:4]
  86. // 将提取的字符串转换为整数
  87. digits, err := strconv.Atoi(digitStr)
  88. if err != nil {
  89. return 0
  90. }
  91. return int64(digits)
  92. }
  93. // GetTreasureLevel 获取宝物等级
  94. func GetTreasureLevel(tid int64) int64 {
  95. // 取模运算获取最后三位数字
  96. lastThreeDigits := tid % 1000
  97. return lastThreeDigits
  98. }
  99. // GetTreasureStar 获取宝物星级
  100. func GetTreasureStar(tid int64) int64 {
  101. strNum := fmt.Sprintf("%011d", tid)
  102. if len(strNum) < 11 {
  103. return -1
  104. }
  105. digitStr := strNum[6:8]
  106. var digit int64
  107. _, err := fmt.Sscanf(digitStr, "%02d", &digit)
  108. if err != nil {
  109. fmt.Println("输入的数字格式不正确")
  110. return -1
  111. }
  112. if digit < 10 || digit == 0 {
  113. return digit
  114. }
  115. return -1
  116. }
  117. // GetTreasureByTID 获取指定宝物名称
  118. func GetTreasureByTID(tid int64) string {
  119. return GetTreasureName(GetTreasureId(tid))
  120. }