SourceIdAllocator now allocates from the BoatDataSource and RaceDataSource exposed by MockRace, instead of having its own source IDs. Can only allocate ids during PRESTART period.
RaceLogic now calls MockRace.initialiseBoats() after the prestart period has finished. MockRace create a VisualiserBoat when a source ID is allocated. MockOutput now sends updated XML messages during the race, instead of only at the start. #story[1188]
This commit is contained in:
@@ -2,6 +2,7 @@ package mock.model;
|
||||
|
||||
|
||||
import mock.exceptions.SourceIDAllocationException;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -13,24 +14,18 @@ public class SourceIdAllocator {
|
||||
|
||||
|
||||
/**
|
||||
* This list contains all unallocated source IDs.
|
||||
* The race we are allocating for.
|
||||
*/
|
||||
List<Integer> unallocatedIDs = new ArrayList<>();
|
||||
private MockRace mockRace;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This list contains all allocated source IDs.
|
||||
* Creates a SourceIdAllocator for a given race.
|
||||
* @param mockRace Race to allocate source IDs for.
|
||||
*/
|
||||
List<Integer> allocatedIDs = new ArrayList<>();
|
||||
|
||||
|
||||
/**
|
||||
* Creates a source ID allocator, using the given list of unallocated source IDs.
|
||||
* @param unallocatedIDs List of unallocated source IDs.
|
||||
*/
|
||||
public SourceIdAllocator(List<Integer> unallocatedIDs) {
|
||||
//We need to copy the list.
|
||||
this.unallocatedIDs.addAll(unallocatedIDs);
|
||||
public SourceIdAllocator(MockRace mockRace) {
|
||||
this.mockRace = mockRace;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,11 +36,23 @@ public class SourceIdAllocator {
|
||||
*/
|
||||
public synchronized int allocateSourceID() throws SourceIDAllocationException {
|
||||
|
||||
if (mockRace.getRaceStatusEnum() != RaceStatusEnum.PRESTART) {
|
||||
throw new SourceIDAllocationException("Could not allocate a source ID. Can only allocate during pre-start period. It is currently: " + mockRace.getRaceStatusEnum());
|
||||
}
|
||||
|
||||
List<Integer> allocatedIDs = mockRace.getRaceDataSource().getParticipants();
|
||||
List<Integer> allIDs = new ArrayList<>(mockRace.getBoatDataSource().getBoats().keySet());
|
||||
|
||||
//Get list of unallocated ids.
|
||||
List<Integer> unallocatedIDs = new ArrayList<>(allIDs);
|
||||
unallocatedIDs.removeAll(allocatedIDs);
|
||||
|
||||
|
||||
if (!unallocatedIDs.isEmpty()) {
|
||||
|
||||
int sourceID = unallocatedIDs.remove(0);
|
||||
|
||||
allocatedIDs.add(sourceID);
|
||||
mockRace.generateMockBoat(sourceID);
|
||||
|
||||
return sourceID;
|
||||
|
||||
@@ -61,10 +68,6 @@ public class SourceIdAllocator {
|
||||
* @param sourceID Source ID to return.
|
||||
*/
|
||||
public void returnSourceID(Integer sourceID) {
|
||||
|
||||
//We remove an Integer, not an int, so that we remove by value not by index.
|
||||
allocatedIDs.remove(sourceID);
|
||||
|
||||
unallocatedIDs.add(sourceID);
|
||||
mockRace.removeMockBoat(sourceID);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user