Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepingchinchilla committed Feb 25, 2024
2 parents 674bb94 + c24b15e commit 9028418
Show file tree
Hide file tree
Showing 15 changed files with 393 additions and 8 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 @@ -27,5 +27,8 @@ public void parseDate() {
simpleDateFormat.format(GameSetRepository.parseDate("Aug 31, 2020 6:39:53 PM"))); // old SC24
assertEquals("2013/07/19",
simpleDateFormat.format(GameSetRepository.parseDate("19/07/2013"))); // old date
assertEquals("1913/07/14",
simpleDateFormat.format(GameSetRepository.parseDate("1913/07/14"))); // old date

}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
import com.google.web.bindery.event.shared.binder.EventHandler;
import com.playshogi.library.shogi.models.formats.usf.UsfFormat;
import com.playshogi.website.gwt.client.SessionInformation;
import com.playshogi.website.gwt.client.events.collections.ListCollectionGamesEvent;
import com.playshogi.website.gwt.client.events.collections.RemoveGameFromCollectionEvent;
import com.playshogi.website.gwt.client.events.collections.SaveCollectionDetailsResultEvent;
import com.playshogi.website.gwt.client.events.collections.SaveGameCollectionDetailsEvent;
import com.playshogi.website.gwt.client.events.collections.*;
import com.playshogi.website.gwt.client.events.kifu.ImportGameRecordEvent;
import com.playshogi.website.gwt.client.events.user.UserLoggedInEvent;
import com.playshogi.website.gwt.client.place.GameCollectionPlace;
import com.playshogi.website.gwt.client.ui.GameCollectionView;
import com.playshogi.website.gwt.shared.models.GameCollectionDetailsAndGames;
import com.playshogi.website.gwt.shared.models.KifuSearchFilterDetails;
import com.playshogi.website.gwt.shared.services.KifuService;
import com.playshogi.website.gwt.shared.services.KifuServiceAsync;
import org.dominokit.domino.ui.notifications.Notification;
Expand All @@ -25,6 +23,7 @@ public class GameCollectionActivity extends MyAbstractActivity {

private final KifuServiceAsync kifuService = GWT.create(KifuService.class);
private EventBus eventBus;
private GameCollectionDetailsAndGames games;

interface MyEventBinder extends EventBinder<GameCollectionActivity> {
}
Expand Down Expand Up @@ -65,7 +64,8 @@ public void onFailure(Throwable throwable) {

@Override
public void onSuccess(GameCollectionDetailsAndGames result) {
GWT.log("GameCollectionActivity: retrieved collection games");
games = result;
GWT.log("GameCollectionActivity: retrieved collection games: " + result.getGames().length);
eventBus.fireEvent(new ListCollectionGamesEvent(result.getGames(), result.getDetails()));
}
});
Expand Down Expand Up @@ -137,6 +137,35 @@ public void onSuccess(final Void unused) {
});
}

@EventHandler
public void onSearchKifus(final SearchKifusEvent event) {
GWT.log("GameCollectionActivity Handling SearchKifusEvent");


KifuSearchFilterDetails filterDetails = new KifuSearchFilterDetails();
filterDetails.setPartialPositionSearchSfen(event.getPartialPositionSfen());
if (event.getResult() != null) filterDetails.setGameResult(event.getResult().name());
filterDetails.setPlayerName(event.getPlayer());

GWT.log("Querying for collection games with filter");
kifuService.getGameSetKifuDetailsWithFilter(sessionInformation.getSessionId(), place.getCollectionId(),
filterDetails,

new AsyncCallback<GameCollectionDetailsAndGames>() {
@Override
public void onFailure(Throwable throwable) {
GWT.log("GameCollectionActivity: error retrieving collection games with filter");
}

@Override
public void onSuccess(GameCollectionDetailsAndGames result) {
games = result;
GWT.log("GameCollectionActivity: retrieved collection games with filter: " + result.getGames().length);
eventBus.fireEvent(new ListCollectionGamesEvent(result.getGames(), result.getDetails()));
}
});
}

