Browse Source

sort games right

master
GAM 4 years ago
parent
commit
b3307a27d4
3 changed files with 21 additions and 3 deletions
  1. +2
    -1
      db/db_init.sql
  2. +11
    -0
      src/main/java/app/data/entity/GameInfo.java
  3. +8
    -2
      src/main/java/app/data/service/ChessComService.java

+ 2
- 1
db/db_init.sql View File

@ -43,7 +43,8 @@ CREATE TABLE "game_info" (
"id" SERIAL PRIMARY KEY,
"time_control" varchar,
"chess_com_id" varchar,
"fen" varchar
"fen" varchar,
"end_time" bigint
);


+ 11
- 0
src/main/java/app/data/entity/GameInfo.java View File

@ -11,6 +11,7 @@ public class GameInfo {
private Game game;
private String timeControl;
private String fen;
private Long endTime;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ -74,4 +75,14 @@ public class GameInfo {
public void setGame(Game game) {
this.game = game;
}
@Basic
@Column(name = "end_time") // TODO: make not nullable (you need the end times of the first matchday for that...)
public Long getEndTime() {
return endTime;
}
public void setEndTime(Long endTime) {
this.endTime = endTime;
}
}

+ 8
- 2
src/main/java/app/data/service/ChessComService.java View File

@ -14,6 +14,7 @@ import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;
@Service
@ -29,7 +30,7 @@ public class ChessComService {
}
@NonNull
public List<Game> getLatestGamesBetweenPlayers(@NonNull Match match, int minAmountOfGames, int maxAmountOfMonths) {
public List<Game> getLatestGamesBetweenPlayers(@NonNull Match match, int amountOfGames, int maxAmountOfMonths) {
List<Game> games = new ArrayList<>();
for (String archiveUrl : getLatestArchiveUrls(match.getPlayer1(), maxAmountOfMonths)) {
List<ChessComGame> chessComGames = getChessComGames(archiveUrl).stream()
@ -41,10 +42,14 @@ public class ChessComService {
games.addAll(chessComGames.stream()
.map(chessComGame -> getGame(chessComGame, match))
.collect(Collectors.toList()));
if (games.size() >= minAmountOfGames) {
if (games.size() >= amountOfGames) {
break;
}
}
games.sort(Comparator.comparingLong(game -> game.getGameInfo().getEndTime()));
if (games.size() > amountOfGames) {
games = games.subList(games.size() - amountOfGames, games.size());
}
return games;
} // TODO: find exactly two games of each time control
@ -97,6 +102,7 @@ public class ChessComService {
gameInfo.setChessComId(ChessComUtils.getGameId(chessComGame));
gameInfo.setTimeControl(chessComGame.getTimeControl());
gameInfo.setFen(chessComGame.getFen());
gameInfo.setEndTime(chessComGame.getEndTime());
gameInfo.setGame(game);
return game;


Loading…
Cancel
Save