feat: implemented CustomRequest

This commit is contained in:
Yannik Bretschneider 2021-06-09 16:03:08 +02:00
parent b8402f636f
commit f2e737373a
3 changed files with 65 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
import uulm.teamname.marvelous.gamelibrary.requests.*; import uulm.teamname.marvelous.gamelibrary.requests.*;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Optional; import java.util.Optional;
import static uulm.teamname.marvelous.gamelibrary.json.JsonNodeUnwrapper.unwrap; import static uulm.teamname.marvelous.gamelibrary.json.JsonNodeUnwrapper.unwrap;
@ -38,7 +39,9 @@ public class RequestDeserializer extends JsonDeserializer<Request> {
.withTargetField(unwrap(node.get("targetField"), IntVector2.class, codec)) .withTargetField(unwrap(node.get("targetField"), IntVector2.class, codec))
.withStoneType(Optional.ofNullable(unwrap(node.get("stoneType"), EntityID.class, codec)) .withStoneType(Optional.ofNullable(unwrap(node.get("stoneType"), EntityID.class, codec))
.map(x -> StoneType.valueOf(x.id)) .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) { switch (requestType) {
case DisconnectRequest, case DisconnectRequest,

View File

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

View File

@ -4,8 +4,11 @@ import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.Entity; 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.entities.StoneType; import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner; import java.util.StringJoiner;
/** /**
@ -25,6 +28,9 @@ public class RequestBuilder {
private IntVector2 originField; private IntVector2 originField;
private StoneType stoneType; private StoneType stoneType;
private String teamIdentifier;
private Map<String, Object> customContent;
/** /**
* Creates a new {@link RequestBuilder} used for building {@link Request Requests}. * Creates a new {@link RequestBuilder} used for building {@link Request Requests}.
* @param requestType is the type of Event that the final event will have * @param requestType is the type of Event that the final event will have
@ -72,6 +78,16 @@ public class RequestBuilder {
return this; return this;
} }
public RequestBuilder withTeamIdentifier(String teamIdentifier) {
this.teamIdentifier = teamIdentifier;
return this;
}
public RequestBuilder withCustomContent(Map<String, Object> customContent) {
this.customContent = customContent;
return this;
}
/** /**
* Builds a {@link GameRequest} from the values given to the builder. * Builds a {@link GameRequest} from the values given to the builder.
* <ul> * <ul>