mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
faf4600f51
tags : #story[1142]
80 lines
2.7 KiB
Java
80 lines
2.7 KiB
Java
package seng302.gameServer.messages;
|
|
|
|
import java.util.List;
|
|
import java.util.zip.CRC32;
|
|
|
|
public class RaceStatusMessage extends Message{
|
|
private final MessageType MESSAGE_TYPE = MessageType.RACE_STATUS;
|
|
private final int MESSAGE_VERSION = 2; //Always set to 1
|
|
private final int MESSAGE_BASE_SIZE = 24;
|
|
private final double windDirFactor = 0x4000 / 90;
|
|
|
|
|
|
private long currentTime;
|
|
private long raceId;
|
|
private RaceStatus raceStatus;
|
|
private long expectedStartTime;
|
|
private double raceWindDirection;
|
|
private long windSpeed;
|
|
private long numBoatsInRace;
|
|
private RaceType raceType;
|
|
private List<BoatSubMessage> boats;
|
|
private CRC32 crc;
|
|
|
|
/**
|
|
* A message containing the current status of the race
|
|
* @param raceId The ID of the current race
|
|
* @param raceStatus The status of the race
|
|
* @param expectedStartTime The expected start time
|
|
* @param raceWindDirection The wind direction (north, east, south)
|
|
* @param windSpeed The wind speed in mm/sec
|
|
* @param numBoatsInRace The number of boats in the race
|
|
* @param raceType The race type (Match/fleet)
|
|
* @param sourceId The source of this message
|
|
* @param boats A list of boat status sub messages
|
|
*/
|
|
public RaceStatusMessage(long raceId, RaceStatus raceStatus, long expectedStartTime, double raceWindDirection,
|
|
long windSpeed, long numBoatsInRace, RaceType raceType, long sourceId, List<BoatSubMessage> boats){
|
|
currentTime = System.currentTimeMillis();
|
|
this.raceId = raceId;
|
|
this.raceStatus = raceStatus;
|
|
this.expectedStartTime = expectedStartTime;
|
|
this.raceWindDirection = raceWindDirection * windDirFactor;
|
|
this.windSpeed = windSpeed;
|
|
this.numBoatsInRace = numBoatsInRace;
|
|
this.raceType = raceType;
|
|
this.boats = boats;
|
|
crc = new CRC32();
|
|
|
|
setHeader(new Header(MESSAGE_TYPE, (int) sourceId, (short) getSize()));
|
|
allocateBuffer();
|
|
writeHeaderToBuffer();
|
|
|
|
putByte((byte) MESSAGE_VERSION);
|
|
putInt(currentTime, 6);
|
|
putInt((int) raceId, 4);
|
|
putByte((byte) raceStatus.getCode());
|
|
putInt(expectedStartTime, 6);
|
|
putInt((int) this.raceWindDirection, 2);
|
|
putInt((int) windSpeed, 2);
|
|
putByte((byte) numBoatsInRace);
|
|
putByte((byte) raceType.getCode());
|
|
|
|
for (BoatSubMessage boatSubMessage : boats){
|
|
putBytes(boatSubMessage.getByteBuffer(), boatSubMessage.getSize());
|
|
}
|
|
|
|
writeCRC();
|
|
rewind();
|
|
}
|
|
|
|
/**
|
|
* @return the size of this message in bytes
|
|
*/
|
|
@Override
|
|
public int getSize() {
|
|
return MESSAGE_BASE_SIZE + (20 * ((int) numBoatsInRace));
|
|
}
|
|
|
|
}
|