configmgr.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package config
  2. import (
  3. "bet24.com/log"
  4. pb "bet24.com/servers/micros/audioroom/proto"
  5. "encoding/json"
  6. )
  7. var Mgr *configMgr
  8. type configMgr struct {
  9. pb.RoomConfig // 房间配置
  10. taskConfigs []*pb.TaskConfig // 任务配置
  11. upgradeConfigs []*pb.UpgradeConfig // 升级配置
  12. user_taskConfigs []*pb.TaskConfig // 用户任务配置
  13. user_upgradeConfigs []*pb.UpgradeConfig // 升级配置
  14. gameConfigs []pb.GameConfig // 游戏配置(小游戏、开房游戏)
  15. gameIncomeConfigs []*pb.GameIncomeConfig // 游戏收益配置
  16. giftIncomeConfigs []*pb.GiftIncomeConfig // 礼物收益配置
  17. }
  18. func LoadConfigMgr() {
  19. Mgr = new(configMgr)
  20. Mgr.loadConfig()
  21. return
  22. }
  23. // 获取房间配置
  24. func (this *configMgr) GetRoomConfig() pb.RoomConfig {
  25. return this.RoomConfig
  26. }
  27. // 获取房间配置
  28. func (this *configMgr) GetRoomConfigJson() string {
  29. buf, _ := json.Marshal(this.RoomConfig)
  30. return string(buf)
  31. }
  32. // 获取默认语言
  33. func (this *configMgr) GetDefaultLanguage() string {
  34. language := ""
  35. if len(this.RoomConfig.Language) > 0 {
  36. language = this.RoomConfig.Language[0]
  37. }
  38. return language
  39. }
  40. // 获取系统任务配置
  41. func (this *configMgr) GetSysTask(sysFlag bool) []*pb.TaskConfig {
  42. if sysFlag {
  43. return this.taskConfigs
  44. }
  45. return this.user_taskConfigs
  46. }
  47. // 获取系统任务
  48. func (this *configMgr) GetTaskConfig(taskId int, sysFlag bool) *pb.TaskConfig {
  49. var list []*pb.TaskConfig
  50. if sysFlag {
  51. list = append(list, this.taskConfigs...)
  52. } else {
  53. list = append(list, this.user_taskConfigs...)
  54. }
  55. for _, v := range list {
  56. if v.Id == taskId {
  57. return v
  58. }
  59. }
  60. return nil
  61. }
  62. // 获取升级配置
  63. func (this *configMgr) GetUpgradeConfig(sysFlag bool) []*pb.UpgradeConfig {
  64. if sysFlag {
  65. return this.upgradeConfigs
  66. }
  67. return this.user_upgradeConfigs
  68. }
  69. // 根据经验获取等级
  70. func (this *configMgr) GetLevel(exps int, sysFlag bool) (level int) {
  71. var list []*pb.UpgradeConfig
  72. if sysFlag {
  73. list = append(list, this.upgradeConfigs...)
  74. } else {
  75. list = append(list, this.user_upgradeConfigs...)
  76. }
  77. for _, v := range list {
  78. if exps < v.Exps {
  79. break
  80. }
  81. level = v.Level
  82. }
  83. return
  84. }
  85. // 获取升级信息
  86. func (this *configMgr) GetUpgradeInfo(level int, sysFlag bool) *pb.UpgradeConfig {
  87. var list []*pb.UpgradeConfig
  88. if sysFlag {
  89. list = append(list, this.upgradeConfigs...)
  90. } else {
  91. list = append(list, this.user_upgradeConfigs...)
  92. }
  93. for _, v := range list {
  94. if v.Level == level {
  95. return v
  96. }
  97. }
  98. return nil
  99. }
  100. // 获取游戏列表
  101. func (this *configMgr) GetGameList() []pb.GameConfig {
  102. return this.gameConfigs
  103. }
  104. // 添加规则
  105. func (this *configMgr) AddGameRule(gameId int, ruleName string) {
  106. for k, v := range this.gameConfigs {
  107. if v.Id != gameId {
  108. continue
  109. }
  110. for _, rn := range v.GameRules {
  111. if rn == ruleName {
  112. return
  113. }
  114. }
  115. this.gameConfigs[k].GameRules = append(this.gameConfigs[k].GameRules, ruleName)
  116. return
  117. }
  118. }
  119. // 删除规则
  120. func (this *configMgr) RemoveGameRule(gameId int, ruleName string) {
  121. for k, v := range this.gameConfigs {
  122. if v.Id != gameId {
  123. continue
  124. }
  125. for i := 0; i < len(v.GameRules); i++ {
  126. if v.GameRules[i] == ruleName {
  127. this.gameConfigs[k].GameRules = append(this.gameConfigs[k].GameRules[:i], this.gameConfigs[k].GameRules[i+1:]...)
  128. return
  129. }
  130. }
  131. return
  132. }
  133. }
  134. func (this *configMgr) GetDefaultGameRule(gameId int) string {
  135. for _, v := range this.gameConfigs {
  136. if v.Id == gameId {
  137. if len(v.GameRules) > 0 {
  138. return v.GameRules[0]
  139. }
  140. return ""
  141. }
  142. }
  143. return ""
  144. }
  145. func (this *configMgr) IsGameRuleExist(gameId int, ruleName string) bool {
  146. for _, v := range this.gameConfigs {
  147. if v.Id != gameId {
  148. continue
  149. }
  150. for _, rn := range v.GameRules {
  151. if rn == ruleName {
  152. return true
  153. }
  154. }
  155. return false
  156. }
  157. return false
  158. }
  159. // 获取某款游戏信息
  160. func (this *configMgr) GetGameInfo(id int) pb.GameConfig {
  161. for _, v := range this.gameConfigs {
  162. if v.Id == id {
  163. return v
  164. }
  165. }
  166. return pb.GameConfig{}
  167. }
  168. // 获取游戏收益列表
  169. func (this *configMgr) GetGameIncomeList() []*pb.GameIncomeConfig {
  170. return this.gameIncomeConfigs
  171. }
  172. // 获取游戏收益率
  173. func (this *configMgr) GetGameIncomeRatio(gameId, itemType, level int) (ratio float64) {
  174. for _, info := range this.gameIncomeConfigs {
  175. // 游戏id
  176. if info.GameId != gameId {
  177. continue
  178. }
  179. // 最大返还率
  180. var maxRatio float64
  181. // 游戏类型
  182. switch info.GameType {
  183. case pb.GameType_Consume: // 1=消耗类型
  184. maxRatio = 1.00
  185. case pb.GameType_Rebate: // 2=返利类型
  186. maxRatio = 0.02
  187. }
  188. // 游戏收益
  189. for _, v := range info.Incomes {
  190. // 道具类型
  191. if v.ItemType != itemType {
  192. continue
  193. }
  194. // 等级
  195. if v.Level != level {
  196. continue
  197. }
  198. // 收益比率异常(防护)
  199. if v.Ratio < 0 || v.Ratio >= maxRatio {
  200. log.Error("config.Mgr.GetGameIncomeRatio gameId=%d gameType=%d itemType=%d level=%d ==>%+v",
  201. gameId, info.GameType, itemType, level, v)
  202. return
  203. }
  204. ratio = v.Ratio
  205. return
  206. }
  207. }
  208. return
  209. }
  210. // 获取礼物收益列表
  211. func (this *configMgr) GetGiftIncomeList() []*pb.GiftIncomeConfig {
  212. return this.giftIncomeConfigs
  213. }
  214. // 获取礼物收益率
  215. func (this *configMgr) GetGiftIncomeRatio(level, itemType int) (receiver, roomOwner float64) {
  216. for _, info := range this.giftIncomeConfigs {
  217. // 等级
  218. if info.Level != level {
  219. continue
  220. }
  221. // 礼物收益
  222. for _, v := range info.Incomes {
  223. // 道具类型
  224. if v.ItemType != itemType {
  225. continue
  226. }
  227. // 收益比率异常(防护)
  228. if v.Receiver < 0 || v.RoomOwner < 0 || v.Receiver >= 1 || v.RoomOwner >= 1 || v.Receiver+v.RoomOwner >= 1 {
  229. log.Error("config.Mgr.GetGiftIncomeRatio level=%d itemType=%d ==>%+v",
  230. level, itemType, v)
  231. return
  232. }
  233. receiver = v.Receiver
  234. roomOwner = v.RoomOwner
  235. return
  236. }
  237. }
  238. return
  239. }