account.go 5.3 KB

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