GameLogic.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package TribalSlot
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "sort"
  8. "sync"
  9. "time"
  10. "bet24.com/log"
  11. coreservice "bet24.com/servers/coreservice/client"
  12. "bet24.com/servers/games/slotcommon"
  13. "bet24.com/servers/games/slotcommon/slotcount"
  14. "bet24.com/servers/games/slotcommon/usermanager"
  15. )
  16. type GameLogic struct {
  17. MinBet int
  18. MaxBet int
  19. MinJackpot int64
  20. MaxJackpot int64
  21. MaxNoneFreeCount int
  22. Slots []Slot
  23. WinShpes []WinShape
  24. Wins []Win
  25. JackPotInfo JackPot
  26. FreeSpin FreeSpinInfo
  27. Special SpecialMultple
  28. TaxRate int
  29. AdBets []FafafaSlot_FreeAdBet
  30. lock *sync.RWMutex
  31. JackPotAmount int
  32. //origin_slots []Slot
  33. //Test []TestSlots
  34. //SlotCounts []SlotCount_Rate
  35. //defaultRate int // 默认的赔率
  36. //userFreeSpins map[int]*userFreeSpin
  37. slotSink slotcommon.SlotSink
  38. slotCommon *slotcommon.Slotcommon
  39. multipleCountMgr *slotcount.MultipleSlotCountManager
  40. }
  41. func NewGameLogic(slotSink slotcommon.SlotSink) *GameLogic {
  42. obj := new(GameLogic)
  43. obj.lock = &sync.RWMutex{}
  44. obj.MinJackpot = 1000000
  45. obj.MaxJackpot = 100000000
  46. obj.slotSink = slotSink
  47. return obj
  48. }
  49. func (this *GameLogic) run() {
  50. log.Color(LogColor, "TribalSlot GameLogic.run")
  51. rand.Seed(time.Now().UnixNano())
  52. this.refreshData()
  53. this.slotCommon = slotcommon.NewSlotCommon(this.slotSink, GAMEID, GAME_NAME, this.TaxRate, GAME_MESSAGE)
  54. }
  55. func (this *GameLogic) refreshData() {
  56. go this.initData()
  57. time.AfterFunc(5*time.Minute, this.refreshData)
  58. }
  59. func (this *GameLogic) initData() {
  60. data, err := os.ReadFile("slotconf/TribalSlot.json")
  61. if err != nil {
  62. log.Error("read TribalSlot.json failed")
  63. }
  64. this.lock.Lock()
  65. err = json.Unmarshal(data, &this)
  66. if err != nil {
  67. log.Error("Unmarshal TribalSlot.json failed err:%v", err)
  68. this.lock.Unlock()
  69. return
  70. }
  71. // 初始化所有的格字
  72. this.multipleCountMgr = slotcount.NewMultipleSlotCountManager("tribalslotcount_mul")
  73. // 把特殊玩法的概率排序
  74. this.Special.sortByMultiple()
  75. log.Color(LogColor, "%v", this.Special)
  76. this.FreeSpin.sort()
  77. log.Color(LogColor, "%v", this.FreeSpin)
  78. this.lock.Unlock()
  79. if this.MaxNoneFreeCount > 0 {
  80. usermanager.SetMaxNoneFreeCount(GAMEID, this.MaxNoneFreeCount)
  81. }
  82. this.JackPotAmount = coreservice.GetJackpotAmount(0, this.slotSink.IsChipRoom())
  83. sort.Slice(this.AdBets, func(i, j int) bool {
  84. return this.AdBets[i].AdCount > this.AdBets[j].AdCount
  85. })
  86. }
  87. func (this *GameLogic) getUserSlotCount(userId int, betAmount int) *slotcount.SlotCountManager {
  88. level := usermanager.GetUserReturnLevel(userId, GAMEID, betAmount)
  89. return this.multipleCountMgr.GetMgr(level)
  90. }
  91. func (this *GameLogic) getSlot(slotID int) Slot {
  92. for _, v := range this.Slots {
  93. if slotID == v.SlotID {
  94. return v
  95. }
  96. }
  97. log.Release("GameLogic.getSlot failed %d", slotID)
  98. var slot Slot
  99. return slot
  100. }
  101. func (this *GameLogic) get15Slots(userId int, isFree bool, betAmount int) []Slot {
  102. this.lock.RLock()
  103. defer this.lock.RUnlock()
  104. sc := this.getUserSlotCount(userId, betAmount)
  105. ret := make([]Slot, RESULT_COUNT)
  106. for i := 0; i < 15; {
  107. slotId := sc.GetOneSlot(0, i)
  108. if isFree && slotId == this.FreeSpin.SlotID {
  109. continue
  110. }
  111. ret[i] = this.getSlot(slotId)
  112. i++
  113. }
  114. return ret
  115. }
  116. func (this *GameLogic) getOneResult(slotID, slotCount, shapeID int) *Result {
  117. for _, v := range this.Wins {
  118. if v.SlotID != slotID {
  119. continue
  120. }
  121. for _, rate := range v.Rates {
  122. if rate.Count == slotCount {
  123. return &Result{SlotID: slotID, SlotCount: slotCount, WinShapeID: shapeID, WinRate: rate.Win}
  124. }
  125. }
  126. }
  127. return nil
  128. }
  129. func (this *GameLogic) getResult(userId int, betAmount int, isFree bool, controlType int) TribalSlot_Result {
  130. if isFree {
  131. controlType = 0
  132. }
  133. var ret TribalSlot_Result
  134. if betAmount <= 0 {
  135. log.Release("TribalSlot.GameLogic.GetResult betAmount = %d", betAmount)
  136. return ret
  137. }
  138. ret.Slots = make([]int, RESULT_COUNT)
  139. ret.BetAmount = betAmount
  140. // 取15个result
  141. slots := this.get15Slots(userId, isFree, betAmount)
  142. for k, v := range slots {
  143. ret.Slots[k] = v.SlotID
  144. }
  145. // 计算结果
  146. shapeCount := len(this.WinShpes)
  147. for k, v := range this.WinShpes {
  148. // 查看每条连线的数量
  149. slotID, slotCount, magicCount := v.getCount(slots)
  150. // 查看结果
  151. result := this.getOneResult(slotID, slotCount, k)
  152. if result != nil {
  153. // 如果中奖池了
  154. if result.WinRate == -1 {
  155. // jackpot不能有wild
  156. if magicCount == 0 {
  157. ret.Jackpot.WinShapeID = k
  158. ret.Jackpot.Amount = this.JackPotInfo.getBonus(betAmount, this.JackPotAmount)
  159. }
  160. } else {
  161. // 中奖了
  162. ret.WinAmount += betAmount * result.WinRate / shapeCount
  163. ret.Lines = append(ret.Lines, *result)
  164. }
  165. }
  166. }
  167. // 是否有特殊玩法
  168. ret.Special = this.Special.getSpecialResult(slots)
  169. ret.FreeSpin = this.FreeSpin.getFreeTime(slots)
  170. specialMultiple := ret.Special.WinRate1 + ret.Special.WinRate2
  171. if controlType == 1 && (ret.WinAmount > betAmount || ret.FreeSpin > 0 || specialMultiple > 1.0) {
  172. return this.getResult(userId, betAmount, isFree, controlType)
  173. }
  174. if controlType == 2 && ret.WinAmount < betAmount && ret.FreeSpin == 0 && specialMultiple < 1.0 {
  175. return this.getResult(userId, betAmount, isFree, controlType)
  176. }
  177. if ret.FreeSpin > 0 {
  178. this.addFreeSpin(userId, ret.FreeSpin, betAmount, false)
  179. }
  180. return ret
  181. }
  182. func (this *GameLogic) getJackPotAmount() int {
  183. this.lock.RLock()
  184. defer this.lock.RUnlock()
  185. return this.JackPotAmount
  186. }
  187. func (this *GameLogic) getSlots() []Slot {
  188. this.lock.RLock()
  189. defer this.lock.RUnlock()
  190. return this.Slots
  191. }
  192. func (this *GameLogic) getWinShapes() []WinShape {
  193. this.lock.RLock()
  194. defer this.lock.RUnlock()
  195. return this.WinShpes
  196. }
  197. func (this *GameLogic) useFreeSpin(userId int) (bool, int, bool) {
  198. return this.slotCommon.UseFreeSpin(userId)
  199. }
  200. func (this *GameLogic) getFreeSpinTime(userId int) int {
  201. return this.slotCommon.GetFreeSpinTime(userId)
  202. }
  203. func (this *GameLogic) userExit(userId int) {
  204. this.slotCommon.OnUserExit(userId)
  205. }
  206. func (this *GameLogic) getBetDesc(betAmount int) string {
  207. return fmt.Sprintf("bet %d", betAmount)
  208. }
  209. func (this *GameLogic) getResultDesc(result TribalSlot_Result) string {
  210. var total struct {
  211. Slots []int
  212. Lines []int
  213. Special int
  214. Jackpot int
  215. }
  216. total.Slots = result.Slots
  217. for _, v := range result.Lines {
  218. total.Lines = append(total.Lines, v.WinShapeID)
  219. }
  220. total.Jackpot = result.Jackpot.Amount
  221. total.Special = result.BetAmount * (result.Special.WinRate1 + result.Special.WinRate2)
  222. data, _ := json.Marshal(total)
  223. return string(data)
  224. }
  225. func (this *GameLogic) modifyJackpot(amount int, userId int) {
  226. newAmount := coreservice.ModifyJackpot(0, amount, userId, GAME_NAME, this.slotSink.IsChipRoom())
  227. this.lock.Lock()
  228. this.JackPotAmount = newAmount
  229. this.lock.Unlock()
  230. this.sendTribalSlotJackpot(userId)
  231. }
  232. func (this *GameLogic) addFreeSpin(userId int, freeCount, bet int, fromAd bool) {
  233. this.slotCommon.AddFreeSpin(userId, freeCount, bet, fromAd)
  234. }