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 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;

View File

@ -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");
}

View File

@ -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;
/**
* <b>Exists only in a HelloClient message.</b>
* <b>Exists only in a HelloClient message.</b>
* Describes whether the user was in a running game before reconnecting.
*/
boolean runningGame;
/**
* <b>Exists only in GameAssignment and GeneralAssignment messages.</b>
* <b>Exists only in GameAssignment and GeneralAssignment messages.</b>
* Used for notifying the player of which party they have been assigned to
*/
String gameID;

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.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);
}

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 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<String, Object>();
target.customContent.put("customKey", "customResult");