manager.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package limiteditems
  2. import (
  3. "encoding/json"
  4. "os"
  5. "strconv"
  6. "sync"
  7. "time"
  8. "bet24.com/log"
  9. platformconfig "bet24.com/servers/micros/platformconfig/proto"
  10. )
  11. var mgr *manager
  12. const config_key = "limiteditems_config"
  13. type itemGained struct {
  14. ItemId int
  15. Count int
  16. }
  17. type limitedInfo struct {
  18. ItemId int
  19. Count int
  20. }
  21. type manager struct {
  22. lock *sync.RWMutex
  23. userItems map[int][]itemGained
  24. isDirty bool
  25. limitedConf []limitedInfo
  26. lastCheckDay int
  27. }
  28. func getManager() *manager {
  29. if mgr == nil {
  30. mgr = new(manager)
  31. mgr.ctor()
  32. }
  33. return mgr
  34. }
  35. func (m *manager) ctor() {
  36. log.Release("limiteditems.manager.run()")
  37. m.lock = &sync.RWMutex{}
  38. m.userItems = make(map[int][]itemGained)
  39. m.readConf()
  40. m.lastCheckDay = time.Now().Day()
  41. m.loadUserItemsFromRedis()
  42. }
  43. func (m *manager) readConf() {
  44. time.AfterFunc(refresh_config_sec*time.Second, m.readConf)
  45. configString := platformconfig.GetConfig(config_key)
  46. if configString == "" {
  47. data, err := os.ReadFile("fishconf/limiteditems.json")
  48. if err != nil {
  49. log.Release("limiteditems.manager.loadData read limiteditems failed")
  50. return
  51. }
  52. configString = string(data)
  53. if configString != "" {
  54. platformconfig.SetConfig(config_key, configString)
  55. }
  56. }
  57. m.lock.Lock()
  58. defer m.lock.Unlock()
  59. err := json.Unmarshal([]byte(configString), &m.limitedConf)
  60. if err != nil {
  61. log.Release("limiteditems.manager.loadData Unmarshal failed err:%v", err)
  62. }
  63. }
  64. func (m *manager) dump(param1, param2 string) {
  65. if param1 == "" {
  66. m.dumpSys()
  67. return
  68. }
  69. m.dumpUser(param1)
  70. }
  71. func (m *manager) addItem(userId int, itemId int, count int) int {
  72. m.lock.RLock()
  73. if len(m.limitedConf) == 0 {
  74. m.lock.RUnlock()
  75. return count
  76. }
  77. limitedCount := 0
  78. for _, v := range m.limitedConf {
  79. if v.ItemId == itemId {
  80. limitedCount = v.Count
  81. break
  82. }
  83. }
  84. m.lock.RUnlock()
  85. if 0 == limitedCount {
  86. return count
  87. }
  88. m.checkDayRefresh()
  89. userGained := m.getUserGained(userId)
  90. added := false
  91. for i := 0; i < len(userGained); i++ {
  92. if userGained[i].ItemId == itemId {
  93. itemCount := userGained[i].Count
  94. added = true
  95. if itemCount+count > limitedCount {
  96. count = limitedCount - userGained[i].Count
  97. if count < 0 {
  98. count = 0
  99. }
  100. }
  101. userGained[i].Count += count
  102. break
  103. }
  104. }
  105. // 之前没有
  106. if !added {
  107. if count > limitedCount {
  108. count = limitedCount
  109. }
  110. userGained = append(userGained, itemGained{ItemId: itemId, Count: count})
  111. }
  112. m.lock.Lock()
  113. m.userItems[userId] = userGained
  114. m.lock.Unlock()
  115. m.isDirty = true
  116. return count
  117. }
  118. func (m *manager) checkDayRefresh() {
  119. nowDay := time.Now().Day()
  120. if nowDay == m.lastCheckDay {
  121. return
  122. }
  123. m.lastCheckDay = nowDay
  124. // 清理数据
  125. m.lock.Lock()
  126. m.userItems = make(map[int][]itemGained)
  127. m.lock.Unlock()
  128. m.isDirty = true
  129. }
  130. func (m *manager) getUserGained(userId int) []itemGained {
  131. m.lock.RLock()
  132. defer m.lock.RUnlock()
  133. ret, ok := m.userItems[userId]
  134. if !ok {
  135. return []itemGained{}
  136. }
  137. return ret
  138. }
  139. func (m *manager) dumpSys() {
  140. log.Release("-------------------------------")
  141. log.Release("manager.dumpSys")
  142. defer func() {
  143. log.Release("+++++++++++++++++++++++++++++++")
  144. log.Release("")
  145. }()
  146. m.lock.RLock()
  147. defer m.lock.RUnlock()
  148. if len(m.limitedConf) == 0 {
  149. log.Release(" no limited items active")
  150. return
  151. }
  152. for _, v := range m.limitedConf {
  153. log.Release(" itemId:%d count:%d", v.ItemId, v.Count)
  154. }
  155. }
  156. func (m *manager) dumpUser(userIdStr string) {
  157. log.Release("-------------------------------")
  158. log.Release("manager.dumpUser[%s]", userIdStr)
  159. defer func() {
  160. log.Release("+++++++++++++++++++++++++++++++")
  161. log.Release("")
  162. }()
  163. userId, err := strconv.Atoi(userIdStr)
  164. if err != nil {
  165. log.Release(" atoi error %v", err)
  166. return
  167. }
  168. gained := m.getUserGained(userId)
  169. if len(gained) == 0 {
  170. log.Release(" user has no gained")
  171. return
  172. }
  173. for _, v := range gained {
  174. log.Release(" ItemId[%d],gained[%d]", v.ItemId, v.Count)
  175. }
  176. }