chapter.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package gmdata
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "leafstalk/log"
  6. "os"
  7. "sort"
  8. )
  9. type Chapter struct {
  10. ID int64 `json:"ID"`
  11. Type int `json:"type"`
  12. Difficulty int64 `json:"difficulty"`
  13. Name string `json:"name"`
  14. RoomCount int32 `json:"chapter"`
  15. CombatEffe int `json:"combatEffe"`
  16. CombatEffeLimit [][]float64 `json:"combatEffeLimit"`
  17. DailyNumber int `json:"dailyNumber"`
  18. AdRefreshSkillTimes int `json:"adRefreshSkillTimes"`
  19. GoldRatio int `json:"goldRatio"`
  20. EndText string `json:"endText"`
  21. OP int `json:"OP"`
  22. SoundID int `json:"SoundID"`
  23. UnlockSkills []int `json:"unlockSkills"`
  24. EquipNumLimit int `json:"EquipNumLimit"`
  25. ReentryLimit int `json:"ReentryLimit"`
  26. }
  27. type ChapterOptionItem struct {
  28. Label string `json:"label"`
  29. Value string `json:"value"`
  30. }
  31. var (
  32. Chapters []*Chapter
  33. ChapterOption []*ChapterOptionItem
  34. )
  35. // LoadChapter 加载配置信息
  36. func LoadChapter() {
  37. loadChapterFile(CurrentPath+"/"+"chapter.json", &Chapters)
  38. // 根据 ID 和 Difficulty 排序,最小越靠前
  39. sort.Slice(Chapters, func(i, j int) bool {
  40. if Chapters[i].ID == Chapters[j].ID {
  41. return Chapters[i].Difficulty < Chapters[j].Difficulty
  42. }
  43. return Chapters[i].ID < Chapters[j].ID
  44. })
  45. for _, v := range Chapters {
  46. ChapterOption = append(ChapterOption, &ChapterOptionItem{
  47. Label: fmt.Sprintf("%v-%v", v.Name, GetDifficultName(v.Difficulty)),
  48. Value: fmt.Sprintf("%v_%v", v.ID, v.Difficulty),
  49. })
  50. }
  51. }
  52. func loadChapterFile(filePath string, val interface{}) {
  53. ptrFile, err := os.Open(filePath)
  54. if err != nil {
  55. log.Fatalln("读取json文件失败", err)
  56. }
  57. defer ptrFile.Close()
  58. decoder := json.NewDecoder(ptrFile)
  59. if err = decoder.Decode(&val); err != nil {
  60. log.Fatalln("loadTreasureFile Decoder failed ", filePath, err.Error())
  61. }
  62. }
  63. func GetChaptersMap() map[string]*Chapter {
  64. var data = make(map[string]*Chapter, len(Chapters))
  65. for _, v := range Chapters {
  66. data[fmt.Sprintf("%d_%d", v.ID, v.Difficulty)] = v
  67. }
  68. return data
  69. }
  70. func GetChapterById(id int32) *Chapter {
  71. for _, v := range Chapters {
  72. if v.ID == int64(id) {
  73. return v
  74. }
  75. }
  76. return nil
  77. }
  78. func GetDifficultName(diff int64) string {
  79. if diff == 1 {
  80. return "困难"
  81. }
  82. if diff == 2 {
  83. return "噩梦"
  84. }
  85. return "普通"
  86. }