Skip to content

Commit

Permalink
Search kifu - start of server code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tellmarch committed Jan 14, 2024
1 parent a01ebfb commit 0146843
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,18 @@ public PersistentGame getGameById(final int gameId) {
}

public List<PersistentGame> getGamesFromGameSet(final int gameSetId) {
return getGamesFromGameSet(gameSetId, false);
}

public List<PersistentGame> getGamesFromGameSet(final int gameSetId, final boolean reducedLogging) {
ArrayList<PersistentGame> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<PersistentKifu> kifus = new ArrayList<>();

public KifuSearchManager() {
System.out.println("Loading games from db...");
List<PersistentGame> 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<KifuSearchResult> searchGames(final KifuSearchFilter filter) {
List<KifuSearchResult> 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")));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 0146843

Please sign in to comment.