refactor: code cleanup

This commit is contained in:
Yannik Bretschneider 2021-06-03 19:22:32 +02:00
parent 4c33e824f9
commit 29f5eed2fb
6 changed files with 1 additions and 84 deletions

View File

@ -1,32 +0,0 @@
package uulm.teamname.marvelous.gamelibrary.json.login;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.messages.EventMessage;
import uulm.teamname.marvelous.gamelibrary.messages.MessageType;
import uulm.teamname.marvelous.gamelibrary.messages.RequestMessage;
import java.io.IOException;
public class EventMessageDeserializer extends JsonDeserializer<BasicMessage> {
@Override
public BasicMessage deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode node = p.readValueAsTree();
JsonNode subNode;
if ((subNode = node.get("messageType")) != null) {
if (MessageType.valueOf(subNode.textValue()) == MessageType.EVENTS) {
return p.getCodec().treeToValue(node, EventMessage.class);
} else if (MessageType.valueOf(subNode.textValue()) == MessageType.REQUESTS) {
return p.getCodec().treeToValue(node, RequestMessage.class);
}
}
throw new IllegalStateException("EventMessage is actually not an EventMessage");
}
}

View File

@ -1,48 +0,0 @@
package uulm.teamname.marvelous.gamelibrary.json.login;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import uulm.teamname.marvelous.gamelibrary.messages.*;
import java.io.IOException;
import java.util.Optional;
public class MessageDeserializer extends JsonDeserializer<BasicMessage> {
@Override
public BasicMessage deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec codec = p.getCodec();
JsonNode node = codec.readTree(p);
var type = Optional.of(node.get("messageType"))
.map(JsonNode::textValue)
.map(MessageType::valueOf);
if (type.isEmpty()) {
throw new IOException("MessageType does not exist");
}
BasicMessage message;
switch (type.get()) {
case HELLO_CLIENT -> message = p.readValueAs(HelloClientMessage.class);
case HELLO_SERVER -> message = p.readValueAs(HelloServerMessage.class);
case ERROR -> message = p.readValueAs(ErrorMessage.class);
case PLAYER_READY -> message = p.readValueAs(PlayerReadyMessage.class);
case RECONNECT -> message = p.readValueAs(ReconnectMessage.class);
case GAME_ASSIGNMENT -> message = p.readValueAs(GameAssignmentMessage.class);
case GENERAL_ASSIGNMENT -> message = p.readValueAs(GeneralAssignmentMessage.class);
case CHARACTER_SELECTION -> message = p.readValueAs(CharacterSelectionMessage.class);
case CONFIRM_SELECTION -> message = p.readValueAs(ConfirmSelectionMessage.class);
case GAME_STRUCTURE -> message = p.readValueAs(GameStructureMessage.class);
case EVENTS -> message = p.readValueAs(EventMessage.class);
case REQUESTS -> message = p.readValueAs(RequestMessage.class);
case GOODBYE_CLIENT -> message = p.readValueAs(GoodbyeClientMessage.class);
default -> throw new IllegalStateException("No MessageType given, which should be impossible");
}
return message;
}
}

View File

@ -5,7 +5,6 @@ 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;

View File

@ -3,7 +3,6 @@ package uulm.teamname.marvelous.gamelibrary.messages;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.json.login.EventMessageDeserializer;
import java.util.Arrays;
import java.util.HashMap;

View File

@ -3,7 +3,6 @@ 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;

View File

@ -164,7 +164,7 @@ class GameLogicTest {
server.startGame(player1Selection, player2Selection);
System.out.println(server.toString());
System.out.println(server);
}