matchInfo.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package sngmatch
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. item "bet24.com/servers/micros/item_inventory/proto"
  6. "bet24.com/servers/micros/matches/handler/matchbase"
  7. task "bet24.com/servers/micros/task/proto"
  8. "sync"
  9. "time"
  10. )
  11. type matchInfo struct {
  12. MatchId int
  13. MatchNo int
  14. matchInstance matchbase.MatchInstance
  15. EndTime int64
  16. StartTime int64
  17. createTime int64
  18. mm *sngmatchMgr
  19. lock *sync.RWMutex
  20. userFee map[int]item.ItemPack
  21. robotConfig *matchbase.Robot_config
  22. robotCount int
  23. userAwarded map[int]bool
  24. enrolledUsers []matchbase.EnrollUser
  25. }
  26. func newMatchInfo(matchId, matchNo int, instance matchbase.MatchInstance, mm *sngmatchMgr, robotConfig *matchbase.Robot_config) *matchInfo {
  27. ret := &matchInfo{
  28. MatchId: matchId,
  29. MatchNo: matchNo,
  30. matchInstance: instance,
  31. mm: mm,
  32. createTime: time.Now().Unix(),
  33. }
  34. ret.userFee = make(map[int]item.ItemPack)
  35. ret.userAwarded = make(map[int]bool)
  36. ret.lock = &sync.RWMutex{}
  37. ret.robotConfig = robotConfig
  38. if robotConfig != nil && robotConfig.Max > 0 {
  39. sec := robotConfig.GetWaitSec()
  40. time.AfterFunc(time.Second*time.Duration(sec), ret.checkRobot)
  41. }
  42. return ret
  43. }
  44. func (mi *matchInfo) getStatus() int {
  45. if mi.matchInstance == nil {
  46. return 0
  47. }
  48. return mi.matchInstance.GetStatus()
  49. }
  50. func (mi *matchInfo) addEnrollUser(userId int) {
  51. for k, v := range mi.enrolledUsers {
  52. if v.UserId == userId {
  53. mi.enrolledUsers[k].EnrollTime = time.Now().Unix()
  54. return
  55. }
  56. }
  57. mi.enrolledUsers = append(mi.enrolledUsers, matchbase.EnrollUser{UserId: userId, EnrollTime: time.Now().Unix()})
  58. }
  59. func (mi *matchInfo) isFull() bool {
  60. if mi.matchInstance == nil {
  61. return true
  62. }
  63. return mi.matchInstance.IsFull()
  64. }
  65. func (mi *matchInfo) isUserEnrolled(userId int) bool {
  66. if mi.matchInstance == nil {
  67. return false
  68. }
  69. return mi.matchInstance.IsUserEnrolled(userId)
  70. }
  71. func (mi *matchInfo) isUserPlaying(userId int) bool {
  72. if mi.getStatus() == matchbase.MatchStatus_Ended {
  73. return false
  74. }
  75. if !mi.isUserEnrolled(userId) {
  76. return false
  77. }
  78. instance := mi.matchInstance
  79. if instance == nil {
  80. return true
  81. }
  82. userlist := instance.GetUserList()
  83. for _, v := range userlist {
  84. if v == userId {
  85. return true
  86. }
  87. }
  88. return false
  89. }
  90. func (mi *matchInfo) isTimeout() bool {
  91. if mi.matchInstance == nil {
  92. return true
  93. }
  94. if mi.matchInstance.GetStatus() != matchbase.MatchStatus_Ended {
  95. return false
  96. }
  97. return time.Now().Unix()-mi.EndTime >= match_time_out_ended
  98. }
  99. func (mi *matchInfo) dump() {
  100. status := mi.getStatus()
  101. if status <= matchbase.MatchStatus_Free {
  102. log.Release(" MatchNo[%d] Status[Waiting] IsFull[%v] CreateTime[%s]",
  103. mi.MatchNo, mi.isFull(), common.TimeStampToString(mi.createTime))
  104. } else if status == matchbase.MatchStatus_Playing {
  105. log.Release(" MatchNo[%d] Status[Playing] StartTime[%s]",
  106. mi.MatchNo, common.TimeStampToString(mi.StartTime))
  107. } else {
  108. log.Release(" MatchNo[%d] Status[Ended] StartTime[%s] EndTime[%s]",
  109. mi.MatchNo, common.TimeStampToString(mi.StartTime), common.TimeStampToString(mi.EndTime))
  110. }
  111. }
  112. func (mi *matchInfo) checkRobot() {
  113. log.Debug("sngmatch.matchInfo.checkRobot robotCount = %d ", mi.robotCount)
  114. if mi.getStatus() > matchbase.MatchStatus_Free || mi.robotCount >= mi.robotConfig.Max {
  115. log.Debug("sngmatch.matchInfo.checkRobot playing or max[%d]", mi.robotConfig.Max)
  116. return
  117. }
  118. sec := mi.robotConfig.GetWaitSec()
  119. time.AfterFunc(time.Second*time.Duration(sec), mi.checkRobot)
  120. if mi.isFull() {
  121. log.Debug("sngmatch.matchInfo.checkRobot match is full")
  122. return
  123. }
  124. if mi.mm.addARobot(mi.MatchId) {
  125. mi.robotCount++
  126. } else {
  127. log.Debug("sngmatch.matchInfo.checkRobot addARobot failed")
  128. }
  129. }
  130. func (mi *matchInfo) sendNotification(userId int, data string) {
  131. if mi.matchInstance == nil {
  132. return
  133. }
  134. mi.matchInstance.SendNotification(userId, data)
  135. }
  136. func (mi *matchInfo) setUserFee(userId int, fee item.ItemPack) {
  137. mi.lock.Lock()
  138. mi.userFee[userId] = fee
  139. mi.lock.Unlock()
  140. }
  141. func (mi *matchInfo) getUserFee(userId int, remove bool) item.ItemPack {
  142. mi.lock.Lock()
  143. defer mi.lock.Unlock()
  144. ret, ok := mi.userFee[userId]
  145. if !ok {
  146. return item.ItemPack{}
  147. }
  148. if remove {
  149. delete(mi.userFee, userId)
  150. }
  151. return ret
  152. }
  153. func (mi *matchInfo) isUserAwarded(userId int) bool {
  154. mi.lock.RLock()
  155. defer mi.lock.RUnlock()
  156. ret, ok := mi.userAwarded[userId]
  157. if !ok {
  158. return false
  159. }
  160. return ret
  161. }
  162. func (mi *matchInfo) setUserAwarded(userId int) {
  163. mi.lock.Lock()
  164. defer mi.lock.Unlock()
  165. mi.userAwarded[userId] = true
  166. }
  167. func (mi *matchInfo) getUserList() []int {
  168. if mi.matchInstance == nil {
  169. return []int{}
  170. }
  171. return mi.matchInstance.GetUserList()
  172. }
  173. func (mi *matchInfo) getOnlineUserCount() int {
  174. if mi.matchInstance == nil {
  175. return 0
  176. }
  177. if mi.getStatus() == matchbase.MatchStatus_Ended {
  178. return 0
  179. }
  180. return len(mi.matchInstance.GetUserList())
  181. }
  182. func (mi *matchInfo) getUser(userId int) *matchbase.MatchUser {
  183. if mi.matchInstance != nil {
  184. return mi.matchInstance.GetUser(userId)
  185. }
  186. return nil
  187. }
  188. func (mi *matchInfo) onMatchStart() {
  189. userList := mi.getUserList()
  190. for _, v := range userList {
  191. task.DoTaskAction(v, task.TaskAction_playSNG, 1, task.TaskScope{})
  192. }
  193. }