feat: implemented (this time actually) proper message deserialization

This commit is contained in:
Yannik Bretschneider 2021-06-03 19:13:40 +02:00
parent 429cdd2e42
commit 4c33e824f9
15 changed files with 178 additions and 18 deletions

View File

@ -0,0 +1,7 @@
package uulm.teamname.marvelous.gamelibrary.messages;
public enum Assignment {
PlayerOne,
PlayerTwo,
Spectator
}

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonTypeResolver;
import uulm.teamname.marvelous.gamelibrary.json.login.MessageDeserializer;
import java.util.Objects;
@ -13,25 +14,25 @@ import java.util.Objects;
* messageType, as fields that aren't sent are null. Note that most messages are not deserialized into the {@link
* BasicMessage}, but instead into messages such as the {@link EventMessage} or {@link GameAssignmentMessage}.
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION, defaultImpl = BasicMessage.class)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "messageType")
@JsonSubTypes({
@Type(HelloClientMessage.class),
@Type(HelloServerMessage.class),
@Type(ErrorMessage.class),
@Type(PlayerReadyMessage.class),
@Type(ReconnectMessage.class),
@Type(GameAssignmentMessage.class),
@Type(GeneralAssignmentMessage.class),
@Type(CharacterSelectionMessage.class),
@Type(ConfirmSelectionMessage.class),
@Type(GameStructureMessage.class),
@Type(EventMessage.class),
// @Type(RequestMessage.class), There's a custom deserializer in EventMessage for this
@Type(GoodbyeClientMessage.class)
@Type(value = HelloClientMessage.class, name = "HELLO_CLIENT"),
@Type(value = HelloServerMessage.class, name = "HELLO_SERVER"),
@Type(value = ErrorMessage.class, name = "ERROR"),
@Type(value = PlayerReadyMessage.class, name = "PLAYER_READY"),
@Type(value = ReconnectMessage.class, name = "RECONNECT"),
@Type(value = GameAssignmentMessage.class, name = "GAME_ASSIGNMENT"),
@Type(value = GeneralAssignmentMessage.class, name = "GENERAL_ASSIGNMENT"),
@Type(value = CharacterSelectionMessage.class, name = "CHARACTER_SELECTION"),
@Type(value = ConfirmSelectionMessage.class, name = "CONFIRM_SELECTION"),
@Type(value = GameStructureMessage.class, name = "GAME_STRUCTURE"),
@Type(value = EventMessage.class, name = "EVENTS"),
// @Type(value = RequestMessage.class, name = "REQUESTS"), // There's a custom deserializer in EventMessage for this
@Type(value = GoodbyeClientMessage.class, name = "GOODBYE_CLIENT")
})
public class BasicMessage {
/** The messageType describes the type of message that might be sent */
/** The messageType describes the type of message that might be sent. Override this with the local MessageType. */
public MessageType messageType;
/** I have no idea what this might be used for, even less the boolean in the answer */

View File

@ -1,10 +1,39 @@
package uulm.teamname.marvelous.gamelibrary.messages;
import java.util.Arrays;
public class CharacterSelectionMessage extends BasicMessage {
public final MessageType messageType = MessageType.CHARACTER_SELECTION;
/**
* Boolean array that conveys information about what characters (6)
* of the given characters (12) have been selected
*/
public Boolean[] characters;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
CharacterSelectionMessage that = (CharacterSelectionMessage) o;
return Arrays.equals(characters, that.characters);
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + Arrays.hashCode(characters);
return result;
}
@Override
public String toString() {
return "CharacterSelectionMessage{" +
"messageType=" + messageType +
", optionals='" + optionals + '\'' +
", characters=" + Arrays.toString(characters) +
'}';
}
}

View File

@ -1,7 +1,34 @@
package uulm.teamname.marvelous.gamelibrary.messages;
import java.util.Objects;
public class ConfirmSelectionMessage extends BasicMessage {
public final MessageType messageType = MessageType.CONFIRM_SELECTION;
/** Whether the other player is also done with the selection already */
public Boolean selectionComplete;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
ConfirmSelectionMessage that = (ConfirmSelectionMessage) o;
return Objects.equals(selectionComplete, that.selectionComplete);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), selectionComplete);
}
@Override
public String toString() {
return "ConfirmSelectionMessage{" +
"messageType=" + messageType +
", optionals='" + optionals + '\'' +
", selectionComplete=" + selectionComplete +
'}';
}
}

View File

