package main import ( "fmt" "github.com/oylshe1314/framework/client/db" "github.com/oylshe1314/framework/errors" "github.com/oylshe1314/framework/server" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" ) type Account struct { Id string `bson:"_id"` AccountId uint64 `bson:"accountid"` } type MergeConfig struct { AccountCollection string `json:"accountCollection"` PlayerCollection string `json:"playerCollection"` OriginServers []uint16 `json:"originServers"` TargetServer uint16 `json:"targetServer"` RepeatedNickname bool `json:"repeatedNickname"` } type MergeClient struct { db.MongoClient config *MergeConfig } func (this *MergeClient) WithMergeConfig(config *MergeConfig) { this.config = config } func (this *MergeClient) Init() (err error) { if this.config == nil { return errors.Error("'mergeConfig' can not be nil") } if len(this.config.OriginServers) == 0 { return errors.Error("'mergeConfig.originServers' can not be empty") } if this.config.TargetServer == 0 { return errors.Error("'mergeConfig.targetServer' can not be 0") } if this.config.AccountCollection == "" { this.config.AccountCollection = "account" } if this.config.PlayerCollection == "" { this.config.PlayerCollection = "player" } err = this.MongoClient.Init() if err != nil { return err } fmt.Printf("mongodb %s/%s connected\n", this.MongoClient.Address(), this.MongoClient.Database()) return nil } func (this *MergeClient) copy() error { var baseUId = uint64(80000000000000) var tagSvrUid = baseUId + uint64(this.config.TargetServer)*100000000000 for _, originServer := range this.config.OriginServers { var svrUid = baseUId + uint64(originServer)*100000000000 cur, err := this.Collection(this.config.AccountCollection).Find(this.Context(), bson.M{"accountid": bson.M{"$gt": svrUid, "$lte": svrUid + 99999999999}}) if err != nil { return err } for cur.Next(this.Context()) { var account = new(Account) err = cur.Decode(account) if err != nil { return err } var player = bson.M{} err = this.Collection(this.config.PlayerCollection).FindOne(this.Context(), bson.M{"_id": account.AccountId}).Decode(player) if err != nil { if !errors.Is(err, mongo.ErrNoDocuments) { return err } continue } var newUid = tagSvrUid + (account.AccountId - svrUid) player["_id"] = newUid _, err = this.Collection(this.config.PlayerCollection).InsertOne(this.Context(), player) if err != nil { return err } //_, err = this.Collection(this.config.AccountCollection).UpdateByID(this.Context(), account.Id, bson.M{"$set": bson.M{"accountid": newUid}}) //if err != nil { // return err //} } } return nil } func main() { opt, err := server.ReadOptions("./config.json") if err != nil { fmt.Println(err) return } var client = &MergeClient{} err = opt.Init(client) if err != nil { fmt.Println(err) return } err = client.copy() if err != nil { fmt.Println(err) return } fmt.Println("Finished") }