singlematchmanager.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package singlematch
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. micro_common "bet24.com/servers/micros/common"
  6. item "bet24.com/servers/micros/item_inventory/proto"
  7. privateroom "bet24.com/servers/micros/privateroom/proto"
  8. "fmt"
  9. "strconv"
  10. "sync"
  11. "time"
  12. )
  13. var matchMgr *singlematchmanager
  14. func getMatchManager() *singlematchmanager {
  15. if matchMgr == nil {
  16. matchMgr = new(singlematchmanager)
  17. matchMgr.ctor()
  18. }
  19. return matchMgr
  20. }
  21. type singlematchmanager struct {
  22. matchlist map[int]*singlematchinstance
  23. lock *sync.RWMutex
  24. }
  25. func (sm *singlematchmanager) ctor() {
  26. sm.matchlist = make(map[int]*singlematchinstance)
  27. sm.lock = &sync.RWMutex{}
  28. go privateroom.RegisterServerStatus(sm, sm, micro_common.GetServicePort(), "singlematchmanager")
  29. sm.checkTimeout()
  30. }
  31. func (sm *singlematchmanager) checkTimeout() {
  32. time.AfterFunc(30*time.Second, sm.checkTimeout)
  33. var toRemove []int
  34. sm.lock.Lock()
  35. for k, v := range sm.matchlist {
  36. if v.isTimeout() {
  37. toRemove = append(toRemove, k)
  38. }
  39. }
  40. sm.lock.Unlock()
  41. if len(toRemove) == 0 {
  42. return
  43. }
  44. log.Debug("singlematchmanager.checkTimeout removing %v", toRemove)
  45. sm.lock.Lock()
  46. for _, v := range toRemove {
  47. delete(sm.matchlist, v)
  48. }
  49. sm.lock.Unlock()
  50. }
  51. func (sm *singlematchmanager) dump(param1, param2 string) {
  52. log.Release("-------------------------------")
  53. log.Release("singlematch.singlematchmanager.dump[%s,%s]", param1, param2)
  54. defer func() {
  55. log.Release("+++++++++++++++++++++++++++++++")
  56. log.Release("")
  57. }()
  58. if param1 == "config" {
  59. getConfigManager().dumpSys(param2)
  60. return
  61. }
  62. if param1 == "user" {
  63. userId, err := strconv.Atoi(param2)
  64. if err != nil {
  65. log.Release(" invalid param %s", param2)
  66. return
  67. }
  68. m := sm.getUserMatch(userId)
  69. if m == nil {
  70. log.Release(" user[%d] has no match playing", userId)
  71. return
  72. }
  73. m.dump()
  74. return
  75. }
  76. sm.lock.RLock()
  77. for k, v := range sm.matchlist {
  78. log.Release(" UserId[%d]", k)
  79. v.dump()
  80. log.Release(" -------------")
  81. }
  82. sm.lock.RUnlock()
  83. }
  84. func (sm *singlematchmanager) getUserMatch(userId int) *singlematchinstance {
  85. sm.lock.RLock()
  86. si, _ := sm.matchlist[userId]
  87. sm.lock.RUnlock()
  88. if si != nil {
  89. si.ReviveSecs = si.calReviveTimeout()
  90. }
  91. return si
  92. }
  93. func (sm *singlematchmanager) createMatch(userId int, matchId int) (string, *singlematchinstance) {
  94. existMatch := sm.getUserMatch(userId)
  95. if existMatch != nil && !existMatch.isEnded() {
  96. log.Release("singlematchmanager.createMatch failed,already exist user match [%d]", userId)
  97. return "playing", nil
  98. }
  99. matchConfig := getConfigManager().getMatch(matchId)
  100. if matchConfig == nil {
  101. log.Release("newMatchInstance matchConfig not found[%d]", matchId)
  102. return "no config", nil
  103. }
  104. if matchConfig.EnrollFee.Count > 0 {
  105. ok, err := item.Consume(userId, matchConfig.EnrollFee.ItemId,
  106. common.LOGTYPE_SINGLEMATCH_TICKET, matchConfig.EnrollFee.Count, 0)
  107. if !ok {
  108. errMsg := fmt.Sprintf("not enough fee[%v] for play err = %s", matchConfig.EnrollFee, err)
  109. log.Release("singlematchmanager.createMatch failed %s", errMsg)
  110. return "not enough fee", nil
  111. }
  112. }
  113. mi := newMatchInstance(userId, matchConfig)
  114. sm.lock.Lock()
  115. sm.matchlist[userId] = mi
  116. sm.lock.Unlock()
  117. mi.startRound()
  118. return "ok", mi
  119. }
  120. func (sm *singlematchmanager) revive(userId int) bool {
  121. um := sm.getUserMatch(userId)
  122. if um == nil {
  123. log.Release("singlematchmanager.revive userId[%d] match not found", userId)
  124. return false
  125. }
  126. return um.revive()
  127. }
  128. func (sm *singlematchmanager) noRevive(userId int) bool {
  129. um := sm.getUserMatch(userId)
  130. if um == nil {
  131. log.Release("singlematchmanager.noRevive userId[%d] match not found", userId)
  132. return false
  133. }
  134. return um.noRevive()
  135. }