refactor: split MessageStructure into multiple messages, as a starting point for proper implementation of the login standard

This commit is contained in:
Yannik Bretschneider 2021-05-28 14:51:17 +02:00
parent a92f437412
commit b871b4d3a4
6 changed files with 73 additions and 32 deletions

View File

@ -2,11 +2,11 @@ package uulm.teamname.marvelous.gamelibrary.events;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import uulm.teamname.marvelous.gamelibrary.json.ingame.EventDeserializer; 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; 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) @JsonDeserialize(using = EventDeserializer.class)
public abstract class Event { public abstract class Event {
public EventType type; public EventType type;

View File

@ -3,30 +3,29 @@ package uulm.teamname.marvelous.gamelibrary.json;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.jshell.spi.ExecutionControl; import jdk.jshell.spi.ExecutionControl;
import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage;
import uulm.teamname.marvelous.gamelibrary.json.ingame.MessageStructure;
/** Contains JSON encoding and decoding. /** Contains JSON encoding and decoding.
*/ */
public class JSON { 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. * @param input The JSON to deserialize.
* @return The parsed message. */ * @return The parsed message. */
public static MessageStructure parse(String input) { public static EventMessage parse(String input) {
MessageStructure result = null; EventMessage result = null;
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
try { try {
result = mapper.readValue(input, MessageStructure.class); result = mapper.readValue(input, EventMessage.class);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
} }
return result; return result;
} }
/** Serializes a {@link MessageStructure} into a JSON string. /** Serializes a {@link EventMessage} into a JSON string.
* @param input The message to serialize. * @param input The message to serialize.
* @return The message as JSON. */ * @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 //TODO: implement JSON.stringify
throw new ExecutionControl.NotImplementedException("JSON.stringify is not implemented"); throw new ExecutionControl.NotImplementedException("JSON.stringify is not implemented");
} }

View File

@ -3,26 +3,19 @@ package uulm.teamname.marvelous.gamelibrary.json.basic;
import uulm.teamname.marvelous.gamelibrary.json.MessageType; import uulm.teamname.marvelous.gamelibrary.json.MessageType;
/** /**
* The basic message from the standard, containing all possible expansion keys. * The basic message from the standard, containing all possible expansion keys. In other words, one needs to check the
* In other words, one needs to check the messageType, as fields that aren't sent * messageType, as fields that aren't sent are null. Note that ingame messages are not deserialized into the {@link
* are null. Note that ingame messages are not deserialized into the {@link BasicMessage}, but instead * BasicMessage}, but instead into the {@link EventMessage}.
* into the {@link uulm.teamname.marvelous.gamelibrary.json.ingame.MessageStructure}.
*/ */
public class BasicMessage { 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; 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; 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; String optionals;
/** /**

View File

@ -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.events.Event;
import uulm.teamname.marvelous.gamelibrary.json.MessageType; import uulm.teamname.marvelous.gamelibrary.json.MessageType;
@ -8,7 +8,7 @@ import java.util.HashMap;
import java.util.Objects; import java.util.Objects;
/** Represents a message sent between client and server and contains all possible data. */ /** 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 */ /** This is the message type. It is either REQUESTS or EVENTS, everything else would be invalid here */
public MessageType messageType; public MessageType messageType;
@ -26,7 +26,7 @@ public class MessageStructure {
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; 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); return messageType == that.messageType && Arrays.equals(messages, that.messages) && Objects.equals(customContentType, that.customContentType) && Objects.equals(customContent, that.customContent);
} }

View File

@ -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<String, Object> 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 +
'}';
}
}

View File

@ -8,12 +8,11 @@ import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*; 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.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.events.EventType; 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; import java.util.HashMap;
@ -25,7 +24,7 @@ class JSONTest {
String[] messageStructureEnd; String[] messageStructureEnd;
/** Still need to add: messages, messageType */ /** Still need to add: messages, messageType */
MessageStructure target; EventMessage target;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
@ -56,7 +55,7 @@ class JSONTest {
} }
""", """,
}; };
target = new MessageStructure(); target = new EventMessage();
target.customContentType = "TestCustomContent"; target.customContentType = "TestCustomContent";
target.customContent = new HashMap<String, Object>(); target.customContent = new HashMap<String, Object>();
target.customContent.put("customKey", "customResult"); target.customContent.put("customKey", "customResult");