From 1385500e6822d387944b18f7347eea9df8a13641 Mon Sep 17 00:00:00 2001 From: fjc40 Date: Sat, 5 Aug 2017 19:10:15 +1200 Subject: [PATCH] Added JoinAcceptance and RequestToJoin enumerations. Issue #35 #story[1095] --- .../Messages/Enums/JoinAcceptanceEnum.java | 102 ++++++++++++++++++ .../Messages/Enums/RequestToJoinEnum.java | 97 +++++++++++++++++ .../java/network/Messages/JoinAcceptance.java | 4 + .../java/network/Messages/RequestToJoin.java | 13 +++ 4 files changed, 216 insertions(+) create mode 100644 racevisionGame/src/main/java/network/Messages/Enums/JoinAcceptanceEnum.java create mode 100644 racevisionGame/src/main/java/network/Messages/Enums/RequestToJoinEnum.java create mode 100644 racevisionGame/src/main/java/network/Messages/JoinAcceptance.java create mode 100644 racevisionGame/src/main/java/network/Messages/RequestToJoin.java diff --git a/racevisionGame/src/main/java/network/Messages/Enums/JoinAcceptanceEnum.java b/racevisionGame/src/main/java/network/Messages/Enums/JoinAcceptanceEnum.java new file mode 100644 index 00000000..6939d8f3 --- /dev/null +++ b/racevisionGame/src/main/java/network/Messages/Enums/JoinAcceptanceEnum.java @@ -0,0 +1,102 @@ +package network.Messages.Enums; + + +import java.util.HashMap; +import java.util.Map; + +/** + * This enum encapsulates the different ways in which a server may respond to a client {@link network.Messages.RequestToJoin} message. + */ +public enum JoinAcceptanceEnum { + + + /** + * Client is allowed to join. + */ + JOIN_SUCCESSFUL(1), + + /** + * The race is full - no more participants allowed. + */ + RACE_PARTICIPANTS_FULL(2), + + /** + * The race cannot allow any more ghost participants to join. + */ + GHOST_PARTICIPANTS_FULL(3), + + /** + * The server is completely full, cannot participate or spectate. + */ + SERVER_FULL(4), + + + /** + * Used to indicate that a given byte value is invalid. + */ + NOT_AN_ACCEPTANCE_TYPE(-1); + + + /** + * Primitive value of the enum. + */ + private byte value; + + + /** + * Ctor. Creates a JoinAcceptanceEnum from a given primitive integer value, cast to a byte. + * @param value Integer, which is cast to byte, to construct from. + */ + private JoinAcceptanceEnum(int value) { + this.value = (byte) value; + } + + /** + * Returns the primitive value of the enum. + * @return Primitive value of the enum. + */ + public byte getValue() { + return value; + } + + + + + /** + * Stores a mapping between Byte values and JoinAcceptanceEnum values. + */ + private static final Map byteToAcceptanceMap = new HashMap<>(); + + + /* + Static initialization block. Initializes the byteToAcceptanceMap. + */ + static { + for (JoinAcceptanceEnum type : JoinAcceptanceEnum.values()) { + JoinAcceptanceEnum.byteToAcceptanceMap.put(type.value, type); + } + } + + + /** + * Returns the enumeration value which corresponds to a given byte value. + * @param joinAcceptanceEnum Byte value to convert to a JoinAcceptanceEnum value. + * @return The RequestToJoinEnum value which corresponds to the given byte value. + */ + public static JoinAcceptanceEnum fromByte(byte joinAcceptanceEnum) { + //Gets the corresponding MessageType from the map. + JoinAcceptanceEnum type = JoinAcceptanceEnum.byteToAcceptanceMap.get(joinAcceptanceEnum); + + if (type == null) { + //If the byte value wasn't found, return the NOT_AN_ACCEPTANCE_TYPE JoinAcceptanceEnum. + return JoinAcceptanceEnum.NOT_AN_ACCEPTANCE_TYPE; + } else { + //Otherwise, return the JoinAcceptanceEnum. + return type; + } + + } + + + +} diff --git a/racevisionGame/src/main/java/network/Messages/Enums/RequestToJoinEnum.java b/racevisionGame/src/main/java/network/Messages/Enums/RequestToJoinEnum.java new file mode 100644 index 00000000..2aab2925 --- /dev/null +++ b/racevisionGame/src/main/java/network/Messages/Enums/RequestToJoinEnum.java @@ -0,0 +1,97 @@ +package network.Messages.Enums; + + +import java.util.HashMap; +import java.util.Map; + +/** + * This enum encapsulates the different ways in which a client may wish to connect to a server. + */ +public enum RequestToJoinEnum { + + + /** + * Client wants to spectate. + */ + SPECTATOR(0), + + /** + * Client wants to participate. + */ + PARTICIPANT(1), + + /** + * Client wants to particpate as a ghost. + */ + GHOST(5), + + + /** + * Used to indicate that a given byte value is invalid. + */ + NOT_A_REQUEST_TYPE(-1); + + + /** + * Primitive value of the enum. + */ + private byte value; + + + /** + * Ctor. Creates a RequestToJoinEnum from a given primitive integer value, cast to a byte. + * @param value Integer, which is cast to byte, to construct from. + */ + private RequestToJoinEnum(int value) { + this.value = (byte) value; + } + + /** + * Returns the primitive value of the enum. + * @return Primitive value of the enum. + */ + public byte getValue() { + return value; + } + + + + + /** + * Stores a mapping between Byte values and RequestToJoinEnum values. + */ + private static final Map byteToRequestMap = new HashMap<>(); + + + /* + Static initialization block. Initializes the byteToRequestMap. + */ + static { + for (RequestToJoinEnum type : RequestToJoinEnum.values()) { + RequestToJoinEnum.byteToRequestMap.put(type.value, type); + } + } + + + /** + * Returns the enumeration value which corresponds to a given byte value. + * @param requestToJoinEnum Byte value to convert to a RequestToJoinEnum value. + * @return The RequestToJoinEnum value which corresponds to the given byte value. + */ + public static RequestToJoinEnum fromByte(byte requestToJoinEnum) { + //Gets the corresponding MessageType from the map. + RequestToJoinEnum type = RequestToJoinEnum.byteToRequestMap.get(requestToJoinEnum); + + if (type == null) { + //If the byte value wasn't found, return the NOT_A_REQUEST_TYPE RequestToJoinEnum. + return RequestToJoinEnum.NOT_A_REQUEST_TYPE; + } else { + //Otherwise, return the RequestToJoinEnum. + return type; + } + + } + + + +} diff --git a/racevisionGame/src/main/java/network/Messages/JoinAcceptance.java b/racevisionGame/src/main/java/network/Messages/JoinAcceptance.java new file mode 100644 index 00000000..911efa3f --- /dev/null +++ b/racevisionGame/src/main/java/network/Messages/JoinAcceptance.java @@ -0,0 +1,4 @@ +package network.Messages; + +public class JoinAcceptance { +} diff --git a/racevisionGame/src/main/java/network/Messages/RequestToJoin.java b/racevisionGame/src/main/java/network/Messages/RequestToJoin.java new file mode 100644 index 00000000..cf3f0ccd --- /dev/null +++ b/racevisionGame/src/main/java/network/Messages/RequestToJoin.java @@ -0,0 +1,13 @@ +package network.Messages; + + +/** + * This is the message a client sends to a server to request to join/view a race. + */ +public class RequestToJoin { + + + + + +}