Skip to content

Commit

Permalink
Updated NEX lib versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbarrow committed Apr 8, 2023
1 parent 873f9fb commit 54cbe60
Show file tree
Hide file tree
Showing 53 changed files with 1,114 additions and 742 deletions.
40 changes: 0 additions & 40 deletions create_matchmake_session.go

This file was deleted.

213 changes: 0 additions & 213 deletions database.go

This file was deleted.

23 changes: 23 additions & 0 deletions database/add_new_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package database

import (
"context"

"go.mongodb.org/mongo-driver/bson"
)

func AddNewUser(pid uint32) {
username := GetUsernameFromPID(pid)

filter := bson.D{
{"pid", pid},
{"missed_calls", bson.A{""}},
{"username", username},
{"status", "unallowed"},
}

_, err := usersCollection.InsertOne(context.TODO(), filter)
if err != nil {
panic(err)
}
}
21 changes: 21 additions & 0 deletions database/add_player_session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package database

import (
"context"

"go.mongodb.org/mongo-driver/bson"
)

func AddPlayerSession(pid uint32, urls []string, ip string, port string) {
filter := bson.D{
{"pid", pid},
{"urls", urls},
{"ip", ip},
{"port", port},
}

_, err := sessionsCollection.InsertOne(context.TODO(), filter)
if err != nil {
panic(err)
}
}
5 changes: 5 additions & 0 deletions database/connect_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package database

func ConnectAll() {
connectMongo()
}
41 changes: 41 additions & 0 deletions database/connect_mongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package database

import (
"context"
"os"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

var mongoClient *mongo.Client
var mongoContext context.Context
var accountDatabase *mongo.Database
var doorsDatabase *mongo.Database
var pnidCollection *mongo.Collection
var nexAccountsCollection *mongo.Collection
var regionsCollection *mongo.Collection
var usersCollection *mongo.Collection
var sessionsCollection *mongo.Collection
var callsCollection *mongo.Collection
var tourneysCollection *mongo.Collection

func connectMongo() {
mongoClient, _ = mongo.NewClient(options.Client().ApplyURI(os.Getenv("MONGO_URI")))
mongoContext, _ = context.WithTimeout(context.Background(), 10*time.Second)
_ = mongoClient.Connect(mongoContext)

accountDatabase = mongoClient.Database("pretendo")
pnidCollection = accountDatabase.Collection("pnids")
nexAccountsCollection = accountDatabase.Collection("nexaccounts")

doorsDatabase = mongoClient.Database("doors")
usersCollection = doorsDatabase.Collection("users")
sessionsCollection = doorsDatabase.Collection("sessions")
callsCollection = doorsDatabase.Collection("calls")

sessionsCollection.DeleteMany(context.TODO(), bson.D{})
callsCollection.DeleteMany(context.TODO(), bson.D{})
}
18 changes: 18 additions & 0 deletions database/delete_player_session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package database

import (
"context"

"go.mongodb.org/mongo-driver/bson"
)

func DeletePlayerSession(pid uint32) {
filter := bson.D{
{"pid", pid},
}

_, err := sessionsCollection.DeleteOne(context.TODO(), filter)
if err != nil {
panic(err)
}
}
27 changes: 27 additions & 0 deletions database/does_session_exist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package database

import (
"context"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func DoesSessionExist(pid uint32) bool {
var result bson.M
filter := bson.D{
{"pid", pid},
}

err := sessionsCollection.FindOne(context.TODO(), filter, options.FindOne()).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return false
} else {
panic(err)
}
} else {
return true
}
}
Loading

0 comments on commit 54cbe60

Please sign in to comment.