@ -2,6 +2,9 @@ package uulm.teamname.marvelous.gamelibrary.messages;
public class ErrorMessage extends BasicMessage{
public final MessageType messageType = MessageType.ERROR;
/** Some message telling the client what went wrong */
public String message;

View File

@ -10,9 +10,11 @@ import java.util.HashMap;
import java.util.Objects;
/** Represents a message sent between client and server and contains all possible data. */
@JsonDeserialize(using = EventMessageDeserializer.class)
// @JsonDeserialize(using = EventMessageDeserializer.class)
public class EventMessage extends BasicMessage {
public final MessageType messageType = MessageType.EVENTS;
/** The list of {@link Event Events} sent inside the message. */
public Event[] messages;

View File

@ -1,13 +1,18 @@
package uulm.teamname.marvelous.gamelibrary.messages;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
import uulm.teamname.marvelous.gamelibrary.config.CharacterProperties;
import uulm.teamname.marvelous.gamelibrary.json.login.GameAssignmentDeserializer;
import java.util.Arrays;
import java.util.Objects;
// @JsonDeserialize(using = GameAssignmentDeserializer.class)
public class GameAssignmentMessage extends BasicMessage {
public final MessageType messageType = MessageType.GAME_ASSIGNMENT;
/** The ID of the game that the client is connected to. What this is used for is kind of unknown. */
public String gameID;
@ -29,4 +34,14 @@ public class GameAssignmentMessage extends BasicMessage {
result = 31 * result + Arrays.hashCode(characterSelection);
return result;
}
@Override
public String toString() {
return "GameAssignmentMessage{" +
"messageType=" + messageType +
", optionals='" + optionals + '\'' +
", gameID='" + gameID + '\'' +
", characterSelection=" + Arrays.toString(characterSelection) +
'}';
}
}

View File

@ -8,11 +8,15 @@ import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;
public class GameStructureMessage extends BasicMessage {
// TODO: ADD ENUM FOR THIS
public String assignment;
public final MessageType messageType = MessageType.GAME_STRUCTURE;
/** The role the client has been assigned to */
public Assignment assignment;
/** The name of the first player */
public String playerOneName;
@ -40,4 +44,36 @@ public class GameStructureMessage extends BasicMessage {
.toArray(CharacterProperties[]::new);
return characterConfig;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
GameStructureMessage that = (GameStructureMessage) o;
return Objects.equals(assignment, that.assignment) && Objects.equals(playerOneName, that.playerOneName) && Objects.equals(playerTwoName, that.playerTwoName) && Arrays.equals(playerOneCharacters, that.playerOneCharacters) && Arrays.equals(playerTwoCharacters, that.playerTwoCharacters) && Objects.equals(matchconfig, that.matchconfig) && Objects.equals(scenarioconfig, that.scenarioconfig);
}
@Override
public int hashCode() {
int result = Objects.hash(super.hashCode(), assignment, playerOneName, playerTwoName, matchconfig, scenarioconfig);
result = 31 * result + Arrays.hashCode(playerOneCharacters);
result = 31 * result + Arrays.hashCode(playerTwoCharacters);
return result;
}
@Override
public String toString() {
return "GameStructureMessage{" +
"messageType=" + messageType +
", optionals='" + optionals + '\'' +
", assignment='" + assignment + '\'' +
", playerOneName='" + playerOneName + '\'' +
", playerTwoName='" + playerTwoName + '\'' +
", playerOneCharacters=" + Arrays.toString(playerOneCharacters) +
", playerTwoCharacters=" + Arrays.toString(playerTwoCharacters) +
", matchconfig=" + matchconfig +
", scenarioconfig=" + scenarioconfig +
'}';
}
}

View File

@ -1,7 +1,34 @@
package uulm.teamname.marvelous.gamelibrary.messages;
import java.util.Objects;
public class GeneralAssignmentMessage extends BasicMessage {
public final MessageType messageType = MessageType.GENERAL_ASSIGNMENT;
/** The ID of the game that the client is connected to. What this is used for is kind of unknown. */
public String gameID;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
GeneralAssignmentMessage that = (GeneralAssignmentMessage) o;
return Objects.equals(gameID, that.gameID);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), gameID);
}
@Override
public String toString() {
return "GeneralAssignmentMessage{" +
"messageType=" + messageType +
", optionals='" + optionals + '\'' +
", gameID='" + gameID + '\'' +
'}';
}
}

View File

@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.messages;
public class GoodbyeClientMessage extends BasicMessage {
public final MessageType messageType = MessageType.GOODBYE_CLIENT;
/** A message sent to the client on disconnect */
public String message;
}

View File

@ -1,6 +1,9 @@
package uulm.teamname.marvelous.gamelibrary.messages;
public class HelloClientMessage extends BasicMessage {
public final MessageType messageType = MessageType.HELLO_CLIENT;
/** Whether there is a running game that the player disconnected from */
public Boolean runningGame;
}

View File

@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.messages;
public class HelloServerMessage extends BasicMessage{
public final MessageType messageType = MessageType.HELLO_SERVER;
/** User-chosen name, basically a PlayerName */
public String name;

View File

@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.messages;
public class PlayerReadyMessage extends BasicMessage {
public final MessageType messageType = MessageType.PLAYER_READY;
/** Whether the client wants to start the game. If this is false, the client gets disconnected. */
public Boolean startGame;

View File

@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.messages;
public class ReconnectMessage extends BasicMessage {
public final MessageType messageType = MessageType.RECONNECT;
/** Whether the client wants to reconnect to the previously running game */
public Boolean reconnect;
}

View File

@ -11,6 +11,8 @@ import java.util.Objects;
/** Represents a message sent between client and server and contains all possible data. */
public class RequestMessage extends BasicMessage {
public final MessageType messageType = MessageType.REQUESTS;
/** The list of {@link Event Events} sent inside the message. */
public Request[] messages;