From c472ead14b2181d5bd8c8eba8d7b387aa46c73b5 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Thu, 27 Oct 2022 18:46:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E7=AB=9E=E6=8A=80=E5=9C=BA?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/arena/modelarena.go | 6 ++++- sys/db/init_test.go | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/modules/arena/modelarena.go b/modules/arena/modelarena.go index d273af707..26217e000 100644 --- a/modules/arena/modelarena.go +++ b/modules/arena/modelarena.go @@ -243,7 +243,11 @@ func (this *modelArena) matchePlayer(uid string, dan, num int32) (results []*pb. cursor *mongo.Cursor ) results = make([]*pb.ArenaPlayer, 0) - if cursor, err = this.DBModel.DB.Find(comm.TableArena, bson.M{"uid": bson.M{"$ne": uid}, "dan": dan, "loc": bson.M{"$near": []int32{dan * 1000, rand.Int31n(100)}, "$maxDistance": 100}}, options.Find().SetSkip(0).SetLimit(int64(num))); err != nil { + if cursor, err = this.DBModel.DB.Find(comm.TableArena, bson.M{ + "uid": bson.M{"$ne": uid}, + "dan": dan, + "loc": bson.M{"$geoWithin": bson.M{"$center": bson.A{[]int32{dan * 1000, rand.Int31n(100)}, 100}}}, + }, options.Find().SetSkip(0).SetLimit(int64(num))); err != nil { this.module.Errorln(err) return } else { diff --git a/sys/db/init_test.go b/sys/db/init_test.go index cba39da08..babe29ee4 100644 --- a/sys/db/init_test.go +++ b/sys/db/init_test.go @@ -1,11 +1,18 @@ package db import ( + "context" "fmt" + "go_dreamfactory/comm" + "go_dreamfactory/lego/sys/mgo" + "go_dreamfactory/pb" + "math/rand" "sync" "testing" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo/options" ) func Test_Id(t *testing.T) { @@ -22,3 +29,43 @@ func Test_Id(t *testing.T) { } wg.Wait() } + +func Test_2D(t *testing.T) { + if sys, err := mgo.NewSys( + mgo.SetMongodbUrl("mongodb://10.0.0.9:10013"), + mgo.SetMongodbDatabase("dreamfactory4"), + ); err != nil { + fmt.Printf("err:%v", err) + return + } else { + port := []int32{2 * 1000, rand.Int31n(100)} + fmt.Printf("port:%v \n", port) + if cursor, err := sys.Find(comm.TableArena, bson.M{ + "dan": 2, + "loc": bson.M{ + "$geoWithin": bson.M{ + "$center": bson.A{port, 30}, + }, + }, + // bson.A{[]int32{2 * 1000, rand.Int31n(100)}, 100} + // "loc": bson.M{ + // "$near": bson.M{ + // "$geometry": bson.M{"type": "Point", "coordinates": []int32{dan * 1000, rand.Int31n(100)}}, + // "$maxDistance": 100, + // }, + // }, + }, options.Find().SetSkip(0).SetLimit(int64(5))); err != nil { + fmt.Printf("err:%v", err) + return + } else { + for cursor.Next(context.Background()) { + temp := &pb.DBArenaUser{} + if err = cursor.Decode(temp); err != nil { + fmt.Printf("err:%v", err) + return + } + fmt.Printf("temp:%v\n", temp.Uid) + } + } + } +}