dailyRecord.go 5.7 KB

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