dailyRecord.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/mdb"
  8. )
  9. const (
  10. DailyRecordNewRegistered = iota + 1
  11. DailyRecordLoggedIn
  12. DailyRecordNewLogin
  13. DailyRecordOldLogin
  14. DailyRecordActiveUsers
  15. DailyRecordNewActive
  16. DailyRecordOldActive
  17. DailyRecordTotalPoints
  18. DailyRecordUProduced
  19. DailyRecordUCashout
  20. )
  21. const (
  22. TotalUser = iota + 1
  23. UserLevel
  24. )
  25. type DailyRecord struct {
  26. Platform string `bson:"platform" json:"platform"` // platform: 用户所在的平台,例如“Android”或“IOS”
  27. Channel string `bson:"channel" json:"channel"` // channel: 用户来源渠道
  28. Daily int64 `bson:"daily" json:"daily"` //日期
  29. Registered int64 `bson:"registered" json:"registered"` // registered: 新用户注册的数量
  30. LoggedIn int64 `bson:"logged_in" json:"logged_in"` // logged_in: 登陆产品的用户数量
  31. NewLogin int64 `bson:"new_login" json:"new_login"` // new_login: 登陆产品的新注册用户数量
  32. OldLogin int64 `bson:"old_login" json:"old_login"` // old_login: 登陆产品的老用户数量
  33. ActiveUsers int64 `bson:"active_users" json:"active_users"` // active_users: 登录游戏后投掷过骰子的用户数量
  34. NewActive int64 `bson:"new_active" json:"new_active"` // new_active: 登录游戏后投掷过骰子的新用户数量
  35. OldActive int64 `bson:"old_active" json:"old_active"` // old_active: 登录游戏后投掷过骰子的老用户数量
  36. TotalPoints int64 `bson:"total_points" json:"total_points"` // total_points: 全服积分产出总值
  37. UProduced float64 `bson:"u_produced" json:"u_produced"` // u_produced: 全服U的产出总值
  38. UCashout float64 `bson:"u_cashout" json:"u_cashout"` // u_cashout: 全服U的提现总值
  39. CreatedAt int64 `bson:"createdAt" json:"createdAt"` // CreatedAt: 数据记录的时间戳
  40. UpdatedAt int64 `bson:"updatedAt" json:"updatedAt"`
  41. }
  42. // GetDailyRecordKey 当天
  43. func GetDailyRecordKey(platform, channel string, op int) string {
  44. Daily := mhayaTime.Now().DailyTOTimeStamp()
  45. return fmt.Sprintf("%v:%v:%s:%s:%v", constant.PlayerDailyKey, Daily, platform, channel, op)
  46. }
  47. // GetAppointDailyRecordKey 指定时间
  48. func GetAppointDailyRecordKey(platform, channel string, daily int64, op int) string {
  49. return fmt.Sprintf("%v:%v:%s:%s:%v", constant.PlayerDailyKey, daily, platform, channel, op)
  50. }
  51. // SetDailyRecord 设置当天指定键值数据
  52. func SetDailyRecord(platform, channel string, op, num int) {
  53. key := GetDailyRecordKey(platform, channel, op)
  54. mdb.RDB.IncrBy(context.Background(), key, int64(num))
  55. }
  56. func GetAppointDailyRecord(platform, channel string, daily int64, op int) (int64, error) {
  57. key := GetAppointDailyRecordKey(platform, channel, daily, op)
  58. return mdb.RDB.Get(context.Background(), key).Int64()
  59. }
  60. func SetDailyRecordUserHash(platform, channel, userName string, op int) {
  61. key := GetDailyRecordKey(platform, channel, op)
  62. mdb.RDB.HSet(context.Background(), key, userName, int64(1))
  63. }
  64. func GetAppointDailyRecordUserHash(platform, channel, userName string, daily int64, op int) (int64, error) {
  65. key := GetAppointDailyRecordKey(platform, channel, daily, op)
  66. return mdb.RDB.HGet(context.Background(), key, userName).Int64()
  67. }
  68. func GetDailyRecordLen(platform, channel string, op int) int64 {
  69. key := GetDailyRecordKey(platform, channel, op)
  70. return mdb.RDB.HLen(context.Background(), key).Val()
  71. }
  72. func GetAppointDailyRecordLen(platform, channel string, daily int64, op int) int64 {
  73. key := GetAppointDailyRecordKey(platform, channel, daily, op)
  74. return mdb.RDB.HLen(context.Background(), key).Val()
  75. }
  76. func GetTotalPlayerRecordKey(platform, channel string) string {
  77. return fmt.Sprintf("%v:%v:%v", constant.PlayerStatHKey, platform, channel)
  78. }
  79. func GetTotalPlayerLevelRecordKey(platform, channel string) string {
  80. return fmt.Sprintf("%v:%v:%v", constant.PlayerLevelStatHKey, platform, channel)
  81. }
  82. func GetTotalPlayerRecordLen(platform, channel string) int64 {
  83. key := GetTotalPlayerRecordKey(platform, channel)
  84. return mdb.RDB.HLen(context.Background(), key).Val()
  85. }
  86. func SetTotalPlayerRecord(platform, channel, userName string, level int) {
  87. key := GetTotalPlayerRecordKey(platform, channel)
  88. mdb.RDB.HSet(context.Background(), key, userName, int64(level))
  89. }
  90. func GetAllTotalPlayerRecord(platform, channel string) (map[string]string, error) {
  91. key := GetTotalPlayerRecordKey(platform, channel)
  92. return mdb.RDB.HGetAll(context.Background(), key).Result()
  93. }
  94. func GetServerRecordKey(nodeId int) string {
  95. return fmt.Sprintf("%v:%v", constant.ServerLoadHKey, nodeId)
  96. }
  97. func SetServerRecord(nodeId string, num int) {
  98. mdb.RDB.HSet(context.Background(), constant.ServerLoadHKey, nodeId, num)
  99. }
  100. func GetAllServerRecord() (map[string]string, error) {
  101. return mdb.RDB.HGetAll(context.Background(), constant.ServerLoadHKey).Result()
  102. }