singlematchconfig.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package singlematch
  2. import (
  3. "bet24.com/log"
  4. item "bet24.com/servers/micros/item_inventory/proto"
  5. ladder "bet24.com/servers/micros/ladderservice/proto"
  6. "bet24.com/servers/micros/matches/handler/matchbase"
  7. pb "bet24.com/servers/micros/matches/proto"
  8. "math/rand"
  9. )
  10. type RobotMultiple struct {
  11. Odds int
  12. MultipleMin int
  13. MultipleMax int
  14. }
  15. func (rm *RobotMultiple) getMultiple() int {
  16. if rm.MultipleMin <= rm.MultipleMax {
  17. return rm.MultipleMin
  18. }
  19. return rm.MultipleMin + rand.Intn(rm.MultipleMax-rm.MultipleMin+1)
  20. }
  21. type RoundInfo struct {
  22. pb.SingleMatchRoundInfo
  23. EliminatUser int // 淘汰人数
  24. BaseScore int // 底分
  25. SetCount int // 局数,默认1
  26. ReviveCost item.ItemPack // 复活消耗,空表示不可复活
  27. EliminatByLose bool // 是否按胜负淘汰
  28. }
  29. type SingleMatchConfig struct {
  30. MatchId int
  31. Name string
  32. Desc string
  33. GameId int
  34. GameName string
  35. GameRule string // 从privateroom取
  36. InitScore int // 初始分数
  37. TableUser int
  38. PlayTime int
  39. EnrollFee item.ItemPack
  40. ScoreShrinkPercent int // 分数衰减至下一轮
  41. RankPrizes []matchbase.Range_Rank_Prize
  42. RobotMultiples []RobotMultiple
  43. Rounds []RoundInfo
  44. LadderAdditionalPercent int // 天梯额外加成
  45. ReviveTimeoutSec int // 复活倒计时
  46. }
  47. func (sc *SingleMatchConfig) dump() {
  48. log.Release(" GameRule[%s]Init[%d]TableUser[%d]Fee[%d:%d]Shrink[%d]LAP[%d]",
  49. sc.GameRule, sc.InitScore, sc.TableUser, sc.EnrollFee.ItemId, sc.EnrollFee.Count, sc.ScoreShrinkPercent, sc.LadderAdditionalPercent)
  50. log.Release(" Prizes:")
  51. for _, v := range sc.RankPrizes {
  52. log.Release(" Rank[%d-%d]:%v", v.RankMin, v.RankMax, v.Prize)
  53. }
  54. log.Release(" Rounds:")
  55. for _, v := range sc.Rounds {
  56. t := ""
  57. if v.EliminatByLose {
  58. t = "[LOSE]"
  59. }
  60. log.Release(" User[%d-%d]BaseScore[%d]SetCount[%d]ReviveCost[%d:%d]Prize%v%s",
  61. v.TotalUser, v.TotalUser-v.EliminatUser, v.BaseScore, v.SetCount, v.ReviveCost.ItemId, v.ReviveCost.Count, v.Prize, t)
  62. }
  63. }
  64. func (sc *SingleMatchConfig) getRound(index int) *RoundInfo {
  65. if len(sc.Rounds) == 0 {
  66. log.Release("SingleMatchConfig.getRound no rounds has been configed")
  67. return nil
  68. }
  69. if index <= sc.Rounds[0].Index {
  70. return &sc.Rounds[0]
  71. }
  72. for k, v := range sc.Rounds {
  73. if index == v.Index {
  74. return &sc.Rounds[k]
  75. }
  76. }
  77. log.Release("SingleMatchConfig.getRound Round[%d] not found in rounds maxIndex = %d",
  78. index, sc.Rounds[len(sc.Rounds)-1].Index)
  79. return nil
  80. }
  81. func (sc *SingleMatchConfig) getRandomScore(roundIndex int) int {
  82. round := sc.getRound(roundIndex)
  83. if round == nil {
  84. log.Release("SingleMatchConfig.getRandomScore roundIndex[%d] not found", roundIndex)
  85. return 0
  86. }
  87. oddsTotal := 0
  88. for _, v := range sc.RobotMultiples {
  89. oddsTotal += v.Odds
  90. }
  91. if oddsTotal == 0 {
  92. log.Release("SingleMatchConfig.getRandomScore oddsTotal == 0")
  93. return 0
  94. }
  95. r := rand.Intn(oddsTotal)
  96. // 取倍数
  97. tmpOdds := 0
  98. for _, v := range sc.RobotMultiples {
  99. tmpOdds += v.Odds
  100. if r < tmpOdds {
  101. return v.getMultiple() * round.BaseScore
  102. }
  103. }
  104. log.Release("SingleMatchConfig.getRandomScore failure oddsTotal[%d],r[%d]", oddsTotal, r)
  105. return 0
  106. }
  107. func (sc *SingleMatchConfig) isFinalRound(roundIndex int) bool {
  108. roundCount := len(sc.Rounds)
  109. if roundCount == 0 {
  110. return false
  111. }
  112. return roundIndex >= sc.Rounds[roundCount-1].Index
  113. }
  114. func (sc *SingleMatchConfig) getFinalPrize(rank int) []item.ItemPack {
  115. var prizes []item.ItemPack
  116. for _, v := range sc.RankPrizes {
  117. if rank >= v.RankMin && rank <= v.RankMax {
  118. prizes = append(prizes, v.Prize...)
  119. break
  120. }
  121. }
  122. return prizes
  123. }
  124. func (sc *SingleMatchConfig) createRoundIndex() {
  125. sc.LadderAdditionalPercent = ladder.GetLadderRoomAdditionalPercent(sc.GameId, sc.GameRule)
  126. for i := 0; i < len(sc.Rounds); i++ {
  127. sc.Rounds[i].Index = i + 1
  128. }
  129. if sc.ReviveTimeoutSec == 0 {
  130. sc.ReviveTimeoutSec = 60
  131. }
  132. }