consecutive_userrecord.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package ladder
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. "encoding/json"
  6. "time"
  7. )
  8. const record_expire_seconds = 1800
  9. type userConsecutiveRecord struct {
  10. UserId int
  11. Records []*consecutiveRecord
  12. ping int64
  13. }
  14. func newUserConsecutiveRecord(userId int) *userConsecutiveRecord {
  15. record := getUserConsecutiveRecord(userId)
  16. ur := new(userConsecutiveRecord)
  17. ur.UserId = userId
  18. ur.refreshPing()
  19. err := json.Unmarshal([]byte(record), &ur.Records)
  20. if err != nil {
  21. log.Release("newUserConsecutiveRecord[%d] Unmarshal failed %s", userId, record)
  22. }
  23. return ur
  24. }
  25. func (ucr *userConsecutiveRecord) refreshPing() {
  26. ucr.ping = time.Now().Unix()
  27. }
  28. func (ucr *userConsecutiveRecord) isExpired() bool {
  29. return time.Now().Unix()-ucr.ping > record_expire_seconds
  30. }
  31. func (ucr *userConsecutiveRecord) getConsecutiveWinCount(gameId int) int {
  32. ucr.refreshPing()
  33. for _, v := range ucr.Records {
  34. if v.GameId == gameId || gameId == 0 {
  35. return v.getConsecutiveWinCount()
  36. }
  37. }
  38. return 0
  39. }
  40. func (ucr *userConsecutiveRecord) addRecord(score int, gameId int) {
  41. ucr.refreshPing()
  42. for _, v := range ucr.Records {
  43. if v.GameId == gameId || gameId == 0 {
  44. v.addRecord(score)
  45. // 写入数据库
  46. go ucr.updateRecordToDB()
  47. return
  48. }
  49. }
  50. // 没找到
  51. record := newConsecutiveRecord(gameId)
  52. record.addRecord(score)
  53. ucr.Records = append(ucr.Records, record)
  54. // 写入数据库
  55. go ucr.updateRecordToDB()
  56. }
  57. func (ucr *userConsecutiveRecord) getRecordByGameId(gameId int) *consecutiveRecord {
  58. for k, v := range ucr.Records {
  59. if v.GameId == gameId || gameId == 0 {
  60. return ucr.Records[k]
  61. }
  62. }
  63. log.Debug("ucr.userConsecutiveRecord.getRecordByGameId(%d) userId[%d] not found", gameId, ucr.UserId)
  64. return nil
  65. }
  66. // 移除最后一局输的
  67. func (ucr *userConsecutiveRecord) removeLastLost(gameId int) bool {
  68. ucr.refreshPing()
  69. r := ucr.getRecordByGameId(gameId)
  70. if r == nil {
  71. log.Release("userConsecutiveRecord.removeLastLost UserId [%d] gameId[%d] not found", ucr.UserId, gameId)
  72. return false
  73. }
  74. if r.removeLastLost() {
  75. // 写入数据库
  76. go ucr.updateRecordToDB()
  77. return true
  78. }
  79. return false
  80. }
  81. func (ucr *userConsecutiveRecord) updateRecordToDB() {
  82. d, _ := json.Marshal(ucr.Records)
  83. setUserConsecutiveRecord(ucr.UserId, string(d))
  84. }
  85. func (ucr *userConsecutiveRecord) dump() {
  86. for _, v := range ucr.Records {
  87. log.Release(" GameId:%d", v.GameId)
  88. for _, v1 := range v.Scores {
  89. log.Release(" Score[%d]Removed[%v]Time[%s]", v1.Score, v1.Removed, common.TimeStampToShortString(v1.Time))
  90. }
  91. }
  92. }