policy.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package service
  2. import (
  3. "context"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "go-common/app/interface/main/player/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _userRandomType = "用户随机-尾号"
  13. )
  14. // Policy return policy info.
  15. func (s *Service) Policy(c context.Context, id, mid int64) (item *model.Pitem, err error) {
  16. var policy *model.Policy
  17. if policy, err = s.checkPolicy(id); err != nil {
  18. log.Error("s.getPolicy(%d) err(%v)", id, err)
  19. return
  20. }
  21. switch policy.Type {
  22. case _userRandomType:
  23. if item, err = s.userPolicy(mid, policy); err != nil {
  24. log.Error("s.userPolicy(%d) err(%v)", mid, err)
  25. return
  26. }
  27. }
  28. return
  29. }
  30. func (s *Service) checkPolicy(id int64) (policy *model.Policy, err error) {
  31. if id != 1 {
  32. err = ecode.PLayerPolicyNotExist
  33. return
  34. }
  35. policy = s.c.Policy
  36. if time.Now().Unix() < policy.StartTime.Unix() {
  37. err = ecode.PLayerPolicyNotStart
  38. return
  39. }
  40. if time.Now().Unix() > policy.EndTime.Unix() {
  41. err = ecode.PLayerPolicyEnded
  42. return
  43. }
  44. return
  45. }
  46. // 用户随机-尾号 策略方法
  47. func (s *Service) userPolicy(mid int64, policy *model.Policy) (res *model.Pitem, err error) {
  48. var itemMap = make(map[string]*model.Pitem, len(s.c.Pitem))
  49. for _, item := range s.c.Pitem {
  50. item.Ver = policy.MtimeTime.Unix()
  51. itemMap[item.ExtData] = item
  52. }
  53. if mid > 0 {
  54. utail := int(mid % 100)
  55. for _, item := range itemMap {
  56. var (
  57. begin int
  58. end int
  59. beginAndEnd []string
  60. )
  61. if item.ExtData == "default" {
  62. continue
  63. }
  64. beginAndEnd = strings.Split(item.ExtData, "-")
  65. if len(beginAndEnd) != 2 {
  66. log.Error("item.ExtData error")
  67. return
  68. }
  69. if begin, err = strconv.Atoi(beginAndEnd[0]); err != nil {
  70. log.Error("item.ExtData error")
  71. return
  72. }
  73. if end, err = strconv.Atoi(beginAndEnd[1]); err != nil {
  74. log.Error("item.ExtData error")
  75. return
  76. }
  77. if utail >= begin && utail < end {
  78. res = item
  79. return
  80. }
  81. }
  82. } else {
  83. res = itemMap["default"]
  84. }
  85. return
  86. }