GameLogic.go 6.7 KB

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