account.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package models
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/go-redis/redis/v8"
  6. jsoniter "github.com/json-iterator/go"
  7. mhayaTime "github.com/mhaya/extend/time"
  8. "github.com/mhaya/game/game_cluster/internal/code"
  9. "github.com/mhaya/game/game_cluster/internal/constant"
  10. "github.com/mhaya/game/game_cluster/internal/data"
  11. "github.com/mhaya/game/game_cluster/internal/guid"
  12. "github.com/mhaya/game/game_cluster/internal/mdb"
  13. "github.com/mhaya/game/game_cluster/internal/param"
  14. initdata "github.com/mhaya/game/game_cluster/internal/third/tg_sign"
  15. clog "github.com/mhaya/logger"
  16. "go.mongodb.org/mongo-driver/bson"
  17. "go.mongodb.org/mongo-driver/mongo"
  18. "sort"
  19. "time"
  20. )
  21. type Account struct {
  22. UserName string `json:"username" bson:"userName"`
  23. Platform string `json:"platform" bson:"platform"`
  24. Account string `json:"account" bson:"account"`
  25. Channel string `json:"channel" bson:"channel"`
  26. IsPremium bool `json:"isPremium" bson:"isPremium"`
  27. LoginIp string `json:"loginIp" bson:"loginIp"`
  28. OpenId string `json:"openId" bson:"openId"`
  29. JoinIp string `json:"joinip" bson:"JoinIp"`
  30. JoinTime int64 `json:"jointime" bson:"JoinTime"`
  31. }
  32. type HomeData struct {
  33. Item ItemBasePack `json:"item"` //道具
  34. FirstItem FirstReward `json:"firstItem"` //首次登录奖励
  35. RocketLv RocketLvProgress `json:"rocketLvProgress"` //币安进度
  36. Dirty bool `json:"dirty" //是否关注tg`
  37. TotalIcorme int64 `json:"totalIcorme"`
  38. BtUserName string `json:"btUserName"` //机器人名字
  39. ChatIDName string `json:"chatIDName"` //频道名字
  40. Avatar string `json:"avatar"` //头像地址
  41. QuickLink string `json:"quickLink"` //快捷方式链接
  42. InviteStatus int `json:"inviteStatus"`
  43. }
  44. type RocketLvProgress struct {
  45. Max int `json:"max"`
  46. Cur int `json:"cur"`
  47. }
  48. func (ac *Account) AccountRegisterOrLogin(req *param.LoginReq) (*Account, int32) {
  49. var account Account
  50. ctx := context.Background()
  51. acc, err := mdb.RDB.Get(ctx, fmt.Sprintf("%v:%v", constant.CNameAccount, req.OpenID)).Bytes()
  52. if err != nil && err != redis.Nil {
  53. clog.Debugf("[Account] AccountRegisterOrLogin error: %v", err)
  54. }
  55. findFilter := bson.M{"openId": req.OpenID}
  56. if len(acc) == 0 {
  57. err = mdb.MDB.Collection(constant.CNameAccount).FindOne(context.Background(), findFilter).Decode(&account)
  58. if err != nil && err != mongo.ErrNoDocuments {
  59. clog.Errorf("Failed to AccountRegisterOrLogin select err request: %v err = %v", req, err.Error())
  60. return nil, code.LoginError
  61. }
  62. } else {
  63. err = jsoniter.Unmarshal(acc, &account)
  64. if err != nil {
  65. clog.Debugf("[Account] unmarshal AccountRegisterOrLogin data error: %v", err)
  66. return nil, code.LoginError
  67. }
  68. }
  69. // todo 测试代码
  70. curLvID, _ := mdb.RDB.Get(context.Background(), constant.RocketLvKey).Int()
  71. if curLvID < 100 {
  72. curLvID = 100
  73. mdb.RDB.Set(context.Background(), constant.RocketLvKey, curLvID, 0)
  74. }
  75. user := initdata.GetUserInfo(req.Sign)
  76. //设置IP 规则
  77. num, ok := SetPlayerBlacklistIpRecord(req.IP, req.OpenID)
  78. if ok {
  79. clog.Errorf("Failed to SetPlayerBlacklistIpRecord err request: %v,num:%v", req, num)
  80. return nil, code.LoginError
  81. }
  82. if account.OpenId != "" {
  83. if account.Platform == "on" {
  84. //统计新注册
  85. SetAppointDailyRecordNewUserRegisterHash(req.Platform, req.Channel, account.UserName, account.JoinIp, mhayaTime.CreateFromTimestamp(account.JoinTime).Unix(), DailyRecordNewRegistered)
  86. }
  87. if (len(user.Username) > 0 && account.Account != user.Username) || user.IsPremium != account.IsPremium || req.IP != account.LoginIp || account.Platform != req.Platform {
  88. account.Account = user.Username
  89. account.IsPremium = user.IsPremium
  90. account.LoginIp = req.IP
  91. account.Platform = req.Platform
  92. mdb.MDB.Collection(constant.CNameAccount).UpdateOne(context.Background(), findFilter, bson.M{"$set": bson.M{"platform": req.Platform, "account": user.Username, "isPremium": user.IsPremium, "loginIp": req.IP}})
  93. jsonData, _ := jsoniter.Marshal(&account)
  94. err = mdb.RDB.Set(context.Background(), fmt.Sprintf("%v:%v", constant.CNameAccount, account.OpenId), jsonData, time.Hour*3*24).Err()
  95. }
  96. return &account, code.OK
  97. }
  98. devAccountTable := &Account{
  99. UserName: guid.Next(),
  100. OpenId: req.OpenID,
  101. Platform: req.Platform,
  102. Channel: req.Channel,
  103. Account: user.Username,
  104. IsPremium: user.IsPremium,
  105. LoginIp: req.IP,
  106. JoinIp: req.IP,
  107. JoinTime: mhayaTime.Now().Unix(),
  108. }
  109. _, err = mdb.MDB.Collection(constant.CNameAccount).InsertOne(context.Background(), devAccountTable)
  110. if err != nil {
  111. clog.Errorf("Failed to AccountRegisterOrLogin request: %v err = %v", devAccountTable, err.Error())
  112. return nil, code.LoginError
  113. }
  114. jsonData, _ := jsoniter.Marshal(&devAccountTable)
  115. err = mdb.RDB.Set(context.Background(), fmt.Sprintf("%v:%v", constant.CNameAccount, devAccountTable.OpenId), jsonData, time.Hour*3*24).Err()
  116. //统计新注册
  117. SetDailyRecordNewUserRegisterHash(req.Platform, req.Channel, devAccountTable.UserName, req.IP, DailyRecordNewRegistered)
  118. // 新用户触发币安升级逻辑
  119. // 当前级别
  120. lvID, _ := mdb.RDB.Get(context.Background(), constant.RocketLvKey).Int()
  121. // 当前用户数
  122. userNum, _ := mdb.MDB.Collection(constant.CNamePlayer).CountDocuments(context.Background(), bson.M{})
  123. roConfig := data.RocketLvConfig.GetAll()
  124. // 当前经验
  125. var curExperience int64
  126. // 当前升级所需经验
  127. var curLvNeed int64
  128. keys := []int{}
  129. for _, row := range roConfig {
  130. keys = append(keys, row.ID)
  131. }
  132. sort.Ints(keys)
  133. for _, id := range keys {
  134. lvRow := roConfig[id]
  135. if id == lvID {
  136. curLvNeed = int64(lvRow.LvNeed)
  137. break
  138. }
  139. curExperience += int64(lvRow.LvNeed)
  140. }
  141. //curLvID, _ := mdb.RDB.Get(context.Background(), constant.RocketLvKey).Int()
  142. //if curLvID < 100 {
  143. // curLvID = 100
  144. //}
  145. if userNum-curExperience >= curLvNeed {
  146. // 触发升级
  147. // 并发控制
  148. if lvID == curLvID {
  149. mdb.RDB.Set(context.Background(), constant.RocketLvKey, curLvID+1, 0)
  150. }
  151. }
  152. return devAccountTable, code.OK
  153. }