/
new_room.go
46 lines (41 loc) · 1.02 KB
/
new_room.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
package database
import (
"context"
"math/rand"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func NewRoom(host uint32, gamemode uint32, public bool, region uint32, gameconfig uint32, playercount uint32, dlcmode uint32) uint32 {
var gatheringId uint32
var result bson.M
for true {
gatheringId = rand.Uint32() % 500000
err := roomsCollection.FindOne(context.TODO(), bson.D{{"gid", gatheringId}}, options.FindOne()).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
break
} else {
panic(err)
}
} else {
continue
}
}
players := make([]int64, 12)
gatheringDoc := bson.D{
{"gid", gatheringId},
{"host", host},
{"gamemode", gamemode},
{"players", players},
{"public", public},
{"region", region},
{"gameconfig", gameconfig},
{"player_count", int64(0)},
{"dlc_mode", dlcmode}}
_, err := roomsCollection.InsertOne(context.TODO(), gatheringDoc)
if err != nil {
panic(err)
}
return gatheringId
}