anchor_level.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package model
  2. import (
  3. "errors"
  4. "math"
  5. )
  6. const MaxAnchorLevel = 40
  7. var anchorLevelScoreTable = []int64{
  8. 0,
  9. 50,
  10. 200,
  11. 470,
  12. 920,
  13. 2100,
  14. 4060,
  15. 7160,
  16. 11760,
  17. 18060,
  18. 27160,
  19. 39610,
  20. 56410,
  21. 78810,
  22. 109810,
  23. 154810,
  24. 226810,
  25. 319810,
  26. 442810,
  27. 602810,
  28. 816810,
  29. 1138810,
  30. 1594810,
  31. 2214810,
  32. 3004810,
  33. 3984810,
  34. 5229810,
  35. 6909810,
  36. 9013810,
  37. 11883810,
  38. 15613810,
  39. 20613810,
  40. 27313810,
  41. 36413810,
  42. 47813810,
  43. 62013810,
  44. 79513810,
  45. 99513810,
  46. 122013810,
  47. 147013810,
  48. int64(math.MaxInt64),
  49. }
  50. // Returns the index of first element that **greater** than the given value.
  51. func upperBound(list []int64, value int64) int {
  52. count := len(list)
  53. first:= 0
  54. for count > 0 {
  55. i := first
  56. step := count / 2
  57. i += step
  58. if value >= list[i] {
  59. first = i + 1
  60. count -= step + 1
  61. } else {
  62. count = step
  63. }
  64. }
  65. return first
  66. }
  67. // GetAnchorLevel returns anchor level, i.e. Lv1 ~ Lv40, corresponding to the given score.
  68. func GetAnchorLevel(score int64) (int64, error) {
  69. if score < 0 {
  70. return 0, errors.New("invalid anchor score")
  71. }
  72. return int64(upperBound(anchorLevelScoreTable, score)), nil
  73. }
  74. // GetLevelScoreInfo returns left & right score of a given level.
  75. func GetLevelScoreInfo(lv int64) (left, right int64, err error) {
  76. if lv < 1 || lv >= int64(len(anchorLevelScoreTable)) {
  77. return 0, 0, errors.New("invalid request level")
  78. }
  79. left = anchorLevelScoreTable[lv-1]
  80. right = anchorLevelScoreTable[lv] - 1
  81. return
  82. }
  83. // GetAnchorLevelColor returns level color.
  84. func GetAnchorLevelColor(lv int64) (int64, error) {
  85. if lv < 1 || lv > MaxAnchorLevel {
  86. return 0, errors.New("invalid request level")
  87. }
  88. q := lv / 10
  89. r := lv % 10
  90. if r == 0 {
  91. q -= 1
  92. }
  93. return q, nil
  94. }