mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
38 lines
1.0 KiB
Java
38 lines
1.0 KiB
Java
package seng302.gameServer;
|
|
|
|
import java.util.Arrays;
|
|
import seng302.models.stream.packets.StreamPacket;
|
|
import seng302.server.messages.BoatActionType;
|
|
|
|
|
|
public class ServerPacketParser {
|
|
|
|
|
|
public static BoatActionType extractBoatAction(StreamPacket packet) {
|
|
byte[] payload = packet.getPayload();
|
|
int messageVersionNo = payload[0];
|
|
long actionTypeValue = bytesToLong(Arrays.copyOfRange(payload, 0, 1));
|
|
return BoatActionType.getType((int) actionTypeValue);
|
|
}
|
|
|
|
/**
|
|
* takes an array of up to 7 bytes and returns a positive
|
|
* long constructed from the input bytes
|
|
*
|
|
* @return a positive long if there is less than 7 bytes -1 otherwise
|
|
*/
|
|
private static long bytesToLong(byte[] bytes) {
|
|
long partialLong = 0;
|
|
int index = 0;
|
|
for (byte b : bytes) {
|
|
if (index > 6) {
|
|
return -1;
|
|
}
|
|
partialLong = partialLong | (b & 0xFFL) << (index * 8);
|
|
index++;
|
|
}
|
|
return partialLong;
|
|
}
|
|
}
|
|
|