Skip to content

Commit

Permalink
Search kifu - send param from client to server
Browse files Browse the repository at this point in the history
  • Loading branch information
Tellmarch committed Jan 14, 2024
1 parent 0146843 commit c24b15e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
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.GameDetails;
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;

import java.util.Arrays;

public class GameCollectionActivity extends MyAbstractActivity {

private final KifuServiceAsync kifuService = GWT.create(KifuService.class);
Expand Down Expand Up @@ -143,9 +141,29 @@ public void onSuccess(final Void unused) {
public void onSearchKifus(final SearchKifusEvent event) {
GWT.log("GameCollectionActivity Handling SearchKifusEvent");

eventBus.fireEvent(new ListCollectionGamesEvent(
Arrays.stream(games.getGames()).filter(x -> event.getPlayer().equals(x.getSente())).toArray(GameDetails[]::new),
games.getDetails()));

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

public class SearchKifusEvent extends GenericEvent {
private final GameResult result;

private final String player;
private final String partialPositionSfen;

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

public GameResult getResult() {
Expand All @@ -20,4 +21,8 @@ public GameResult getResult() {
public String getPlayer() {
return player;
}

public String getPartialPositionSfen() {
return partialPositionSfen;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ public class SearchKifuForm {
private Select<GameResult> gameResult;
private SuggestBox<String> playerName;
private EventBus eventBus;
private TextBox partialSearch;

public HtmlContentBuilder<HTMLDivElement> getForm() {
if (div != null) {
return div;
}

partialSearch = TextBox.create().setLabel("Search for partial position").setHelperText("SFEN");

playerName = SuggestBox.create("Player:", playerNames)
.setHelperText("Type any letter and see suggestions");

Expand All @@ -39,7 +42,7 @@ public HtmlContentBuilder<HTMLDivElement> getForm() {
.setSearchable(false)
.selectAt(0);

div = Elements.div().add(playerName).add(gameResult);
div = Elements.div().add(partialSearch).add(playerName).add(gameResult);

return div;
}
Expand All @@ -59,7 +62,8 @@ public void showInPopup() {
closeButton.addClickListener(evt -> modal.close());
Button searchButton = Button.create("SEARCH").linkify();
searchButton.addClickListener(evt -> {
eventBus.fireEvent(new SearchKifusEvent(gameResult.getValue(), playerName.getValue()));
eventBus.fireEvent(new SearchKifusEvent(gameResult.getValue(), playerName.getValue(),
partialSearch.getValue()));
modal.close();
});
modal.appendFooterChild(searchButton);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.playshogi.library.database.*;
import com.playshogi.library.database.models.*;
import com.playshogi.library.database.models.PersistentKifu.KifuType;
import com.playshogi.library.database.search.KifuSearchFilter;
import com.playshogi.library.database.search.KifuSearchManager;
import com.playshogi.library.database.search.KifuSearchResult;
import com.playshogi.library.shogi.engine.*;
import com.playshogi.library.shogi.engine.insights.GameInsights;
import com.playshogi.library.shogi.engine.insights.Mistake;
Expand Down Expand Up @@ -44,6 +47,8 @@ public class KifuServiceImpl extends RemoteServiceServlet implements KifuService
private final TsumeEscapeSolver tsumeEscapeSolver = new TsumeEscapeSolver(queuedTsumeSolver);
private final QueuedKifuAnalyzer queuedKifuAnalyzer = new QueuedKifuAnalyzer(EngineConfiguration.NORMAL_ENGINE);

private final KifuSearchManager kifuSearchManager = new KifuSearchManager();

public KifuServiceImpl() {
DbConnection dbConnection = new DbConnection();
gameSetRepository = new GameSetRepository(dbConnection);
Expand Down Expand Up @@ -227,6 +232,49 @@ public GameCollectionDetailsAndGames getGameSetKifuDetails(final String sessionI
return result;
}

@Override
public GameCollectionDetailsAndGames getGameSetKifuDetailsWithFilter(final String sessionId,
final String gameSetId,
final KifuSearchFilterDetails filterDetails) {
LOGGER.log(Level.INFO, "getGameSetKifuDetailsWithFilter:\n" + gameSetId + "\n" + filterDetails);

//TODO access control

LoginResult loginResult = authenticator.checkSession(sessionId);
if (loginResult == null || !loginResult.isLoggedIn() || loginResult.getUserId() != 2) {
throw new IllegalStateException("Only some logged in users can search for games");
}

PersistentGameSet gameSet = gameSetRepository.getGameSetById(Integer.parseInt(gameSetId));
if (gameSet == null) {
throw new IllegalArgumentException("Invalid gameSet ID");
}

List<PersistentGame> games = gameRepository.getGamesFromGameSet(Integer.parseInt(gameSetId));

List<KifuSearchResult> kifuSearchResults = kifuSearchManager.searchGames(createKifuSearchFilter(filterDetails));


GameCollectionDetailsAndGames result = new GameCollectionDetailsAndGames();
result.setDetails(getCollectionDetails(gameSet));
result.setGames(games.stream().filter(g -> matchesFilter(filterDetails, kifuSearchResults, g))
.map(this::createGameDetails).toArray(GameDetails[]::new));

return result;
}

private boolean matchesFilter(final KifuSearchFilterDetails filterDetails,
final List<KifuSearchResult> kifuSearchResults, PersistentGame game) {
String playerName = filterDetails.getPlayerName();
if (!Strings.isNullOrEmpty(playerName) && !game.getSenteName().startsWith(playerName) && !game.getSenteName().startsWith(playerName))
return false;
return kifuSearchResults.stream().anyMatch(r -> r.getKifu().getId() == game.getKifuId());
}

private KifuSearchFilter createKifuSearchFilter(final KifuSearchFilterDetails filterDetails) {
return new KifuSearchFilter(SfenConverter.fromSFEN(filterDetails.getPartialPositionSearchSfen()));
}

private GameDetails createGameDetails(final PersistentGame game) {
GameDetails gameDetails = new GameDetails();
gameDetails.setId(String.valueOf(game.getId()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.playshogi.website.gwt.shared.models;

import java.io.Serializable;

public class KifuSearchFilterDetails implements Serializable {

private String partialPositionSearchSfen;
private String playerName;
private String gameResult;

public KifuSearchFilterDetails() {
}

public String getPartialPositionSearchSfen() {
return partialPositionSearchSfen;
}

public void setPartialPositionSearchSfen(final String partialPositionSearchSfen) {
this.partialPositionSearchSfen = partialPositionSearchSfen;
}

public String getPlayerName() {
return playerName;
}

public void setPlayerName(final String playerName) {
this.playerName = playerName;
}

public String getGameResult() {
return gameResult;
}

public void setGameResult(final String gameResult) {
this.gameResult = gameResult;
}

@Override
public String toString() {
return "KifuSearchFilterDetails{" +
"partialPositionSearchSfen='" + partialPositionSearchSfen + '\'' +
", playerName='" + playerName + '\'' +
", gameResult='" + gameResult + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public interface KifuService extends RemoteService {

GameCollectionDetailsAndGames getGameSetKifuDetails(String sessionId, String gameSetId);

GameCollectionDetailsAndGames getGameSetKifuDetailsWithFilter(String sessionId, String gameSetId,
KifuSearchFilterDetails filterDetails);

PositionDetails getPositionDetails(String sfen, String gameSetId);

PositionEvaluationDetails analysePosition(String sessionId, String sfen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ void saveGameAndAddToCollection(String sessionId, String kifuUsf, String collect
void getGameSetKifuDetails(String sessionId, String gameSetId,
AsyncCallback<GameCollectionDetailsAndGames> callback);

void getGameSetKifuDetailsWithFilter(String sessionId, String gameSetId, KifuSearchFilterDetails filterDetails,
AsyncCallback<GameCollectionDetailsAndGames> callback);

void getPositionDetails(String sfen, String gameSetId, AsyncCallback<PositionDetails> callback);

void analysePosition(String sessionId, String sfen, AsyncCallback<PositionEvaluationDetails> callback);
Expand Down

0 comments on commit c24b15e

Please sign in to comment.