account.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. }
  40. type RocketLvProgress struct {
  41. Max int `json:"max"`
  42. Cur int `json:"cur"`
  43. }
  44. func (ac *Account) AccountRegisterOrLogin(req *param.LoginReq) (*Account, int32) {
  45. var account Account
  46. findFilter := bson.M{"openId": req.OpenID}
  47. err := mdb.MDB.Collection(constant.CNameAccount).FindOne(context.Background(), findFilter).Decode(&account)
  48. if err != nil && err != mongo.ErrNoDocuments {
  49. clog.Errorf("Failed to AccountRegisterOrLogin select err request: %v err = %v", req, err.Error())
  50. return nil, code.LoginError
  51. }
  52. // todo 测试代码
  53. curLvID, _ := mdb.RDB.Get(context.Background(), constant.RocketLvKey).Int()
  54. if curLvID < 100 {
  55. curLvID = 100
  56. mdb.RDB.Set(context.Background(), constant.RocketLvKey, curLvID, 0)
  57. }
  58. user := initdata.GetUserInfo(req.Sign)
  59. //设置IP 规则
  60. num, ok := SetPlayerBlacklistIpRecord(req.IP, req.OpenID)
  61. if ok {
  62. clog.Errorf("Failed to SetPlayerBlacklistIpRecord err request: %v,num:%v", req, num)
  63. return nil, code.LoginError
  64. }
  65. if account.OpenId != "" {
  66. if account.Platform == "on" {
  67. //统计新注册
  68. SetAppointDailyRecordNewUserRegisterHash(req.Platform, req.Channel, account.UserName, account.JoinIp, mhayaTime.CreateFromTimestamp(account.JoinTime).Unix(), DailyRecordNewRegistered)
  69. }
  70. if (len(user.Username) > 0 && account.Account != user.Username) || user.IsPremium != account.IsPremium || req.IP != account.LoginIp || account.Platform != req.Platform {
  71. account.Account = user.Username
  72. account.IsPremium = user.IsPremium
  73. account.LoginIp = req.IP
  74. account.Platform = req.Platform
  75. 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}})
  76. }
  77. return &account, code.OK
  78. }
  79. devAccountTable := &Account{
  80. UserName: guid.Next(),
  81. OpenId: req.OpenID,
  82. Platform: req.Platform,
  83. Channel: req.Channel,
  84. Account: user.Username,
  85. IsPremium: user.IsPremium,
  86. LoginIp: req.IP,
  87. JoinIp: req.IP,
  88. JoinTime: mhayaTime.Now().Unix(),
  89. }
  90. accountStr, _ := jsoniter.MarshalToString(devAccountTable)
  91. err = mdb.RDB.Set(context.Background(), constant.CNameAccount, accountStr, 0).Err()
  92. _, err = mdb.MDB.Collection(constant.CNameAccount).InsertOne(context.Background(), devAccountTable)
  93. if err != nil {
  94. clog.Errorf("Failed to AccountRegisterOrLogin request: %v err = %v", devAccountTable, err.Error())
  95. return nil, code.LoginError
  96. }
  97. //统计新注册
  98. SetDailyRecordNewUserRegisterHash(req.Platform, req.Channel, devAccountTable.UserName, req.IP, DailyRecordNewRegistered)
  99. // 新用户触发币安升级逻辑
  100. // 当前级别
  101. lvID, _ := mdb.RDB.Get(context.Background(), constant.RocketLvKey).Int()
  102. // 当前用户数
  103. userNum, _ := mdb.MDB.Collection(constant.CNamePlayer).CountDocuments(context.Background(), bson.M{})
  104. roConfig := data.RocketLvConfig.GetAll()
  105. // 当前经验
  106. var curExperience int64
  107. // 当前升级所需经验
  108. var curLvNeed int64
  109. keys := []int{}
  110. for _, row := range roConfig {
  111. keys = append(keys, row.ID)
  112. }
  113. sort.Ints(keys)
  114. for _, id := range keys {
  115. lvRow := roConfig[id]
  116. if id == lvID {
  117. curLvNeed = int64(lvRow.LvNeed)
  118. break
  119. }
  120. curExperience += int64(lvRow.LvNeed)
  121. }
  122. //curLvID, _ := mdb.RDB.Get(context.Background(), constant.RocketLvKey).Int()
  123. //if curLvID < 100 {
  124. // curLvID = 100
  125. //}
  126. if userNum-curExperience >= curLvNeed {
  127. // 触发升级
  128. // 并发控制
  129. if lvID == curLvID {
  130. mdb.RDB.Set(context.Background(), constant.RocketLvKey, curLvID+1, 0)
  131. }
  132. }
  133. return devAccountTable, code.OK
  134. }