From dee6fdbf95467bbd726eb3724cbbcbd2ad136ace Mon Sep 17 00:00:00 2001 From: James Quinley Date: Fri, 23 Jun 2023 22:29:35 -0700 Subject: [PATCH] SERVER UPDATE: Matchmaking V1.1 ADDS LOBBY CREATION TWEAKS TO REQUEST DELETION TIMING BUGFIXES --- .../delegationServices.java | 131 +++++++++++++++++- src/main/java/com/jamesquinley/Main.java | 4 +- 2 files changed, 130 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/jamesquinley/DelegationServices/delegationServices.java b/src/main/java/com/jamesquinley/DelegationServices/delegationServices.java index 8e3361c..461ba51 100644 --- a/src/main/java/com/jamesquinley/DelegationServices/delegationServices.java +++ b/src/main/java/com/jamesquinley/DelegationServices/delegationServices.java @@ -5,10 +5,12 @@ 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.Random; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -66,7 +68,7 @@ public class delegationServices { 3 - Submit Lobby for posting 4 - Return posting and lobby key 5 - Submit Lobby de-listing request - 6 - Return de-lisitng result (return status is deleted:true;false) + 6 - Return de-listing result (return status is deleted:true;false) */ public static void delegationProcesser() { @@ -79,7 +81,7 @@ public class delegationServices { } int request = (int) Lrequest.get("type"); if (request == 1 || request == 3) { - if (request ==1 ) { + if (request == 1 ) { List lobbyNameList = new ArrayList<>(); List lobbyIPList = new ArrayList<>(); List lobbyKeyList = new ArrayList<>(); @@ -132,6 +134,126 @@ public class delegationServices { DeleteTargetRequestByID(ClientID); } } + if (request == 3 ) { + Document index = new Document(); + index = lobbyIndex.find(eq("indexref", "ocs_matchmakingidx")).first(); + List lobbyNameList = new ArrayList<>(); + List lobbyIPList = new ArrayList<>(); + List lobbyKeyList = new ArrayList<>(); + List 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) index.get("LobbyName"); + lobbyIPList = (List) index.get("LobbyIP"); + lobbyKeyList = (List) index.get("LobbyPrivateKey"); + lobbyVersion = (List) 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.contains(lobbyPostName)) { + //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) @@ -153,7 +275,7 @@ public class delegationServices { }; scheduler.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS); } - public static void init() { + public static void buildIndex() { BasicDBObject searchQueryT = new BasicDBObject(); BasicDBObject searchQueryF = new BasicDBObject(); BasicDBObject LobbyIndex = new BasicDBObject(); @@ -170,12 +292,15 @@ public class delegationServices { List lobbyNameList = new ArrayList<>(); List lobbyIPList = new ArrayList<>(); List lobbyKeyList = new ArrayList<>(); + List 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); diff --git a/src/main/java/com/jamesquinley/Main.java b/src/main/java/com/jamesquinley/Main.java index 3ec00a2..ca0d33c 100644 --- a/src/main/java/com/jamesquinley/Main.java +++ b/src/main/java/com/jamesquinley/Main.java @@ -1,6 +1,6 @@ package com.jamesquinley; -import static com.jamesquinley.DelegationServices.delegationServices.init; +import static com.jamesquinley.DelegationServices.delegationServices.buildIndex; /** I/O and lobby management backend for new sandbox multiplayer game, go team (Marci and I!) @@ -9,7 +9,7 @@ public class Main { public static void main(String[] args) throws InterruptedException { com.jamesquinley.DelegationServices.delegationServices.connectionInit(); System.out.println("Connected to NetJump server and Matchmaking Storage Server"); - init(); + buildIndex(); System.out.println("An index has been initialized, matchmaking is now enabled."); while (true) { isolatedTrigger();