dailyRecord.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package models
  2. import (
  3. "context"
  4. "fmt"
  5. mhayaTime "github.com/mhaya/extend/time"
  6. "github.com/mhaya/game/game_cluster/internal/constant"
  7. "github.com/mhaya/game/game_cluster/internal/data"
  8. "github.com/mhaya/game/game_cluster/internal/mdb"
  9. clog "github.com/mhaya/logger"
  10. "time"
  11. )
  12. const (
  13. DailyRecordNewRegistered = iota + 1
  14. DailyRecordLoggedIn
  15. DailyRecordNewLogin
  16. DailyRecordOldLogin
  17. DailyRecordActiveUsers
  18. DailyRecordNewActive
  19. DailyRecordOldActive
  20. DailyRecordTotalPoints
  21. DailyRecordUProduced
  22. DailyRecordUCashout
  23. DailyRecordTonCashout
  24. )
  25. const (
  26. TotalUser = iota + 1
  27. UserLevel
  28. )
  29. type DailyRecord struct {
  30. Platform string `bson:"platform" json:"platform"` // platform: 用户所在的平台,例如“Android”或“IOS”
  31. Channel string `bson:"channel" json:"channel"` // channel: 用户来源渠道
  32. Daily int64 `bson:"daily" json:"daily"` //日期
  33. Registered int64 `bson:"registered" json:"registered"` // registered: 新用户注册的数量
  34. LoggedIn int64 `bson:"logged_in" json:"logged_in"` // logged_in: 登陆产品的用户数量
  35. NewLogin int64 `bson:"new_login" json:"new_login"` // new_login: 登陆产品的新注册用户数量
  36. OldLogin int64 `bson:"old_login" json:"old_login"` // old_login: 登陆产品的老用户数量
  37. ActiveUsers int64 `bson:"active_users" json:"active_users"` // active_users: 登录游戏后投掷过骰子的用户数量
  38. NewActive int64 `bson:"new_active" json:"new_active"` // new_active: 登录游戏后投掷过骰子的新用户数量
  39. OldActive int64 `bson:"old_active" json:"old_active"` // old_active: 登录游戏后投掷过骰子的老用户数量
  40. TotalPoints int64 `bson:"total_points" json:"total_points"` // total_points: 全服积分产出总值
  41. UProduced float64 `bson:"u_produced" json:"u_produced"` // u_produced: 全服U的产出总值
  42. UCashout float64 `bson:"u_cashout" json:"u_cashout"` // u_cashout: 全服U的提现总值
  43. CreatedAt int64 `bson:"createdAt" json:"createdAt"` // CreatedAt: 数据记录的时间戳
  44. UpdatedAt int64 `bson:"updatedAt" json:"updatedAt"`
  45. }
  46. // GetDailyRecordKey 当天
  47. func GetDailyRecordKey(platform, channel string, op int) string {
  48. Daily := mhayaTime.Now().DailyTOTimeStamp()
  49. return fmt.Sprintf("%v:%v:%s:%s:%v", constant.PlayerDailyKey, Daily, platform, channel, op)
  50. }
  51. // GetAppointDailyRecordKey 指定时间
  52. func GetAppointDailyRecordKey(platform, channel string, daily int64, op int) string {
  53. return fmt.Sprintf("%v:%v:%s:%s:%v", constant.PlayerDailyKey, daily, platform, channel, op)
  54. }
  55. // SetDailyRecord 设置当天指定键值数据
  56. func SetDailyRecord(platform, channel string, op, num int) {
  57. key := GetDailyRecordKey(platform, channel, op)
  58. mdb.RDB.IncrBy(context.Background(), key, int64(num))
  59. }
  60. func GetAppointDailyRecord(platform, channel string, daily int64, op int) (int64, error) {
  61. key := GetAppointDailyRecordKey(platform, channel, daily, op)
  62. return mdb.RDB.Get(context.Background(), key).Int64()
  63. }
  64. func SetDailyRecordUserHash(platform, channel, userName string, op int) {
  65. key := GetDailyRecordKey(platform, channel, op)
  66. mdb.RDB.HSet(context.Background(), key, userName, int64(1))
  67. }
  68. func SetDailyRecordNewUserRegisterHash(platform, channel, userName, ip string, op int) {
  69. key := GetDailyRecordKey(platform, channel, op)
  70. mdb.RDB.HSet(context.Background(), key, userName, ip)
  71. }
  72. func SetAppointDailyRecordNewUserRegisterHash(platform, channel, userName, ip string, daily int64, op int) {
  73. key := GetAppointDailyRecordKey(platform, channel, daily, op)
  74. err := mdb.RDB.HSet(context.Background(), key, userName, ip).Err()
  75. if err != nil {
  76. clog.Errorf("set err:=%v", err)
  77. }
  78. }
  79. func GetAllDailyRecordNewUserRegister(platform, channel string, op int) (map[string]string, error) {
  80. key := GetDailyRecordKey(platform, channel, op)
  81. return mdb.RDB.HGetAll(context.Background(), key).Result()
  82. }
  83. func GetAppointDailyRecordUserHash(platform, channel string, daily int64, op int) (map[string]string, error) {
  84. key := GetAppointDailyRecordKey(platform, channel, daily, op)
  85. return mdb.RDB.HGetAll(context.Background(), key).Result()
  86. }
  87. func GetDailyRecordLen(platform, channel string, op int) int64 {
  88. key := GetDailyRecordKey(platform, channel, op)
  89. return mdb.RDB.HLen(context.Background(), key).Val()
  90. }
  91. func GetAppointDailyRecordLen(platform, channel string, daily int64, op int) int64 {
  92. key := GetAppointDailyRecordKey(platform, channel, daily, op)
  93. return mdb.RDB.HLen(context.Background(), key).Val()
  94. }
  95. func GetTotalPlayerRecordKey(platform, channel string) string {
  96. return fmt.Sprintf("%v:%v:%v", constant.PlayerStatHKey, platform, channel)
  97. }
  98. func GetTotalPlayerLevelRecordKey(platform, channel string) string {
  99. return fmt.Sprintf("%v:%v:%v", constant.PlayerLevelStatHKey, platform, channel)
  100. }
  101. func GetTotalPlayerRecordLen(platform, channel string) int64 {
  102. key := GetTotalPlayerRecordKey(platform, channel)
  103. return mdb.RDB.HLen(context.Background(), key).Val()
  104. }
  105. func SetTotalPlayerRecord(platform, channel, userName string, level int) {
  106. key := GetTotalPlayerRecordKey(platform, channel)
  107. mdb.RDB.HSet(context.Background(), key, userName, int64(level))
  108. }
  109. func GetAllTotalPlayerRecord(platform, channel string) (map[string]string, error) {
  110. key := GetTotalPlayerRecordKey(platform, channel)
  111. return mdb.RDB.HGetAll(context.Background(), key).Result()
  112. }
  113. func GetServerRecordKey(nodeId int) string {
  114. return fmt.Sprintf("%v:%v", constant.ServerLoadHKey, nodeId)
  115. }
  116. func SetServerRecord(nodeId string, num int) {
  117. mdb.RDB.HSet(context.Background(), constant.ServerLoadHKey, nodeId, num)
  118. }
  119. func GetAllServerRecord() (map[string]string, error) {
  120. return mdb.RDB.HGetAll(context.Background(), constant.ServerLoadHKey).Result()
  121. }
  122. func SetPlayerBlacklistIpRecord(ip, userName string) (int64, bool) {
  123. ret, ok := data.RuleConfig.Get(1)
  124. if !ok {
  125. return 0, false
  126. }
  127. key := fmt.Sprintf("%v:%v", constant.PlayerIpRecordKey, ip)
  128. mdb.RDB.HIncrBy(context.Background(), key, userName, 1)
  129. mdb.RDB.Expire(context.Background(), key, time.Duration(ret.MaxTime)*24*time.Hour)
  130. num := mdb.RDB.HLen(context.Background(), key).Val()
  131. if int(num) > ret.Num {
  132. return num, true
  133. }
  134. return 0, false
  135. }