319 lines
18 KiB
Java
319 lines
18 KiB
Java
package com.jamesquinley.DelegationServices;
|
|
import com.mongodb.*;
|
|
import com.mongodb.client.MongoClient;
|
|
import com.mongodb.client.MongoClients;
|
|
import com.mongodb.client.MongoCollection;
|
|
import com.mongodb.client.MongoDatabase;
|
|
import org.bson.Document;
|
|
import org.bson.conversions.Bson;
|
|
|
|
import javax.print.Doc;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.Random;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import static com.mongodb.client.model.Filters.eq;
|
|
import static java.util.concurrent.TimeUnit.SECONDS;
|
|
|
|
public class delegationServices {
|
|
static ConnectionString scReadconnectionString;
|
|
static MongoClientSettings scReadsettings;
|
|
static MongoClient dClient;
|
|
static MongoDatabase dClientDatabase;
|
|
static MongoCollection<Document> dCollection;
|
|
static MongoDatabase RGFilterDB;
|
|
static MongoCollection<Document> RGFilter;
|
|
static Document Lrequest = null;
|
|
public static void connectionInit() {
|
|
|
|
scReadconnectionString = new ConnectionString("mongodb+srv://putmoongodbnode");
|
|
//connects to database that production clients can use
|
|
System.out.println("This is a production server build (and will only work on that type)");
|
|
scReadsettings = MongoClientSettings.builder()
|
|
.applyConnectionString(scReadconnectionString)
|
|
.applyToSocketSettings(builder ->
|
|
builder.connectTimeout(5, SECONDS)
|
|
.readTimeout(6, SECONDS))
|
|
.build();
|
|
dClient = MongoClients.create(scReadsettings);
|
|
dClientDatabase = dClient.getDatabase("edgecomm");
|
|
dCollection = dClientDatabase.getCollection("nettranslate");
|
|
}
|
|
static ConnectionString AccountCon = new ConnectionString("mongodb://putmongodbdatabackend");
|
|
//mongodb://localhost:27017
|
|
//mongodb://account:removed@x.x.x.x:27017/account?retryWrites=true&w=majority
|
|
static MongoClientSettings AccountSettings = MongoClientSettings.builder()
|
|
.applyConnectionString(AccountCon)
|
|
.applyToSocketSettings(builder ->
|
|
builder.connectTimeout(5, SECONDS)
|
|
.readTimeout(6, SECONDS))
|
|
.build();
|
|
static MongoClient accountGetClient = MongoClients.create(AccountSettings);
|
|
static MongoDatabase gameServices = accountGetClient.getDatabase("gameservices");
|
|
static MongoCollection<Document> lobbyIndex = gameServices.getCollection("matchmaking");
|
|
static MongoDatabase database = accountGetClient.getDatabase("service");
|
|
static MongoCollection<Document> servicesStatus = database.getCollection("config");
|
|
static int RequestR;
|
|
//still need to config
|
|
|
|
/*
|
|
Request (Simple) Index
|
|
|
|
1 - Get all active lobbies
|
|
2 - Return All lobbies request
|
|
3 - Submit Lobby for posting
|
|
4 - Return posting and lobby key
|
|
5 - Submit Lobby de-listing request
|
|
6 - Return de-listing result (return status is deleted:true;false)
|
|
*/
|
|
|
|
public static void delegationProcesser() {
|
|
Document Filter;
|
|
Object ClientID = null;
|
|
try {
|
|
ClientID = Lrequest.get("_id");
|
|
} catch (Exception exrp) {
|
|
System.out.println("This request has opted out of CID.");
|
|
}
|
|
int request = (int) Lrequest.get("type");
|
|
if (request == 1 || request == 3) {
|
|
if (request == 1 ) {
|
|
List<String> lobbyNameList = new ArrayList<>();
|
|
List<String> lobbyIPList = new ArrayList<>();
|
|
List<String> lobbyKeyList = new ArrayList<>();
|
|
BasicDBObject requestModifier = new BasicDBObject();
|
|
requestModifier.append("_id", ClientID);
|
|
BasicDBObject updateQuery = new BasicDBObject();
|
|
BasicDBObject nameUpdate = new BasicDBObject();
|
|
BasicDBObject ipUpdate = new BasicDBObject();
|
|
BasicDBObject statusUpdate = new BasicDBObject();
|
|
BasicDBObject lobbyNum = new BasicDBObject();
|
|
|
|
|
|
Document index = new Document();
|
|
index = lobbyIndex.find(eq("indexref", "ocs_matchmakingidx")).first();
|
|
lobbyNameList = (List<String>) index.get("LobbyName");
|
|
lobbyIPList = (List<String>) index.get("LobbyIP");
|
|
//lobbyKeyList = (List<String>) index.get("LobbyPrivateKey");
|
|
System.out.println(lobbyNameList);
|
|
System.out.println(lobbyIPList);
|
|
if (lobbyNameList.stream().count() -1 ==0) {
|
|
nameUpdate.append("$set",
|
|
new BasicDBObject().append("lobbies", "No lobbies are available..."));
|
|
updateQuery.append("$set",
|
|
new BasicDBObject().append("type", 2));
|
|
lobbyNum.append("$set",
|
|
new BasicDBObject().append("LobbyCount", 0));
|
|
statusUpdate.append("$set",
|
|
new BasicDBObject().append("status", true));
|
|
dCollection.updateOne(requestModifier, updateQuery);
|
|
dCollection.updateOne(requestModifier,nameUpdate);
|
|
dCollection.updateOne(requestModifier,lobbyNum);
|
|
dCollection.updateOne(requestModifier, statusUpdate);
|
|
DeleteTargetRequestByID(ClientID);
|
|
} else {
|
|
updateQuery.append("$set",
|
|
new BasicDBObject().append("type", 2));
|
|
nameUpdate.append("$set",
|
|
new BasicDBObject().append("lobbies", lobbyNameList));
|
|
ipUpdate.append("$set",
|
|
new BasicDBObject().append("ip", lobbyIPList));
|
|
lobbyNum.append("$set",
|
|
new BasicDBObject().append("LobbyCount", (int) lobbyNameList.stream().count() -1));
|
|
statusUpdate.append("$set",
|
|
new BasicDBObject().append("status", true));
|
|
dCollection.updateOne(requestModifier, updateQuery);
|
|
dCollection.updateOne(requestModifier, nameUpdate);
|
|
dCollection.updateOne(requestModifier, ipUpdate);
|
|
dCollection.updateOne(requestModifier,lobbyNum);
|
|
dCollection.updateOne(requestModifier, statusUpdate);
|
|
DeleteTargetRequestByID(ClientID);
|
|
}
|
|
}
|
|
if (request == 3 ) {
|
|
Document index = new Document();
|
|
index = lobbyIndex.find(eq("indexref", "ocs_matchmakingidx")).first();
|
|
List<String> lobbyNameList = new ArrayList<>();
|
|
List<String> lobbyIPList = new ArrayList<>();
|
|
List<String> lobbyKeyList = new ArrayList<>();
|
|
List<String> lobbyVersion = new ArrayList<>();
|
|
String lobbyPostName = new String();
|
|
String lobbyPostIP = new String();
|
|
String lobbyPostVersion = new String();
|
|
String privateKeyNew = new String();
|
|
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
|
|
StringBuilder sb = new StringBuilder();
|
|
Random random = new Random();
|
|
boolean genStatus = false;
|
|
BasicDBObject requestModifier = new BasicDBObject();
|
|
requestModifier.append("indexref", "ocs_matchmakingidx");
|
|
BasicDBObject userRequestModifier = new BasicDBObject();
|
|
userRequestModifier.append("_id", ClientID);
|
|
//Define Variables ^
|
|
lobbyNameList = (List<String>) index.get("LobbyName");
|
|
lobbyIPList = (List<String>) index.get("LobbyIP");
|
|
lobbyKeyList = (List<String>) index.get("LobbyPrivateKey");
|
|
lobbyVersion = (List<String>) index.get("LobbyVersion");
|
|
lobbyPostName = (String) Lrequest.get("LobbyName");
|
|
lobbyPostIP = (String) Lrequest.get("LobbyIP");
|
|
lobbyPostVersion = (String) Lrequest.get("LobbyVersion");
|
|
BasicDBObject keyUpdate = new BasicDBObject();
|
|
BasicDBObject nameUpdate = new BasicDBObject();
|
|
BasicDBObject ipUpdate = new BasicDBObject();
|
|
BasicDBObject versionUpdate = new BasicDBObject();
|
|
BasicDBObject updateQuery = new BasicDBObject();
|
|
BasicDBObject nameReturn = new BasicDBObject();
|
|
BasicDBObject privateKey = new BasicDBObject();
|
|
BasicDBObject lobbyPostPermissionResponse = new BasicDBObject();
|
|
BasicDBObject statusUpdate = new BasicDBObject();
|
|
if (!lobbyNameList.toString().replaceAll(" ","").toLowerCase(Locale.ROOT).contains(lobbyPostName.replaceAll(" ","").toLowerCase(Locale.ROOT))) {
|
|
//Get existing lobby information from the index service, use these to append later ^
|
|
while (genStatus == false) {
|
|
int length = 50;
|
|
for (int i = 0; i < length; i++) {
|
|
int idIndex = random.nextInt(alphabet.length());
|
|
char randomChar = alphabet.charAt(idIndex);
|
|
sb.append(randomChar);
|
|
}
|
|
privateKeyNew = sb.toString();
|
|
if (!lobbyKeyList.contains(privateKeyNew)) {
|
|
genStatus = true;
|
|
}
|
|
}
|
|
//Generate the "private key" which is used to de-list via client request
|
|
lobbyKeyList.add(privateKeyNew);
|
|
lobbyIPList.add(lobbyPostIP);
|
|
lobbyNameList.add(lobbyPostName);
|
|
lobbyVersion.add(lobbyPostVersion);
|
|
keyUpdate.append("$set",
|
|
new BasicDBObject().append("LobbyPrivateKey", lobbyKeyList));
|
|
ipUpdate.append("$set",
|
|
new BasicDBObject().append("LobbyIP", lobbyIPList));
|
|
nameUpdate.append("$set",
|
|
new BasicDBObject().append("LobbyName", lobbyNameList));
|
|
versionUpdate.append("$set",
|
|
new BasicDBObject().append("LobbyVersion", lobbyVersion));
|
|
if (lobbyKeyList.stream().count() == lobbyNameList.stream().count() && lobbyVersion.stream().count() == lobbyIPList.stream().count()) {
|
|
lobbyIndex.updateOne(requestModifier,keyUpdate);
|
|
lobbyIndex.updateOne(requestModifier,ipUpdate);
|
|
lobbyIndex.updateOne(requestModifier,nameUpdate);
|
|
lobbyIndex.updateOne(requestModifier,versionUpdate);
|
|
//Indexes match up, send new index
|
|
updateQuery.append("$set",
|
|
new BasicDBObject().append("type", 4));
|
|
nameReturn.append("$set",
|
|
new BasicDBObject().append("lobbyName", lobbyPostName));
|
|
privateKey.append("$set",
|
|
new BasicDBObject().append("lobbySecret", privateKeyNew));
|
|
lobbyPostPermissionResponse.append("$set",
|
|
new BasicDBObject().append("lobbyPosted", true));
|
|
statusUpdate.append("$set",
|
|
new BasicDBObject().append("status", true));
|
|
dCollection.updateOne(userRequestModifier, updateQuery);
|
|
dCollection.updateOne(userRequestModifier, nameReturn);
|
|
dCollection.updateOne(userRequestModifier, privateKey);
|
|
dCollection.updateOne(userRequestModifier,lobbyPostPermissionResponse);
|
|
dCollection.updateOne(userRequestModifier, statusUpdate);
|
|
DeleteTargetRequestByID(ClientID);
|
|
} else {
|
|
buildIndex();
|
|
updateQuery.append("$set",
|
|
new BasicDBObject().append("type", 4));
|
|
nameReturn.append("$set",
|
|
new BasicDBObject().append("info", "The index failed to verify and has been reset, please try again."));
|
|
lobbyPostPermissionResponse.append("$set",
|
|
new BasicDBObject().append("lobbyPosted", false));
|
|
statusUpdate.append("$set",
|
|
new BasicDBObject().append("status", true));
|
|
dCollection.updateOne(userRequestModifier, updateQuery);
|
|
dCollection.updateOne(userRequestModifier, nameReturn);
|
|
dCollection.updateOne(userRequestModifier,lobbyPostPermissionResponse);
|
|
dCollection.updateOne(userRequestModifier, statusUpdate);
|
|
DeleteTargetRequestByID(ClientID);
|
|
//Something went wrong, the indexes don't match up, reinitialize the index, send client error
|
|
}
|
|
|
|
//Send new index to server before sending response "Good" or "Bad" (if something fails) ^
|
|
} else {
|
|
updateQuery.append("$set",
|
|
new BasicDBObject().append("type", 4));
|
|
nameReturn.append("$set",
|
|
new BasicDBObject().append("info", "This lobby name already exists."));
|
|
lobbyPostPermissionResponse.append("$set",
|
|
new BasicDBObject().append("lobbyPosted", false));
|
|
statusUpdate.append("$set",
|
|
new BasicDBObject().append("status", true));
|
|
dCollection.updateOne(userRequestModifier, updateQuery);
|
|
dCollection.updateOne(userRequestModifier, nameReturn);
|
|
dCollection.updateOne(userRequestModifier,lobbyPostPermissionResponse);
|
|
dCollection.updateOne(userRequestModifier, statusUpdate);
|
|
DeleteTargetRequestByID(ClientID);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public static void DeleteTargetRequestByID (Object ID)
|
|
{
|
|
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
|
final Runnable runnable = new Runnable() {
|
|
int countdownStarter = 5;
|
|
public void run() {
|
|
countdownStarter--;
|
|
if (countdownStarter <= 0) {
|
|
countdownStarter = 5;
|
|
BasicDBObject searchQuery = new BasicDBObject();
|
|
searchQuery.append("_id", ID);
|
|
dCollection.deleteMany(searchQuery);
|
|
System.out.println("Flushed a targeted request (CID) from the server");
|
|
scheduler.shutdown();
|
|
}
|
|
}
|
|
};
|
|
scheduler.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
|
|
}
|
|
public static void buildIndex() {
|
|
BasicDBObject searchQueryT = new BasicDBObject();
|
|
BasicDBObject searchQueryF = new BasicDBObject();
|
|
BasicDBObject LobbyIndex = new BasicDBObject();
|
|
searchQueryT.append("status", true);
|
|
searchQueryF.append("status", false);
|
|
LobbyIndex.append("indexref", "ocs_matchmakingidx");
|
|
dCollection.deleteMany(searchQueryT);
|
|
dCollection.deleteMany(searchQueryF);
|
|
System.out.println("Flushed NetJump Server");
|
|
lobbyIndex.deleteMany(LobbyIndex);
|
|
System.out.println("Flushed Matchmaking Index Server");
|
|
System.out.println("Creating Matchmaking Storage Index...");
|
|
Document lobbyIndexTemplate = new Document();
|
|
List<String> lobbyNameList = new ArrayList<>();
|
|
List<String> lobbyIPList = new ArrayList<>();
|
|
List<String> lobbyKeyList = new ArrayList<>();
|
|
List<String> lobbyVersionList = new ArrayList<>();
|
|
lobbyNameList.add("startatone");
|
|
lobbyIPList.add("startatone");
|
|
lobbyKeyList.add("startatone");
|
|
lobbyVersionList.add("startatone");
|
|
lobbyIndexTemplate.append("LobbyName",lobbyNameList);
|
|
lobbyIndexTemplate.append("LobbyIP", lobbyIPList);
|
|
lobbyIndexTemplate.append("LobbyPrivateKey",lobbyKeyList);
|
|
lobbyIndexTemplate.append("LobbyVersion",lobbyVersionList);
|
|
lobbyIndexTemplate.append("indexref", "ocs_matchmakingidx");
|
|
lobbyIndex.insertOne(lobbyIndexTemplate);
|
|
|
|
|
|
}
|
|
public static void matchmakingService() {
|
|
Lrequest = dCollection.find(eq("status", false)).first();
|
|
if (!(Lrequest == null)) {
|
|
delegationProcesser();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|