grandmaster.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package gmdata
  2. import (
  3. "encoding/json"
  4. "leafstalk/log"
  5. "os"
  6. "sort"
  7. )
  8. type KingSeasonDan struct {
  9. Id int `json:"id"`
  10. Type int `json:"type"`
  11. DanName string `json:"danName"`
  12. DanIcon string `json:"danIcon"`
  13. Count int64 `json:"count"`
  14. IsReducePoints int `json:"isReducePoints"`
  15. Reward []int `json:"reward"`
  16. }
  17. var (
  18. KingSeasonDans []*KingSeasonDan
  19. )
  20. type ByMasterScore []*KingSeasonDan
  21. func (a ByMasterScore) Len() int { return len(a) }
  22. func (a ByMasterScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  23. func (a ByMasterScore) Less(i, j int) bool { return a[i].Count < a[j].Count }
  24. // LoadKingSeason 加载配置信息
  25. func LoadKingSeason(confPath string) {
  26. loadLoadKingSeasonFile(confPath+"/"+"KingSeasonDan.json", &KingSeasonDans)
  27. sort.Sort(sort.Reverse(ByMasterScore(KingSeasonDans)))
  28. }
  29. func loadLoadKingSeasonFile(filePath string, val interface{}) {
  30. ptrFile, err := os.Open(filePath)
  31. if err != nil {
  32. log.Fatalln("读取json文件失败", err)
  33. }
  34. defer ptrFile.Close()
  35. decoder := json.NewDecoder(ptrFile)
  36. if err = decoder.Decode(&val); err != nil {
  37. log.Fatalln("loadLoadKingSeasonFile Decoder failed ", filePath, err.Error())
  38. }
  39. }
  40. // GetDanByScore 通过段位积分获取当前段位
  41. func GetDanByScore(score int64) *KingSeasonDan {
  42. cnt := len(KingSeasonDans)
  43. i := sort.Search(cnt, func(i int) bool {
  44. return score >= KingSeasonDans[i].Count
  45. })
  46. if i < cnt {
  47. return KingSeasonDans[i]
  48. }
  49. return nil
  50. }