vitalitymgr.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package vitality
  2. import (
  3. "sync"
  4. "bet24.com/log"
  5. )
  6. type vitalitymgr struct {
  7. user_list map[int]*uservitality
  8. lock *sync.RWMutex
  9. }
  10. func NewVitalityMgr() *vitalitymgr {
  11. obj := new(vitalitymgr)
  12. obj.user_list = make(map[int]*uservitality)
  13. obj.lock = &sync.RWMutex{}
  14. log.Debug("vitalitymanager running")
  15. return obj
  16. }
  17. func (this *vitalitymgr) getUserInfo(userId int) *uservitality {
  18. this.lock.RLock()
  19. user, ok := this.user_list[userId]
  20. this.lock.RUnlock()
  21. if !ok {
  22. user = newUserVitality(userId)
  23. if user == nil {
  24. return nil
  25. }
  26. this.lock.Lock()
  27. this.user_list[userId] = user
  28. this.lock.Unlock()
  29. }
  30. return user
  31. }
  32. func (this *vitalitymgr) addPoint(userId, point int) int {
  33. user := this.getUserInfo(userId)
  34. if user == nil {
  35. log.Debug("vitality.addPoint user[%d] is not exist", userId)
  36. return 0
  37. }
  38. return user.addPoint(point)
  39. }
  40. func (vm *vitalitymgr) onUserEnter(userId int) {
  41. vm.lock.RLock()
  42. _, ok := vm.user_list[userId]
  43. vm.lock.RUnlock()
  44. if ok {
  45. return
  46. }
  47. usr := newUserVitality(userId)
  48. vm.lock.Lock()
  49. vm.user_list[userId] = usr
  50. vm.lock.Unlock()
  51. }
  52. func (vm *vitalitymgr) onUserExit(userId int) {
  53. vm.lock.Lock()
  54. defer vm.lock.Unlock()
  55. delete(vm.user_list, userId)
  56. }