/
LiquidUser.go
145 lines (125 loc) · 3.62 KB
/
LiquidUser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package Models
import (
"github.com/cesnow/LiquidEngine/Modules/LiquidSDK"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"math/rand"
"strconv"
"time"
)
type LiquidUser struct {
AutoId string `json:"auto_id" bson:"auto_id"`
InviteCode string `json:"invite_code" bson:"invite_code"`
Fingerprint string `json:"fingerprint" bson:"fingerprint"`
FromType string `json:"from_type" bson:"from_type"`
FromId string `json:"from_id" bson:"from_id"`
FromToken string `json:"from_token" bson:"from_token"`
IsDeactivate bool `json:"is_deactivate" bson:"is_deactivate"`
Create time.Time `json:"create" bson:"create"`
Update time.Time `json:"update" bson:"update"`
}
func FindLiquidGuestUser(autoId string) *LiquidUser {
filter := bson.M{"auto_id": autoId}
var user *LiquidUser
findUserErr := LiquidSDK.GetServer().GetLiquidUserCol().FindOne(nil, filter).Decode(&user)
if findUserErr != nil {
return nil
}
return user
}
func CreateLiquidUser(fromType string, fromId string) *LiquidUser {
autoId := GetAutoID()
if autoId == "" {
return nil
}
dateTime := time.Now()
inviteCode := getAutoIdToInviteCode(autoId)
fingerprint := ""
if fromId == "" {
fromId = autoId
}
liquidUser := &LiquidUser{
AutoId: autoId,
InviteCode: inviteCode,
Fingerprint: fingerprint,
FromType: fromType,
FromId: fromId,
IsDeactivate: false,
Create: dateTime,
Update: dateTime,
}
_, insertErr := LiquidSDK.GetServer().GetLiquidUserCol().InsertOne(nil, liquidUser)
if insertErr != nil {
return nil
}
createDefaultUserData(autoId)
liquidUser = FindLiquidGuestUser(autoId)
return liquidUser
}
func FindLiquidUserFromType(fromType string, fromId string) *LiquidUser {
filter := bson.M{"from_id": fromId, "from_type": fromType}
var user *LiquidUser
findUserErr := LiquidSDK.GetServer().GetLiquidUserCol().FindOne(nil, filter).Decode(&user)
if findUserErr != nil {
return nil
}
return user
}
func FindLiquidUserByAutoId(AutoId string, InviteCode string) *LiquidUser {
filter := bson.M{"auto_id": AutoId, "invite_code": InviteCode}
var user *LiquidUser
findUserErr := LiquidSDK.GetServer().GetLiquidUserCol().FindOne(nil, filter).Decode(&user)
if findUserErr != nil {
return nil
}
return user
}
func BindLiquidUser(AutoId string, FromId string, FromType string, FromToken string) (*mongo.UpdateResult, error) {
filter := bson.M{"auto_id": AutoId}
setBindData := bson.M{
"$set": bson.M{
"from_id": FromId,
"from_type": FromType,
"from_token": FromToken,
},
}
setBindResult, setBindResultErr := LiquidSDK.GetServer().GetLiquidUserCol().UpdateOne(
nil,
filter,
setBindData,
)
if setBindResultErr != nil {
return nil, setBindResultErr
}
return setBindResult, nil
}
var convertTable = [...]string{
"U", "V", "W", "X", "Y",
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"L", "M", "N", "Z", "K",
"P", "Q", "R", "S", "T",
}
func getAutoIdToInviteCode(autoId string) string {
inviteCode := ""
InviteCodeList := make([]int, 0)
autoIdInt, _ := strconv.ParseInt(autoId, 10, 64)
rand.Seed(autoIdInt + time.Now().UnixNano())
newId := strconv.FormatInt(time.Now().Unix()+rand.Int63n(time.Now().Unix()), 10)
newId = newId + autoId
newId2Int, _ := strconv.Atoi(newId[len(newId)-13:])
resultToBase := decimalToBase(InviteCodeList, newId2Int)
for _, base := range resultToBase {
inviteCode += convertTable[base]
}
return inviteCode
}
func decimalToBase(baseList []int, decimal int) []int {
base := len(convertTable)
baseList = append(baseList, decimal%base)
div := decimal / base
if div == 0 {
return baseList
}
return decimalToBase(baseList, div)
}