From f2e737373a77e795d5590847dd1e35cd8401fcb4 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Wed, 9 Jun 2021 16:03:08 +0200 Subject: [PATCH] feat: implemented CustomRequest --- .../deserialize/RequestDeserializer.java | 5 ++- .../gamelibrary/requests/CustomRequest.java | 45 +++++++++++++++++++ .../gamelibrary/requests/RequestBuilder.java | 16 +++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/main/java/uulm/teamname/marvelous/gamelibrary/requests/CustomRequest.java diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/RequestDeserializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/RequestDeserializer.java index b3a48cd..9b3b457 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/RequestDeserializer.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/RequestDeserializer.java @@ -11,6 +11,7 @@ import uulm.teamname.marvelous.gamelibrary.entities.StoneType; import uulm.teamname.marvelous.gamelibrary.requests.*; import java.io.IOException; +import java.util.HashMap; import java.util.Optional; import static uulm.teamname.marvelous.gamelibrary.json.JsonNodeUnwrapper.unwrap; @@ -38,7 +39,9 @@ public class RequestDeserializer extends JsonDeserializer { .withTargetField(unwrap(node.get("targetField"), IntVector2.class, codec)) .withStoneType(Optional.ofNullable(unwrap(node.get("stoneType"), EntityID.class, codec)) .map(x -> StoneType.valueOf(x.id)) - .orElse(null)); + .orElse(null)) + .withTeamIdentifier(unwrap(node.get("teamIdentifier"), String.class, codec)) + .withCustomContent(unwrap(node.get("teamIdentifier"), HashMap.class, codec)); switch (requestType) { case DisconnectRequest, diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/requests/CustomRequest.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/requests/CustomRequest.java new file mode 100644 index 0000000..ca79a73 --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/requests/CustomRequest.java @@ -0,0 +1,45 @@ +package uulm.teamname.marvelous.gamelibrary.requests; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import uulm.teamname.marvelous.gamelibrary.events.Event; +import uulm.teamname.marvelous.gamelibrary.events.EventType; +import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.EventDeserializer; +import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.RequestDeserializer; +import uulm.teamname.marvelous.gamelibrary.json.ingame.serialize.EventSerializer; +import uulm.teamname.marvelous.gamelibrary.json.ingame.serialize.RequestSerializer; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** Represents a custom request */ +@JsonDeserialize(using = RequestDeserializer.class) +@JsonSerialize(using = RequestSerializer.class) +public class CustomRequest extends Request { + public String teamIdentifier; + public Map customContent; + + @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; + CustomRequest that = (CustomRequest) o; + return Objects.equals(teamIdentifier, that.teamIdentifier) && Objects.equals(customContent, that.customContent); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), teamIdentifier, customContent); + } + + @Override + public String toString() { + return "CustomEvent{" + + "eventType=" + this.type + + ", teamIdentifier='" + teamIdentifier + '\'' + + ", customContent=" + customContent + + '}'; + } +} diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/requests/RequestBuilder.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/requests/RequestBuilder.java index 8c654f0..668888d 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/requests/RequestBuilder.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/requests/RequestBuilder.java @@ -4,8 +4,11 @@ import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.entities.Entity; import uulm.teamname.marvelous.gamelibrary.entities.EntityID; import uulm.teamname.marvelous.gamelibrary.entities.StoneType; +import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; import java.util.StringJoiner; /** @@ -25,6 +28,9 @@ public class RequestBuilder { private IntVector2 originField; private StoneType stoneType; + private String teamIdentifier; + private Map customContent; + /** * Creates a new {@link RequestBuilder} used for building {@link Request Requests}. * @param requestType is the type of Event that the final event will have @@ -72,6 +78,16 @@ public class RequestBuilder { return this; } + public RequestBuilder withTeamIdentifier(String teamIdentifier) { + this.teamIdentifier = teamIdentifier; + return this; + } + + public RequestBuilder withCustomContent(Map customContent) { + this.customContent = customContent; + return this; + } + /** * Builds a {@link GameRequest} from the values given to the builder. *