fishuser.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package gamelogic
  2. import (
  3. "sync"
  4. "time"
  5. "bet24.com/log"
  6. item "bet24.com/servers/micros/item_inventory/proto"
  7. userservice "bet24.com/servers/micros/userservices/proto"
  8. )
  9. const IDLE_SECONDS = 300
  10. type special struct {
  11. fishkey int
  12. bulletOdds int
  13. specialEffect int
  14. specialType int
  15. specialPower int
  16. specialLimit int
  17. earn int
  18. }
  19. func (s *special) used(earn int) {
  20. s.specialLimit--
  21. s.earn += earn
  22. }
  23. type fishuser struct {
  24. userId int
  25. // 临时成绩
  26. consume int // 成绩缓存
  27. earn int
  28. // 我的概率
  29. extra_odds int
  30. isIndividualControl bool // 是否走个人奖池
  31. waterValue int // 水池值
  32. totalBetAmount int
  33. chan_score chan int
  34. delayWriteScore bool
  35. cannonPower int
  36. BulletId int
  37. BulletMax int
  38. CannonId int
  39. Essence int
  40. bulletMin int
  41. bulletMax int
  42. specials []*special
  43. lastAction int64
  44. newbieExtra *newbie_extra
  45. scoreLock *sync.RWMutex
  46. controlLock *sync.RWMutex
  47. }
  48. func newFishUser(userId int, bulletMin, bulletMax int, newbieExtra *newbie_extra) *fishuser {
  49. u := new(fishuser)
  50. u.chan_score = make(chan int)
  51. u.delayWriteScore = true
  52. u.userId = userId
  53. u.bulletMin = bulletMin
  54. u.bulletMax = bulletMax
  55. u.BulletMax = bulletMax
  56. u.setBullet(u.bulletMin)
  57. u.isIndividualControl = false
  58. u.extra_odds = 0
  59. u.waterValue = 0
  60. u.totalBetAmount = 0
  61. u.lastAction = time.Now().Unix()
  62. u.newbieExtra = newbieExtra
  63. u.scoreLock = &sync.RWMutex{}
  64. u.controlLock = &sync.RWMutex{}
  65. decos := userservice.GetUserDecoration(userId)
  66. for _, v := range decos {
  67. if v.Type == item.Item_Decoration_FishCannon {
  68. u.setCannon(v.ItemId, false)
  69. }
  70. }
  71. return u
  72. }
  73. func (fu *fishuser) isIdled() bool {
  74. return time.Now().Unix()-fu.lastAction > IDLE_SECONDS
  75. }
  76. func (fu *fishuser) doAction() {
  77. fu.lastAction = time.Now().Unix()
  78. }
  79. func (fu *fishuser) getUserOdds() int {
  80. return fu.getUserExtraOdds() + fu.cannonPower + fu.newbieExtra.getExtra(fu.userId)
  81. }
  82. func (fu *fishuser) addScore(amount int) {
  83. fu.doAction()
  84. fu.scoreLock.Lock()
  85. defer fu.scoreLock.Unlock()
  86. if amount > 0 {
  87. fu.earn += amount
  88. } else {
  89. fu.consume += amount
  90. }
  91. }
  92. func (fu *fishuser) getConsumeAndEarnAndClear() (int, int) {
  93. fu.scoreLock.Lock()
  94. defer fu.scoreLock.Unlock()
  95. c, e := fu.consume, fu.earn
  96. fu.consume = 0
  97. fu.earn = 0
  98. return c, e
  99. }
  100. func (fu *fishuser) setUserExtraOdd(odds int, isControl bool) {
  101. if fu == nil {
  102. return
  103. }
  104. fu.controlLock.Lock()
  105. defer fu.controlLock.Unlock()
  106. fu.extra_odds = odds
  107. fu.isIndividualControl = isControl
  108. }
  109. func (fu *fishuser) getUserExtraOdds() int {
  110. fu.controlLock.RLock()
  111. defer fu.controlLock.RUnlock()
  112. return fu.extra_odds
  113. }
  114. func (fu *fishuser) isInControl() bool {
  115. fu.controlLock.RLock()
  116. defer fu.controlLock.RUnlock()
  117. return fu.isIndividualControl
  118. }
  119. func (fu *fishuser) setWaterPoolValue(waterValue, betAmount int) {
  120. fu.waterValue += waterValue
  121. fu.totalBetAmount += betAmount
  122. }
  123. func (fu *fishuser) getWaterPoolValueAndClear() (int, int) {
  124. v := fu.waterValue
  125. b := fu.totalBetAmount
  126. fu.waterValue = 0
  127. fu.totalBetAmount = 0
  128. return v, b
  129. }
  130. func (fu *fishuser) setBullet(bulletID int) (isChanged bool) {
  131. isChanged = false
  132. // 检查一下限制
  133. if fu.bulletMin > 0 && bulletID < fu.bulletMin {
  134. log.Debug("fishuser.setBullet bulletID[%d] < fu.bulletMin[%d]", bulletID, fu.bulletMin)
  135. return
  136. }
  137. if fu.bulletMax > 0 && bulletID > fu.bulletMax {
  138. log.Debug("fishuser.setBullet bulletID[%d] > fu.bulletMax[%d]", bulletID, fu.bulletMax)
  139. return
  140. }
  141. if bulletID > fu.BulletMax {
  142. log.Debug("fishuser.setBullet bulletID[%d] > fu.BulletMax[%d]", bulletID, fu.BulletMax)
  143. return
  144. }
  145. isChanged = fu.BulletId != bulletID
  146. fu.BulletId = bulletID
  147. fu.doAction()
  148. return
  149. }
  150. func (fu *fishuser) getBulletId() int {
  151. return fu.BulletId
  152. }
  153. func (fu *fishuser) getMaxBulletId() int {
  154. return fu.BulletMax
  155. }
  156. func (fu *fishuser) setMaxBulletId(value int) {
  157. fu.BulletMax = value
  158. fu.setBullet(value)
  159. }
  160. func (fu *fishuser) setCannon(cannon int, check bool) (isChanged bool) {
  161. fu.doAction()
  162. isChanged = false
  163. if check && !userservice.SetUserDecoration(fu.userId, item.Item_Decoration_FishCannon, cannon) {
  164. return
  165. }
  166. isChanged = true
  167. itm := item.GetItem(cannon)
  168. if itm != nil {
  169. fu.cannonPower = itm.ExtraPower
  170. }
  171. fu.CannonId = cannon
  172. return
  173. }
  174. func (fu *fishuser) addSpecial(fishkey, bulletOdds, specialEffect, specialType, specialPower, specialLimit int) {
  175. log.Debug("fishuser addSpecial %d,%d,%d", fishkey, bulletOdds, specialEffect)
  176. fu.specials = append(fu.specials, &special{fishkey, bulletOdds, specialEffect, specialType, specialPower, specialLimit, 0})
  177. }
  178. func (fu *fishuser) clearSpecial() {
  179. fu.specials = nil
  180. }
  181. func (fu *fishuser) removeSpecial(fishkey int) int {
  182. for i := 0; i < len(fu.specials); {
  183. if fu.specials[i].fishkey == fishkey {
  184. earn := fu.specials[i].earn
  185. fu.specials = append(fu.specials[:i], fu.specials[i+1:]...)
  186. return earn
  187. } else {
  188. i++
  189. }
  190. }
  191. return 0
  192. }
  193. func (fu *fishuser) useSpecial(fishkey int) (int, int) {
  194. if len(fu.specials) == 0 {
  195. return 0, 0
  196. }
  197. for _, v := range fu.specials {
  198. if v.fishkey != fishkey {
  199. continue
  200. }
  201. if v.specialLimit > 0 {
  202. return v.bulletOdds, v.specialPower
  203. }
  204. }
  205. fu.doAction()
  206. return 0, 0
  207. }
  208. func (fu *fishuser) specialUsed(fishkey int, earn int) {
  209. fu.doAction()
  210. for _, v := range fu.specials {
  211. if v.fishkey == fishkey {
  212. v.used(earn)
  213. break
  214. }
  215. }
  216. }