From 0146843262b8d29add74375e4fbf3c9e3644514b Mon Sep 17 00:00:00 2001 From: Tellmarch <13988079+Tellmarch@users.noreply.github.com> Date: Sun, 14 Jan 2024 15:34:58 +0100 Subject: [PATCH] Search kifu - start of server code --- .../library/database/GameRepository.java | 7 +- .../library/database/KifuRepository.java | 7 +- .../database/search/KifuSearchFilter.java | 16 ++++ .../database/search/KifuSearchManager.java | 78 +++++++++++++++++++ .../database/search/KifuSearchResult.java | 22 ++++++ .../models/formats/usf/UsfMoveConverter.java | 2 +- 6 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchFilter.java create mode 100644 playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchManager.java create mode 100644 playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchResult.java diff --git a/playshogi-library-database/src/main/java/com/playshogi/library/database/GameRepository.java b/playshogi-library-database/src/main/java/com/playshogi/library/database/GameRepository.java index 3eeb92d7..2ed97ff5 100644 --- a/playshogi-library-database/src/main/java/com/playshogi/library/database/GameRepository.java +++ b/playshogi-library-database/src/main/java/com/playshogi/library/database/GameRepository.java @@ -100,13 +100,18 @@ public PersistentGame getGameById(final int gameId) { } public List getGamesFromGameSet(final int gameSetId) { + return getGamesFromGameSet(gameSetId, false); + } + + public List getGamesFromGameSet(final int gameSetId, final boolean reducedLogging) { ArrayList games = new ArrayList<>(); Connection connection = dbConnection.getConnection(); try (PreparedStatement preparedStatement = connection.prepareStatement(SELECT_GAMES_FROM_GAMESET)) { preparedStatement.setInt(1, gameSetId); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { - LOGGER.log(Level.INFO, "Found game with id: " + rs.getInt("game_id")); + if (!reducedLogging) + LOGGER.log(Level.INFO, "Found game with id: " + rs.getInt("game_id")); int kifuId = rs.getInt("kifu_id"); int gameId = rs.getInt("game_id"); diff --git a/playshogi-library-database/src/main/java/com/playshogi/library/database/KifuRepository.java b/playshogi-library-database/src/main/java/com/playshogi/library/database/KifuRepository.java index 10f2fcca..726f7b8e 100644 --- a/playshogi-library-database/src/main/java/com/playshogi/library/database/KifuRepository.java +++ b/playshogi-library-database/src/main/java/com/playshogi/library/database/KifuRepository.java @@ -77,12 +77,17 @@ public int saveKifu(final GameRecord gameRecord, final String name, final int au } public PersistentKifu getKifuById(final int kifuId) { + return getKifuById(kifuId, false); + } + + public PersistentKifu getKifuById(final int kifuId, final boolean reducedLogging) { Connection connection = dbConnection.getConnection(); try (PreparedStatement preparedStatement = connection.prepareStatement(SELECT_KIFU)) { preparedStatement.setInt(1, kifuId); ResultSet rs = preparedStatement.executeQuery(); if (rs.next()) { - LOGGER.log(Level.INFO, "Found kifu: " + rs.getString("name") + " with id: " + rs.getInt("id")); + if (!reducedLogging) + LOGGER.log(Level.INFO, "Found kifu: " + rs.getString("name") + " with id: " + rs.getInt("id")); String name = rs.getString("name"); int authorId = rs.getInt("author_id"); String usfString = rs.getString("usf"); diff --git a/playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchFilter.java b/playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchFilter.java new file mode 100644 index 00000000..c5af4d27 --- /dev/null +++ b/playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchFilter.java @@ -0,0 +1,16 @@ +package com.playshogi.library.database.search; + +import com.playshogi.library.shogi.models.position.ReadOnlyShogiPosition; + +public class KifuSearchFilter { + + private final ReadOnlyShogiPosition partialPositionSearch; + + public KifuSearchFilter(final ReadOnlyShogiPosition partialPositionSearch) { + this.partialPositionSearch = partialPositionSearch; + } + + public ReadOnlyShogiPosition getPartialPositionSearch() { + return partialPositionSearch; + } +} diff --git a/playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchManager.java b/playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchManager.java new file mode 100644 index 00000000..b45d1582 --- /dev/null +++ b/playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchManager.java @@ -0,0 +1,78 @@ +package com.playshogi.library.database.search; + +import com.playshogi.library.database.DbConnection; +import com.playshogi.library.database.GameRepository; +import com.playshogi.library.database.KifuRepository; +import com.playshogi.library.database.models.PersistentGame; +import com.playshogi.library.database.models.PersistentKifu; +import com.playshogi.library.shogi.models.formats.sfen.SfenConverter; +import com.playshogi.library.shogi.models.position.ReadOnlyShogiPosition; +import com.playshogi.library.shogi.models.position.Square; +import com.playshogi.library.shogi.models.record.GameNavigation; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class KifuSearchManager { + + private static final Logger LOGGER = Logger.getLogger(KifuSearchManager.class.getName()); + + + private final GameRepository gameRepository = new GameRepository(new DbConnection()); + private final KifuRepository kifuRepository = new KifuRepository(new DbConnection()); + + private final List kifus = new ArrayList<>(); + + public KifuSearchManager() { + System.out.println("Loading games from db..."); + List games = gameRepository.getGamesFromGameSet(1, true); + for (PersistentGame game : games) { + if (game.getKifuId() % 1000 == 0) System.out.println("Loading #" + game.getKifuId()); + try { + kifus.add(kifuRepository.getKifuById(game.getKifuId(), true)); + } catch (Exception ex) { + LOGGER.log(Level.SEVERE, "Error loading the kifu " + game.getKifuId(), ex); + } + } + System.out.println("Loaded games from db!"); + } + + public List searchGames(final KifuSearchFilter filter) { + List result = new ArrayList<>(); + for (PersistentKifu persistentKifu : kifus) { + GameNavigation navigation = new GameNavigation(persistentKifu.getKifu().getGameTree()); + int movec = 0; + while (navigation.canMoveForward() && movec++ < 40) { + navigation.moveForward(); + if (positionContains(navigation.getPosition(), filter.getPartialPositionSearch())) { + System.out.println(persistentKifu.getKifu().getGameInformation() + " - In kifu #" + persistentKifu.getId() + ":"); + System.out.println(navigation.getPosition()); + + result.add(new KifuSearchResult(persistentKifu, navigation.getPosition())); + break; + } + } + } + + return result; + } + + private boolean positionContains(final ReadOnlyShogiPosition fullPosition, + final ReadOnlyShogiPosition partialPosition) { + for (Square square : partialPosition.getAllSquares()) { + if (partialPosition.isEmptySquare(square)) continue; + if (fullPosition.isEmptySquare(square)) return false; + if (partialPosition.getPieceAt(square).get() != fullPosition.getPieceAt(square).get()) return false; + } + + //TODO komadai? + return true; + } + + public static void main(String[] args) { + KifuSearchManager manager = new KifuSearchManager(); + manager.searchGames(new KifuSearchFilter(SfenConverter.fromSFEN("9/9/9/9/9/9/9/6R2/9 b kr2b4g4s4n4l18p"))); + } +} diff --git a/playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchResult.java b/playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchResult.java new file mode 100644 index 00000000..d4f729e8 --- /dev/null +++ b/playshogi-library-database/src/main/java/com/playshogi/library/database/search/KifuSearchResult.java @@ -0,0 +1,22 @@ +package com.playshogi.library.database.search; + +import com.playshogi.library.database.models.PersistentKifu; +import com.playshogi.library.shogi.models.position.ReadOnlyShogiPosition; + +public class KifuSearchResult { + private final PersistentKifu kifu; + private final ReadOnlyShogiPosition position; + + public KifuSearchResult(final PersistentKifu kifu, final ReadOnlyShogiPosition position) { + this.kifu = kifu; + this.position = position; + } + + public PersistentKifu getKifu() { + return kifu; + } + + public ReadOnlyShogiPosition getPosition() { + return position; + } +} diff --git a/playshogi-library-shogi/src/main/java/com/playshogi/library/shogi/models/formats/usf/UsfMoveConverter.java b/playshogi-library-shogi/src/main/java/com/playshogi/library/shogi/models/formats/usf/UsfMoveConverter.java index 057726c8..478e696b 100644 --- a/playshogi-library-shogi/src/main/java/com/playshogi/library/shogi/models/formats/usf/UsfMoveConverter.java +++ b/playshogi-library-shogi/src/main/java/com/playshogi/library/shogi/models/formats/usf/UsfMoveConverter.java @@ -13,7 +13,7 @@ public class UsfMoveConverter { "MATE", "REPT", "NMAT", "++++", "++..", "+...", "====", "-...", "--..", "----", "=88=", "+88-"}; public static final SpecialMoveType[] specialTypes = {null, null, SpecialMoveType.SILENT, SpecialMoveType.RESIGN, - null, + SpecialMoveType.BREAK, SpecialMoveType.JISHOGI, SpecialMoveType.TIMEOUT, SpecialMoveType.ILLEGAL_MOVE, null, SpecialMoveType.CHECKMATE, SpecialMoveType.SENNICHITE, null, null, null, null, null, null, null, null, null, null};