serverinfo.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package handler
  2. import (
  3. "bet24.com/log"
  4. pb "bet24.com/servers/micros/privateroom/proto"
  5. "encoding/json"
  6. "time"
  7. )
  8. const (
  9. server_timeout_sec = 60
  10. )
  11. type gameRule_option struct {
  12. TargetOptions []int
  13. UserCountOptions []int
  14. PlayTimeOptions []int
  15. EnrollFeeOptions []int
  16. }
  17. type gameRule struct {
  18. GameId int
  19. Name string
  20. Desc string
  21. Data string
  22. Options gameRule_option
  23. }
  24. func (gr *gameRule) isDual(userCount int) bool {
  25. if userCount != 2 {
  26. log.Debug("gameRule.isDual userCount = %d", userCount)
  27. return false
  28. }
  29. if len(gr.Options.UserCountOptions) != 2 {
  30. log.Debug("gameRule.isDual len(gr.Options.UserCountOptions) = %d", len(gr.Options.UserCountOptions))
  31. return false
  32. }
  33. ret := gr.Options.UserCountOptions[1] == 4
  34. if !ret {
  35. log.Debug("gameRule.isDual gr.Options.UserCountOptions[1] != 4 = %d", gr.Options.UserCountOptions[1])
  36. }
  37. return ret
  38. }
  39. func (gr *gameRule) isOptionValid(target, userCount, playTime, enrollFee int, roomType string) bool {
  40. // target == 1 为
  41. if len(gr.Options.TargetOptions) > 0 && target > 1 {
  42. found := false
  43. for _, v := range gr.Options.TargetOptions {
  44. if v == target {
  45. found = true
  46. break
  47. }
  48. }
  49. if !found {
  50. return false
  51. }
  52. }
  53. if len(gr.Options.UserCountOptions) > 0 {
  54. found := false
  55. for _, v := range gr.Options.UserCountOptions {
  56. if v == userCount {
  57. found = true
  58. break
  59. }
  60. }
  61. if !found {
  62. return false
  63. }
  64. }
  65. if len(gr.Options.PlayTimeOptions) > 0 {
  66. found := false
  67. for _, v := range gr.Options.PlayTimeOptions {
  68. if v == playTime {
  69. found = true
  70. break
  71. }
  72. }
  73. if !found {
  74. return false
  75. }
  76. }
  77. if len(gr.Options.EnrollFeeOptions) > 0 && roomType == pb.RoomType_Normal {
  78. found := false
  79. for _, v := range gr.Options.EnrollFeeOptions {
  80. if v == enrollFee {
  81. found = true
  82. break
  83. }
  84. }
  85. if !found {
  86. return false
  87. }
  88. }
  89. return true
  90. }
  91. func (gr *gameRule) queryOptions() {
  92. err := json.Unmarshal([]byte(gr.Data), &gr.Options)
  93. if err != nil {
  94. log.Release("gameRule.queryOptions Unmarshal failed %s", gr.Data)
  95. }
  96. //log.Debug("gameRule.queryOptions options=%v", gr.Options)
  97. }
  98. type serverInfo struct {
  99. GameId int
  100. GameName string
  101. Addr string
  102. GameRules []gameRule
  103. OnlineUser int
  104. lastPing int64
  105. isOnline bool
  106. }
  107. func (si *serverInfo) isTimeout() bool {
  108. return time.Now().Unix()-si.lastPing >= server_timeout_sec
  109. }
  110. func (si *serverInfo) getRule(ruleName string) *gameRule {
  111. for i := 0; i < len(si.GameRules); i++ {
  112. if si.GameRules[i].Name == ruleName {
  113. return &si.GameRules[i]
  114. }
  115. }
  116. return nil
  117. }
  118. func (si *serverInfo) addGameRule(name, desc, data string) {
  119. for i := 0; i < len(si.GameRules); i++ {
  120. if si.GameRules[i].Name == name {
  121. si.GameRules[i].Desc = desc
  122. si.GameRules[i].Data = data
  123. si.GameRules[i].GameId = si.GameId
  124. si.GameRules[i].queryOptions()
  125. return
  126. }
  127. }
  128. newGameRule := gameRule{GameId: si.GameId, Name: name, Desc: desc, Data: data}
  129. newGameRule.queryOptions()
  130. si.GameRules = append(si.GameRules, newGameRule)
  131. }
  132. func (si *serverInfo) dump() {
  133. log.Release(" Server[%d][%s][%s] online[%d] idled[%d]", si.GameId, si.GameName, si.Addr, si.OnlineUser, time.Now().Unix()-si.lastPing)
  134. for k, v := range si.GameRules {
  135. log.Release(" Rule[%d]:%v", k, v)
  136. }
  137. log.Release(" ------")
  138. }
  139. func (si *serverInfo) updateOnline(online int) {
  140. si.isOnline = true
  141. si.lastPing = time.Now().Unix()
  142. si.OnlineUser = online
  143. }