You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.2 KiB
35 lines
1.2 KiB
package network.MessageDecoders;
|
|
|
|
import network.Exceptions.InvalidMessageException;
|
|
import network.Messages.AC35Data;
|
|
import network.Messages.HostGame;
|
|
import network.Messages.HostGamesRequest;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
import static network.Utils.ByteConverter.bytesToInt;
|
|
|
|
public class HostedGamesRequestDecoder implements MessageDecoder{
|
|
@Override
|
|
public AC35Data decode(byte[] encodedMessage) throws InvalidMessageException {
|
|
try{
|
|
int numberOfGames = bytesToInt(Arrays.copyOfRange(encodedMessage, 0, 4));
|
|
|
|
HostGameMessageDecoder lineDecoder = new HostGameMessageDecoder();
|
|
List<HostGame> knownGames = new ArrayList<>();
|
|
int byteIndex = 4;
|
|
for (int i = 0; i < numberOfGames; i++){
|
|
knownGames.add((HostGame) lineDecoder.decode(Arrays.copyOfRange(encodedMessage, byteIndex, byteIndex+14)));
|
|
byteIndex += 14;
|
|
}
|
|
|
|
return new HostGamesRequest(knownGames);
|
|
|
|
} catch (Exception e) {
|
|
throw new InvalidMessageException("Could not decode Host game message.", e);
|
|
}
|
|
}
|
|
}
|