gamesink.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package gamelogic
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "time"
  7. "bet24.com/redis"
  8. "bet24.com/servers/games/masharie_table/common"
  9. "bet24.com/servers/games/masharie_table/config"
  10. "bet24.com/servers/insecureframe/frame"
  11. )
  12. var gs *gamesink
  13. func newGameSink() *gamesink {
  14. gs = new(gamesink)
  15. return gs
  16. }
  17. type gamesink struct {
  18. }
  19. func (gs *gamesink) GetServerPort() int {
  20. return config.Server.ServerPort
  21. }
  22. func (gs *gamesink) GetGameID() int {
  23. return common.GAMEID
  24. }
  25. func (gs *gamesink) GetGameName() string {
  26. return common.GAME_NAME
  27. }
  28. func (gs *gamesink) GetRoomName() string {
  29. return config.Server.ServerName
  30. }
  31. func (gs *gamesink) CreateTableSink(table frame.Table, data string) frame.TableSink {
  32. return newTableSink(table, data)
  33. }
  34. func (gs *gamesink) GetOfflineSeconds() int64 {
  35. return 0
  36. }
  37. func (gs *gamesink) GetCertFile() string {
  38. return config.Server.CertFile
  39. }
  40. func (gs *gamesink) GetKeyFile() string {
  41. return config.Server.KeyFile
  42. }
  43. func (gs *gamesink) GetServerAddr() string {
  44. return config.Rooms.Rooms[0].ServerIP
  45. }
  46. func (gs *gamesink) GetRobotCount() int {
  47. if len(config.Rooms.Rooms[0].RobotConfig) == 0 {
  48. return 0
  49. }
  50. max := 0
  51. for _, v := range config.Rooms.Rooms[0].RobotConfig {
  52. if v.CountMax > max {
  53. max = v.CountMax
  54. }
  55. }
  56. return max
  57. }
  58. func (gs *gamesink) GetRobotGoldLimit() (min, max int) {
  59. if len(config.Rooms.Rooms[0].RobotConfig) == 0 {
  60. return 0, 0
  61. }
  62. hour := time.Now().Hour()
  63. for _, v := range config.Rooms.Rooms[0].RobotConfig {
  64. if gs.isBetween(hour, v.HourStart, v.HourEnd) {
  65. min, max = v.GoldMin, v.GoldMax
  66. return
  67. }
  68. }
  69. min, max = config.Rooms.Rooms[0].RobotConfig[0].GoldMin, config.Rooms.Rooms[0].RobotConfig[0].GoldMax
  70. return
  71. }
  72. func (gs *gamesink) GetRobotOnlineSec() int {
  73. if len(config.Rooms.Rooms[0].RobotConfig) == 0 {
  74. return 0
  75. }
  76. hour := time.Now().Hour()
  77. for _, v := range config.Rooms.Rooms[0].RobotConfig {
  78. if gs.isBetween(hour, v.HourStart, v.HourEnd) {
  79. return v.Online
  80. }
  81. }
  82. return config.Rooms.Rooms[0].RobotConfig[0].Online
  83. }
  84. func (gs *gamesink) GetRoomDatas() []string {
  85. var ret []string
  86. for _, v := range config.Rooms.Rooms {
  87. ret = append(ret, v.RoomName)
  88. }
  89. return ret
  90. }
  91. func (gs *gamesink) GetMinRoomCount(roomData string) int {
  92. for _, v := range config.Rooms.Rooms {
  93. if roomData == v.RoomName {
  94. return v.MinRoom
  95. }
  96. }
  97. return 0
  98. }
  99. func (gs *gamesink) GetRoomRobotCount(roomName string) int {
  100. for _, v := range config.Rooms.Rooms {
  101. if roomName != v.RoomName {
  102. continue
  103. }
  104. hour := time.Now().Hour()
  105. for _, v1 := range v.RobotConfig {
  106. if gs.isBetween(hour, v1.HourStart, v1.HourEnd) {
  107. return gs.getRandom(v1.CountMin, v1.CountMax)
  108. }
  109. }
  110. }
  111. return 0
  112. }
  113. func (gs *gamesink) GetRoomRobotGoldLimit(roomName string) (min, max int) {
  114. for _, v := range config.Rooms.Rooms {
  115. if roomName != v.RoomName {
  116. continue
  117. }
  118. hour := time.Now().Hour()
  119. for _, v1 := range v.RobotConfig {
  120. if gs.isBetween(hour, v1.HourStart, v1.HourEnd) {
  121. return v1.GoldMin, v1.GoldMax
  122. }
  123. }
  124. }
  125. return 0, 0
  126. }
  127. func (gs *gamesink) IsChipRoom() bool {
  128. return config.Server.IsChipRoom
  129. }
  130. func (gs *gamesink) OnPlatformConfig(key string) {
  131. config.OnConfigChanged(key)
  132. }
  133. func (gs *gamesink) IsPrivateRoom() bool {
  134. return config.Server.IsPrivateRoom
  135. }
  136. func (gs *gamesink) GetChairCount() int {
  137. return 1
  138. }
  139. func (gs *gamesink) getRandom(min, max int) int {
  140. if min >= max {
  141. return min
  142. }
  143. return min + rand.Intn(max-min)
  144. }
  145. func (gs gamesink) isBetween(p, min, max int) bool {
  146. return p >= min && p <= max
  147. }
  148. func (gs *gamesink) setOfflineStatus(userId int, offline bool, second int, isExpired bool) {
  149. keyName := fmt.Sprintf("UserGameStatus:UserID:%v:", userId)
  150. if offline {
  151. o := struct {
  152. GameName string
  153. RoomName string
  154. ServerAddr string
  155. GameId int
  156. }{
  157. GameName: gs.GetGameName(),
  158. RoomName: gs.GetRoomName(),
  159. ServerAddr: gs.GetServerAddr(),
  160. GameId: gs.GetGameID(),
  161. }
  162. d, _ := json.Marshal(o)
  163. if isExpired {
  164. redis.String_SetEx(keyName, string(d), int(second))
  165. } else {
  166. redis.String_Set(keyName, string(d))
  167. }
  168. } else {
  169. redis.Key_Del(keyName)
  170. }
  171. }
  172. func (gs *gamesink) GetRoomLevel(data string) int {
  173. return 0
  174. }
  175. func (gs *gamesink) GetVersionID() int {
  176. return config.Server.VersionID
  177. }