Skip to content

Commit

Permalink
Search kifu - added button and form, search player
Browse files Browse the repository at this point in the history
  • Loading branch information
Tellmarch committed Jan 14, 2024
1 parent a5b19ba commit a01ebfb
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 5 deletions.
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 @@ -8,23 +8,24 @@
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.GameDetails;
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);
private EventBus eventBus;
private GameCollectionDetailsAndGames games;

interface MyEventBinder extends EventBinder<GameCollectionActivity> {
}
Expand Down Expand Up @@ -65,7 +66,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 +139,15 @@ public void onSuccess(final Void unused) {
});
}

@EventHandler
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()));
}

private void refresh() {
fetchData();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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;

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

public GameResult getResult() {
return result;
}

public String getPlayer() {
return player;
}
}
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.playshogi.website.gwt.client.widget.collections;

import com.google.web.bindery.event.shared.EventBus;
import com.playshogi.library.shogi.models.record.GameResult;
import com.playshogi.website.gwt.client.events.collections.SearchKifusEvent;
import elemental2.dom.HTMLDivElement;
import org.dominokit.domino.ui.button.Button;
import org.dominokit.domino.ui.forms.*;
import org.dominokit.domino.ui.modals.ModalDialog;
import org.jboss.elemento.Elements;
import org.jboss.elemento.HtmlContentBuilder;

import java.util.List;

public class SearchKifuForm {


LocalSuggestBoxStore<String> playerNames = LocalSuggestBoxStore.create();
private HtmlContentBuilder<HTMLDivElement> div = null;

private Select<GameResult> gameResult;
private SuggestBox<String> playerName;
private EventBus eventBus;

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

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

gameResult = Select.<GameResult>create("Game Result")
.appendChild(SelectOption.create(null, "Any result"))
.appendChild(SelectOption.create(GameResult.BLACK_WIN, "Black wins"))
.appendChild(SelectOption.create(GameResult.WHITE_WIN, "White wins"))
.appendChild(SelectOption.create(GameResult.OTHER, "Other"))
.appendChild(SelectOption.create(GameResult.UNKNOWN, "Unknown"))
.setSearchable(false)
.selectAt(0);

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

return div;
}


public void updatePlayerNames(final List<String> names) {
playerNames.getSuggestions().clear();
for (String name : names) {
playerNames.addSuggestion(SuggestItem.create(name));
}
}

public void showInPopup() {
ModalDialog modal = ModalDialog.create("Search for Kifus").setAutoClose(false);
modal.appendChild(getForm());
Button closeButton = Button.create("CANCEL").linkify();
closeButton.addClickListener(evt -> modal.close());
Button searchButton = Button.create("SEARCH").linkify();
searchButton.addClickListener(evt -> {
eventBus.fireEvent(new SearchKifusEvent(gameResult.getValue(), playerName.getValue()));
modal.close();
});
modal.appendFooterChild(searchButton);
modal.appendFooterChild(closeButton);
modal.open();
}

public void activate(final EventBus eventBus) {
this.eventBus = eventBus;
}
}

0 comments on commit a01ebfb

Please sign in to comment.