private void refresh() {
fetchData();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.playshogi.website.gwt.client.events.collections;

import com.google.web.bindery.event.shared.binder.GenericEvent;
import com.playshogi.library.shogi.models.record.GameResult;

public class SearchKifusEvent extends GenericEvent {
private final GameResult result;
private final String player;
private final String partialPositionSfen;

public SearchKifusEvent(final GameResult result, String player, String partialPositionSfen) {
this.result = result;
this.player = player;
this.partialPositionSfen = partialPositionSfen;
}

public GameResult getResult() {
return result;
}

public String getPlayer() {
return player;
}

public String getPartialPositionSfen() {
return partialPositionSfen;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import com.playshogi.website.gwt.client.place.OpeningsPlace;
import com.playshogi.website.gwt.client.tables.GameTable;
import com.playshogi.website.gwt.client.util.ElementWidget;
import com.playshogi.website.gwt.client.widget.collections.SearchKifuForm;
import com.playshogi.website.gwt.client.widget.collections.UploadKifusPopup;
import com.playshogi.website.gwt.client.widget.kifu.ImportKifuPanel;
import com.playshogi.website.gwt.shared.models.GameCollectionDetails;
import com.playshogi.website.gwt.shared.models.GameDetails;
import com.playshogi.website.gwt.shared.models.KifuDetails;
import elemental2.dom.HTMLAnchorElement;
import elemental2.dom.HTMLDivElement;
Expand All @@ -29,7 +31,9 @@

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

@Singleton
public class GameCollectionView extends Composite {
Expand All @@ -44,13 +48,17 @@ interface MyEventBinder extends EventBinder<GameCollectionView> {
private final GameTable gameTable;
private final ImportKifuPanel importKifuPanel = new ImportKifuPanel();
private final UploadKifusPopup uploadKifusPopup = new UploadKifusPopup(false, false, true, false, false);

private final SearchKifuForm searchKifuForm = new SearchKifuForm();
private final SessionInformation sessionInformation;
private final AppPlaceHistoryMapper historyMapper;
private final HtmlContentBuilder<HTMLAnchorElement> exploreLink;
private final Button newKifuButton;
private final Button addKifuButton;
private final Button uploadKifuButton;

private final Button searchKifuButton;

private EventBus eventBus;
private GameCollectionDetails collectionDetails;

Expand Down Expand Up @@ -87,6 +95,11 @@ public GameCollectionView(final SessionInformation sessionInformation, final App
})
.style().setMarginRight("3em"));

searchKifuButton = Button.createPrimary(Icons.ALL.database_search_mdi()).setContent("Search Kifu(s)");
root.add(searchKifuButton
.addClickListener(evt -> searchKifuForm.showInPopup())
.style().setMarginRight("3em"));

exploreLink = Elements.a("#");
root.add(exploreLink.add(Button.createSuccess(Icons.ALL.pie_chart()).setContent("Explore Openings")));

Expand All @@ -105,6 +118,7 @@ public void activate(final EventBus eventBus) {
gameTable.activate(eventBus);
importKifuPanel.activate(eventBus);
uploadKifusPopup.activate(eventBus);
searchKifuForm.activate(eventBus);
}

@EventHandler
Expand All @@ -122,6 +136,14 @@ public void onCollectionList(final ListCollectionGamesEvent event) {
collectionDetails.getId()));
exploreLink.attr("href", exploreHRef);

HashSet<String> playerNames = new HashSet<>();
for (GameDetails detail : event.getDetails()) {
if (detail.getSente() != null) playerNames.add(detail.getSente());
if (detail.getGote() != null) playerNames.add(detail.getGote());
}

searchKifuForm.updatePlayerNames(new ArrayList<>(playerNames));

if (isAuthor) {
newKifuButton.show();
addKifuButton.show();
Expand Down
Loading

0 comments on commit 9028418

Please sign in to comment.