From b871b4d3a44e13974aa4a0e3c4a7d488906e2c7b Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Fri, 28 May 2021 14:51:17 +0200 Subject: [PATCH] refactor: split MessageStructure into multiple messages, as a starting point for proper implementation of the login standard --- .../marvelous/gamelibrary/events/Event.java | 4 +- .../marvelous/gamelibrary/json/JSON.java | 15 +++--- .../gamelibrary/json/basic/BasicMessage.java | 23 +++------ .../EventMessage.java} | 6 +-- .../json/basic/RequestMessage.java | 50 +++++++++++++++++++ .../marvelous/gamelibrary/json/JSONTest.java | 7 ++- 6 files changed, 73 insertions(+), 32 deletions(-) rename src/main/java/uulm/teamname/marvelous/gamelibrary/json/{ingame/MessageStructure.java => basic/EventMessage.java} (92%) create mode 100644 src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/RequestMessage.java diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/events/Event.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/events/Event.java index b1d8bdf..14f0b74 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/events/Event.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/events/Event.java @@ -2,11 +2,11 @@ package uulm.teamname.marvelous.gamelibrary.events; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import uulm.teamname.marvelous.gamelibrary.json.ingame.EventDeserializer; -import uulm.teamname.marvelous.gamelibrary.json.ingame.MessageStructure; +import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage; import java.util.Objects; -/** Represents an abstract event sent inside a {@link MessageStructure} between client and server. */ +/** Represents an abstract event sent inside a {@link EventMessage} between client and server. */ @JsonDeserialize(using = EventDeserializer.class) public abstract class Event { public EventType type; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java index 233da8b..0d75cf6 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java @@ -3,30 +3,29 @@ package uulm.teamname.marvelous.gamelibrary.json; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jdk.jshell.spi.ExecutionControl; -import uulm.teamname.marvelous.gamelibrary.events.Event; -import uulm.teamname.marvelous.gamelibrary.json.ingame.MessageStructure; +import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage; /** Contains JSON encoding and decoding. */ public class JSON { - /** Deserializes an incoming network message into a {@link MessageStructure}. + /** Deserializes an incoming network message into a {@link EventMessage}. * @param input The JSON to deserialize. * @return The parsed message. */ - public static MessageStructure parse(String input) { - MessageStructure result = null; + public static EventMessage parse(String input) { + EventMessage result = null; ObjectMapper mapper = new ObjectMapper(); try { - result = mapper.readValue(input, MessageStructure.class); + result = mapper.readValue(input, EventMessage.class); } catch (JsonProcessingException e) { e.printStackTrace(); } return result; } - /** Serializes a {@link MessageStructure} into a JSON string. + /** Serializes a {@link EventMessage} into a JSON string. * @param input The message to serialize. * @return The message as JSON. */ - public static String stringify(MessageStructure input) throws ExecutionControl.NotImplementedException { + public static String stringify(EventMessage input) throws ExecutionControl.NotImplementedException { //TODO: implement JSON.stringify throw new ExecutionControl.NotImplementedException("JSON.stringify is not implemented"); } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/BasicMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/BasicMessage.java index e28dfc2..607323f 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/BasicMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/BasicMessage.java @@ -3,36 +3,29 @@ package uulm.teamname.marvelous.gamelibrary.json.basic; import uulm.teamname.marvelous.gamelibrary.json.MessageType; /** - * The basic message from the standard, containing all possible expansion keys. - * In other words, one needs to check the messageType, as fields that aren't sent - * are null. Note that ingame messages are not deserialized into the {@link BasicMessage}, but instead - * into the {@link uulm.teamname.marvelous.gamelibrary.json.ingame.MessageStructure}. + * The basic message from the standard, containing all possible expansion keys. In other words, one needs to check the + * messageType, as fields that aren't sent are null. Note that ingame messages are not deserialized into the {@link + * BasicMessage}, but instead into the {@link EventMessage}. */ public class BasicMessage { - /** - * The user ID is (apparently) a MD5 hash made from the username and device IP. It is assigned by the server. - */ + /** The user ID is (apparently) a MD5 hash made from the username and device IP. It is assigned by the server. */ String userID; - /** - * The messageType describes the type of message that might be sent - */ + /** The messageType describes the type of message that might be sent */ MessageType messageType; - /** - * I have no idea what this might be used for, even less the boolean in the answer - */ + /** I have no idea what this might be used for, even less the boolean in the answer */ String optionals; /** - * Exists only in a HelloClient message. + * Exists only in a HelloClient message. * Describes whether the user was in a running game before reconnecting. */ boolean runningGame; /** - * Exists only in GameAssignment and GeneralAssignment messages. + * Exists only in GameAssignment and GeneralAssignment messages. * Used for notifying the player of which party they have been assigned to */ String gameID; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/MessageStructure.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/EventMessage.java similarity index 92% rename from src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/MessageStructure.java rename to src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/EventMessage.java index 7ced3b1..bf95f31 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/MessageStructure.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/EventMessage.java @@ -1,4 +1,4 @@ -package uulm.teamname.marvelous.gamelibrary.json.ingame; +package uulm.teamname.marvelous.gamelibrary.json.basic; import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.json.MessageType; @@ -8,7 +8,7 @@ import java.util.HashMap; import java.util.Objects; /** Represents a message sent between client and server and contains all possible data. */ -public class MessageStructure { +public class EventMessage { /** This is the message type. It is either REQUESTS or EVENTS, everything else would be invalid here */ public MessageType messageType; @@ -26,7 +26,7 @@ public class MessageStructure { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - MessageStructure that = (MessageStructure) o; + EventMessage that = (EventMessage) o; return messageType == that.messageType && Arrays.equals(messages, that.messages) && Objects.equals(customContentType, that.customContentType) && Objects.equals(customContent, that.customContent); } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/RequestMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/RequestMessage.java new file mode 100644 index 0000000..ffcf5f6 --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/basic/RequestMessage.java @@ -0,0 +1,50 @@ +package uulm.teamname.marvelous.gamelibrary.json.basic; + +import uulm.teamname.marvelous.gamelibrary.events.Event; +import uulm.teamname.marvelous.gamelibrary.json.MessageType; +import uulm.teamname.marvelous.gamelibrary.requests.Request; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Objects; + +/** Represents a message sent between client and server and contains all possible data. */ +public class RequestMessage { + + /** This is the message type. It is either REQUESTS or EVENTS, everything else would be invalid here */ + public MessageType messageType; + + /** The list of {@link Event}s sent inside the message. */ + public Request[] messages; + + /** The type of the custom content sent with the message. */ + public String customContentType; + + /** The decoded contents of the custom content sent. */ + public HashMap customContent; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + RequestMessage that = (RequestMessage) o; + return messageType == that.messageType && Arrays.equals(messages, that.messages) && Objects.equals(customContentType, that.customContentType) && Objects.equals(customContent, that.customContent); + } + + @Override + public int hashCode() { + int result = Objects.hash(messageType, customContentType, customContent); + result = 31 * result + Arrays.hashCode(messages); + return result; + } + + @Override + public String toString() { + return "MessageStructure{" + + "messageType=" + messageType + + ", messages=" + Arrays.toString(messages) + + ", customContentType='" + customContentType + '\'' + + ", customContent=" + customContent + + '}'; + } +} diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java index e824afa..e70c8e2 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java @@ -8,12 +8,11 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.*; -import uulm.teamname.marvelous.gamelibrary.entities.Entity; import uulm.teamname.marvelous.gamelibrary.entities.EntityID; import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; import uulm.teamname.marvelous.gamelibrary.events.EventType; -import uulm.teamname.marvelous.gamelibrary.json.ingame.MessageStructure; +import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage; import java.util.HashMap; @@ -25,7 +24,7 @@ class JSONTest { String[] messageStructureEnd; /** Still need to add: messages, messageType */ - MessageStructure target; + EventMessage target; @BeforeEach void setUp() { @@ -56,7 +55,7 @@ class JSONTest { } """, }; - target = new MessageStructure(); + target = new EventMessage(); target.customContentType = "TestCustomContent"; target.customContent = new HashMap(); target.customContent.put("customKey", "customResult");