record_manager.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package gamelogic
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/games/fish/fishcommon"
  5. "bet24.com/servers/insecureframe/frame"
  6. "sync"
  7. )
  8. type RecordManager struct {
  9. ServerName string // 服务器名
  10. RoomID int // 桌子号
  11. BatchID int // 序列号
  12. From int
  13. UserRecords map[int]*UserRecord
  14. lock *sync.RWMutex
  15. table frame.Table
  16. }
  17. func (this *RecordManager) newUserRecord(userID int) *UserRecord {
  18. this.lock.RLock()
  19. ur, ok := this.UserRecords[userID]
  20. this.lock.RUnlock()
  21. if ok {
  22. return ur
  23. }
  24. ur = new(UserRecord)
  25. ur.UserId = userID
  26. ur.ServerName = this.ServerName
  27. ur.RoomID = this.RoomID
  28. ur.BatchID = this.BatchID
  29. ur.From = this.From
  30. ur.Records = make(map[int]*RecordInfo)
  31. this.lock.Lock()
  32. this.UserRecords[userID] = ur
  33. this.lock.Unlock()
  34. return ur
  35. }
  36. func (this *RecordManager) getUserRecord(userID int) *UserRecord {
  37. return this.newUserRecord(userID)
  38. }
  39. func (this *RecordManager) addUserRecord(userID int, fishID int, consume int, ret int) {
  40. ur := this.getUserRecord(userID)
  41. ur.addRecord(fishID, consume, ret)
  42. }
  43. // 写入数据库
  44. func (this *RecordManager) flush() {
  45. this.lock.RLock()
  46. defer this.lock.RUnlock()
  47. if len(this.UserRecords) == 0 {
  48. log.Release("RecordManager.flush no record")
  49. return
  50. }
  51. log.Release("RecordManager.flush")
  52. for _, v := range this.UserRecords {
  53. v.flush(this.table)
  54. }
  55. }
  56. func newRecordManager(ServerName string, RoomID int, table frame.Table) *RecordManager {
  57. ret := new(RecordManager)
  58. ret.ServerName = ServerName
  59. ret.RoomID = RoomID
  60. ret.BatchID = fishcommon.GetBatchID()
  61. ret.From = fishcommon.GetTime()
  62. ret.UserRecords = make(map[int]*UserRecord)
  63. ret.lock = &sync.RWMutex{}
  64. ret.table = table
  65. return ret
  66. }
  67. func (this *RecordManager) dumpRecord() {
  68. this.lock.RLock()
  69. defer this.lock.RUnlock()
  70. log.Release("RecordManager.dumpRecord")
  71. for _, v := range this.UserRecords {
  72. log.Release(" User[%d]", v.UserId)
  73. log.Release(" %v", v.Records)
  74. }
  75. }