diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/Assignment.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/Assignment.java new file mode 100644 index 0000000..cbcf67b --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/Assignment.java @@ -0,0 +1,7 @@ +package uulm.teamname.marvelous.gamelibrary.messages; + +public enum Assignment { + PlayerOne, + PlayerTwo, + Spectator +} diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/BasicMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/BasicMessage.java index 5485802..d2dc13e 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/BasicMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/BasicMessage.java @@ -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 */ diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/CharacterSelectionMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/CharacterSelectionMessage.java index 42e0b93..63c8fb3 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/CharacterSelectionMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/CharacterSelectionMessage.java @@ -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) + + '}'; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ConfirmSelectionMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ConfirmSelectionMessage.java index bce9962..c596fa6 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ConfirmSelectionMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ConfirmSelectionMessage.java @@ -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 + + '}'; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ErrorMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ErrorMessage.java index b9ad7fd..b2e9339 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ErrorMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ErrorMessage.java @@ -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; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/EventMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/EventMessage.java index 44bbe9c..685d921 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/EventMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/EventMessage.java @@ -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; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GameAssignmentMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GameAssignmentMessage.java index 92e6cbc..41a5168 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GameAssignmentMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GameAssignmentMessage.java @@ -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) + + '}'; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GameStructureMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GameStructureMessage.java index 4cf8396..aeacf40 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GameStructureMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GameStructureMessage.java @@ -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 + + '}'; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GeneralAssignmentMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GeneralAssignmentMessage.java index 358b2e2..1a54159 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GeneralAssignmentMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GeneralAssignmentMessage.java @@ -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 + '\'' + + '}'; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GoodbyeClientMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GoodbyeClientMessage.java index 0428157..d7b1773 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GoodbyeClientMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/GoodbyeClientMessage.java @@ -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; } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/HelloClientMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/HelloClientMessage.java index 482fad7..2238cf2 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/HelloClientMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/HelloClientMessage.java @@ -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; } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/HelloServerMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/HelloServerMessage.java index 2586aaa..c23b085 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/HelloServerMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/HelloServerMessage.java @@ -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; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/PlayerReadyMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/PlayerReadyMessage.java index 9a99e43..3b456f8 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/PlayerReadyMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/PlayerReadyMessage.java @@ -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; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ReconnectMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ReconnectMessage.java index cd9f354..a979577 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ReconnectMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/ReconnectMessage.java @@ -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; } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/RequestMessage.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/RequestMessage.java index 73777ca..77146a7 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/RequestMessage.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/messages/RequestMessage.java @@ -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;