itemmgr.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package handler
  2. import (
  3. "bet24.com/log"
  4. pb "bet24.com/servers/micros/item_inventory/proto"
  5. "sync"
  6. )
  7. type itemmgr struct {
  8. itemlist map[int]*pb.Item
  9. lock *sync.RWMutex
  10. lastConfig string
  11. lockDuration *sync.RWMutex
  12. itemDurations map[int]int
  13. }
  14. var item_manager *itemmgr
  15. func getItemManager() *itemmgr {
  16. if item_manager == nil {
  17. item_manager = newItemMgr()
  18. }
  19. return item_manager
  20. }
  21. func newItemMgr() *itemmgr {
  22. ret := new(itemmgr)
  23. ret.itemlist = make(map[int]*pb.Item)
  24. ret.lock = &sync.RWMutex{}
  25. ret.itemDurations = make(map[int]int)
  26. ret.lockDuration = &sync.RWMutex{}
  27. ret.loadItems()
  28. return ret
  29. }
  30. func (im *itemmgr) loadItems() {
  31. if im.loadSysItemFromJson() {
  32. return
  33. }
  34. items := getSysItemList()
  35. im.lock.Lock()
  36. im.itemlist = items
  37. im.lock.Unlock()
  38. im.lockDuration.Lock()
  39. im.itemDurations = make(map[int]int)
  40. for _, v := range items {
  41. if v.Duration > 0 {
  42. im.itemDurations[v.Id] = v.Duration
  43. }
  44. }
  45. im.lockDuration.Unlock()
  46. }
  47. func (im *itemmgr) getItem(itemId int) *pb.Item {
  48. im.lock.RLock()
  49. defer im.lock.RUnlock()
  50. item, ok := im.itemlist[itemId]
  51. if !ok {
  52. return nil
  53. }
  54. return item
  55. }
  56. func (im *itemmgr) getItems() map[int]*pb.Item {
  57. im.lock.RLock()
  58. defer im.lock.RUnlock()
  59. return im.itemlist
  60. }
  61. func (im *itemmgr) getItemValue(itemId int, count int) int {
  62. itm := im.getItem(itemId)
  63. if itm == nil {
  64. return 0
  65. }
  66. if itm.Id == pb.Item_Gold || itm.Id == pb.Item_Chip {
  67. return count
  68. }
  69. return itm.ShowPrice * count
  70. }
  71. func (im *itemmgr) checkDecortaionType(itemId int, t int) bool {
  72. itm := im.getItem(itemId)
  73. if itm == nil {
  74. log.Release("itemmgr.checkDecortaionType item[%d] not found", itemId)
  75. return false
  76. }
  77. if itm.Type != pb.Item_Decoration {
  78. log.Release("itemmgr.checkDecortaionType item[%d] not a decoration", itemId)
  79. return false
  80. }
  81. if itm.DecorationType != t {
  82. log.Release("itemmgr.checkDecortaionType item[%d] mismatch type [%d] is not [%d]", itemId, t, itm.DecorationType)
  83. return false
  84. }
  85. return true
  86. }
  87. func (im *itemmgr) isVirtualItem(itemId int) bool {
  88. im.lock.RLock()
  89. defer im.lock.RUnlock()
  90. for _, v := range im.itemlist {
  91. if v.ActiveId == itemId {
  92. return true
  93. }
  94. }
  95. return false
  96. }
  97. func (im *itemmgr) changeItemCountToDuracion(items []pb.ItemPack) []pb.ItemPack {
  98. for k, v := range items {
  99. duration := im.getItemDuration(v.ItemId)
  100. if duration > 0 {
  101. items[k].Count = -duration
  102. }
  103. }
  104. return items
  105. }
  106. func (im *itemmgr) getItemDuration(itemId int) int {
  107. im.lockDuration.RLock()
  108. d, ok := im.itemDurations[itemId]
  109. im.lockDuration.RUnlock()
  110. if !ok {
  111. return 0
  112. }
  113. return d
  114. }