Improved sorting of tables and matches.

This commit is contained in:
LevelX2 2014-04-17 16:39:19 +02:00
parent 6e28320493
commit 4e53ff73ea
3 changed files with 50 additions and 6 deletions

View file

@ -36,8 +36,8 @@ import java.util.UUID;
*/
public abstract class RoomImpl implements Room {
private UUID chatId;
private UUID roomId;
private final UUID chatId;
private final UUID roomId;
public RoomImpl() {
roomId = UUID.randomUUID();

View file

@ -87,8 +87,7 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
ArrayList<TableView> tableList = new ArrayList<>();
ArrayList<MatchView> matchList = new ArrayList<>();
List<Table> allTables = new ArrayList<>(tables.values());
Collections.sort(allTables, new TimestampSorter());
Collections.reverse(allTables);
Collections.sort(allTables, new TableListSorter());
for (Table table: allTables) {
if (table.getState() != TableState.FINISHED) {
tableList.add(new TableView(table));
@ -197,10 +196,39 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
}
class TimestampSorter implements Comparator<Table> {
/**
* Sorts the tables for table and match view of the client room
*
* @author LevelX2
*/
class TableListSorter implements Comparator<Table> {
@Override
public int compare(Table one, Table two) {
return one.getCreateTime().compareTo(two.getCreateTime());
// priority 1 - Not started yet
if (one.getState().equals(TableState.STARTING) || one.getState().equals(TableState.WAITING)) {
if (two.getState().equals(TableState.STARTING) || two.getState().equals(TableState.WAITING)) {
return two.getCreateTime().compareTo(one.getCreateTime());
} else {
return -1; // one has higher priority
}
}
// priority 2 - Not finished yet -> Sorted by time started
if (two.getState().equals(TableState.STARTING) || two.getState().equals(TableState.WAITING)) {
return -1;
} else if (one.getEndTime() == null) {
if (two.getEndTime() == null) {
return two.getStartTime().compareTo(one.getStartTime());
} else {
return -1;
}
}
// priority 3 - Finished tables -> Sorted by time finished
if (two.getEndTime() == null) {
return 1;
} else {
return two.getEndTime().compareTo(one.getEndTime());
}
}
}

View file

@ -255,4 +255,20 @@ public class Table implements Serializable {
this.tournamentSubTable = tournamentSubTable;
}
public Date getStartTime() {
if (isTournament) {
return tournament.getStartTime();
} else {
return match.getStartTime();
}
}
public Date getEndTime() {
if (isTournament) {
return tournament.getEndTime();
} else {
return match.getEndTime();
}
}
}