giftpackmgr.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package giftpack
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "bet24.com/log"
  7. "bet24.com/servers/common"
  8. inventory "bet24.com/servers/micros/item_inventory/proto"
  9. item "bet24.com/servers/micros/item_inventory/proto"
  10. )
  11. type giftpackmgr struct {
  12. giftPackList map[int]*GiftPack
  13. }
  14. func newGiftPackManager() *giftpackmgr {
  15. gm := new(giftpackmgr)
  16. gm.giftPackList = make(map[int]*GiftPack)
  17. gm.loadGiftPackList()
  18. return gm
  19. }
  20. func (gm *giftpackmgr) loadGiftPackList() {
  21. // TODO 从数据库加载列表
  22. gm.giftPackList = getGiftPackList()
  23. }
  24. // 获取用户可以购买的礼包列表
  25. type buyableInfo struct {
  26. id int
  27. group int
  28. groupIndex int
  29. timeLeft int
  30. }
  31. func (gm *giftpackmgr) getUserGiftPack(userId int) []UserBuyable {
  32. var buyables []buyableInfo
  33. userHistory := gm.getUserHistoty(userId)
  34. for _, sysGiftPack := range gm.giftPackList {
  35. buyable, timeLeft := gm.isBuyable(sysGiftPack, userHistory)
  36. if buyable {
  37. buyables = append(buyables, buyableInfo{
  38. id: sysGiftPack.Id,
  39. group: sysGiftPack.Group,
  40. groupIndex: sysGiftPack.GroupIndex,
  41. timeLeft: timeLeft,
  42. })
  43. }
  44. }
  45. // 去除同Group中低级可购买项
  46. ret := gm.trimSameGroup(buyables)
  47. // 插入当天已购买的
  48. now := common.GetTimeStamp()
  49. for _, v := range userHistory {
  50. g, ok := gm.giftPackList[v.GiftPackId]
  51. if !ok {
  52. continue
  53. }
  54. if g.Group == 0 {
  55. continue
  56. }
  57. if common.IsSameDay(v.LastPhaseTime, now) {
  58. ret = append(ret, UserBuyable{v.GiftPackId, false, 0})
  59. }
  60. }
  61. return ret
  62. }
  63. func (gm *giftpackmgr) trimSameGroup(buyables []buyableInfo) []UserBuyable {
  64. //log.Debug("giftpackmgr.trimSameGroup start %v", buyables)
  65. var ret []UserBuyable
  66. groupMax := make(map[int]buyableInfo)
  67. for _, v := range buyables {
  68. if v.group == 0 {
  69. ret = append(ret, UserBuyable{v.id, true, v.timeLeft})
  70. continue
  71. }
  72. max, ok := groupMax[v.group]
  73. if !ok || max.groupIndex < v.groupIndex {
  74. groupMax[v.group] = v
  75. }
  76. }
  77. for _, v := range groupMax {
  78. ret = append(ret, UserBuyable{v.id, true, v.timeLeft})
  79. }
  80. //log.Debug("giftpackmgr.trimSameGroup end %v", ret)
  81. return ret
  82. }
  83. func (gm *giftpackmgr) isBuyable(giftPack *GiftPack, history []*UserGiftPackHistory) (bool, int) {
  84. var us *UserGiftPackHistory
  85. var pre *UserGiftPackHistory
  86. for _, v := range history {
  87. if v.GiftPackId == giftPack.Id {
  88. us = v
  89. }
  90. if v.group == giftPack.Group && v.groupIndex == giftPack.GroupIndex-1 {
  91. pre = v
  92. }
  93. }
  94. // 只能购买一次
  95. if giftPack.Limit == GiftPackLimit_Once && us != nil {
  96. return false, 0
  97. }
  98. now := common.GetTimeStamp()
  99. regTime := 0
  100. for _, v := range history {
  101. if v.GiftPackId == 0 {
  102. regTime = v.LastPhaseTime
  103. }
  104. }
  105. // 如果是限时道具
  106. timeLeft := 0
  107. if giftPack.Duration != 0 {
  108. timeLeft = regTime + giftPack.Duration - now
  109. if timeLeft < 0 {
  110. timeLeft = 0
  111. }
  112. return timeLeft > 0, timeLeft
  113. }
  114. timeLeft = 0
  115. // 不用连续的
  116. if giftPack.Group == 0 {
  117. if giftPack.Limit == GiftPackLimit_None || us == nil {
  118. return true, timeLeft
  119. }
  120. // 限制每天一次
  121. return !common.IsSameDay(us.LastPhaseTime, now), timeLeft
  122. }
  123. // 有Group
  124. // 今天买过了
  125. if us != nil && common.IsSameDay(us.LastPhaseTime, now) {
  126. return false, timeLeft
  127. }
  128. // 第一个一定可以购买
  129. if giftPack.GroupIndex == 0 {
  130. // 如果相同的Group其他的物品今天买过,也不行
  131. for _, v := range history {
  132. if v.group != giftPack.Group {
  133. continue
  134. }
  135. if common.IsSameDay(v.LastPhaseTime, now) {
  136. return false, timeLeft
  137. }
  138. }
  139. return true, timeLeft
  140. }
  141. // 不是第一个
  142. if pre == nil {
  143. return false, timeLeft
  144. }
  145. return common.IsContinueDay(pre.LastPhaseTime, now), 0
  146. }
  147. func (gm *giftpackmgr) getUserHistoty(userId int) []*UserGiftPackHistory {
  148. // TODO 用户购买记录
  149. ret := getUserGiftPackHistory(userId)
  150. for _, v := range ret {
  151. g, ok := gm.giftPackList[v.GiftPackId]
  152. if !ok {
  153. //log.Debug("giftpackmgr.getUserHistoty giftPackId not valid %d", v.GiftPackId)
  154. continue
  155. }
  156. v.group = g.Group
  157. v.groupIndex = g.GroupIndex
  158. }
  159. return ret
  160. }
  161. func (gm *giftpackmgr) getGiftPack(giftPackId int) *GiftPack {
  162. return gm.giftPackList[giftPackId]
  163. }
  164. func (gm *giftpackmgr) buyGiftPack(userId int, productId string) []item.ItemPack {
  165. for _, v := range gm.giftPackList {
  166. if v.ProductId == productId {
  167. logType := 0
  168. switch v.Type {
  169. case GiftPackType_First:
  170. logType = common.LOGTYPE_PACK_FIRST
  171. case GiftPackType_TimeLimited:
  172. logType = common.LOGTYPE_PACK_TIMELIMITED
  173. case GiftPackType_EveryDay:
  174. logType = common.LOGTYPE_PACK_EVERYDAY
  175. }
  176. inventory.AddItems(userId, v.Items, fmt.Sprintf("特惠礼包[%d]", v.Id), logType)
  177. go gm.updateUserHistoty(userId, v.Id)
  178. return v.Items
  179. }
  180. }
  181. log.Debug("buyGiftPack failed invalid userId[%d] productId[%s]", userId, productId)
  182. return nil
  183. }
  184. func (gm *giftpackmgr) updateUserHistoty(userId int, giftPackId int) {
  185. now := common.GetNowTime().Format(common.Layout)
  186. log.Debug("giftpackmgr.updateUserHistoty %d,%d,%d", userId, giftPackId, now)
  187. // TODO insert 或 update 用户购买记录
  188. updateUserGiftPackHistory(userId, giftPackId, now)
  189. }
  190. // 获取所有大礼包信息
  191. func (gm *giftpackmgr) getGiftPackList() map[int]*GiftPack {
  192. return gm.giftPackList
  193. }
  194. func (gm *giftpackmgr) dumpSys(param string) {
  195. log.Release("-------------------------------")
  196. log.Release("giftpackmgr.dumpSys %s", param)
  197. defer func() {
  198. log.Release("+++++++++++++++++++++++++++++++")
  199. log.Release("")
  200. }()
  201. d, _ := json.Marshal(gm.giftPackList)
  202. log.Release(string(d))
  203. }
  204. func (gm *giftpackmgr) dumpUser(param string) {
  205. log.Release("-------------------------------")
  206. log.Release("giftpackmgr.dumpUser %s", param)
  207. defer func() {
  208. log.Release("+++++++++++++++++++++++++++++++")
  209. log.Release("")
  210. }()
  211. var userId int
  212. var err error
  213. if userId, err = strconv.Atoi(param); err != nil {
  214. log.Release("atoi error %v", err)
  215. return
  216. }
  217. si := gm.getUserGiftPack(userId)
  218. if si == nil {
  219. log.Release("user %d not exist", userId)
  220. return
  221. }
  222. d, _ := json.Marshal(si)
  223. log.Release(string(d))
  224. }