room_bullet.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package gamelogic
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/games/fish/bullet"
  5. "bet24.com/servers/games/fish/fishcommon"
  6. "sync"
  7. )
  8. type userBullet struct {
  9. bullet.Bullet
  10. odds int
  11. }
  12. type bullet_manager struct {
  13. bullet_list []*userBullet
  14. bulleyKey []int
  15. lock *sync.RWMutex
  16. }
  17. func newBulletManager() *bullet_manager {
  18. this := new(bullet_manager)
  19. this.bulleyKey = make([]int, fishcommon.SEAT_COUNT)
  20. for i := 1; i < fishcommon.SEAT_COUNT; i++ {
  21. this.bulleyKey[i] = i * 500000000
  22. }
  23. this.lock = &sync.RWMutex{}
  24. return this
  25. }
  26. func (this *bullet_manager) addBullet(userID int, bulletID int, angle int, fishkey int, chairID int, odds int) *bullet.Bullet {
  27. this.lock.Lock()
  28. defer this.lock.Unlock()
  29. this.bulleyKey[chairID]++
  30. b := bullet.Bullet{BulletID: bulletID, BulletKey: this.bulleyKey[chairID], Angle: angle, UserID: userID, FishKey: fishkey}
  31. this.bullet_list = append(this.bullet_list, &userBullet{Bullet: b, odds: odds})
  32. return &b
  33. }
  34. func (this *bullet_manager) getFlyingBullet(userId int) int {
  35. ret := 0
  36. this.lock.Lock()
  37. defer this.lock.Unlock()
  38. for _, v := range this.bullet_list {
  39. if v.UserID == userId {
  40. ret += v.odds
  41. }
  42. }
  43. return ret
  44. }
  45. func (this *bullet_manager) getBullet(bulletKey int) *userBullet {
  46. this.lock.RLock()
  47. defer this.lock.RUnlock()
  48. for _, v := range this.bullet_list {
  49. if v.BulletKey == bulletKey {
  50. return v
  51. }
  52. }
  53. return nil
  54. }
  55. func (this *bullet_manager) removeBullet(bulletKey int) bool {
  56. this.lock.Lock()
  57. defer this.lock.Unlock()
  58. for k, v := range this.bullet_list {
  59. if v.BulletKey == bulletKey {
  60. this.bullet_list = append(this.bullet_list[:k], this.bullet_list[k+1:]...)
  61. return true
  62. }
  63. }
  64. log.Release("removeBullet bulletKey(%d) not exist", bulletKey)
  65. return false
  66. }
  67. func (this *bullet_manager) removeUserBullet(userID int) int {
  68. this.lock.Lock()
  69. defer this.lock.Unlock()
  70. ret := 0
  71. for i := 0; i < len(this.bullet_list); {
  72. if this.bullet_list[i].UserID == userID {
  73. this.bullet_list = append(this.bullet_list[:i], this.bullet_list[i+1:]...)
  74. ret++
  75. } else {
  76. i++
  77. }
  78. }
  79. return ret
  80. }