dailyRecord.go 6.1 KB

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