Skip to content

Commit

Permalink
refactor(fix puzzle warnings): fixes the right warning by intersectin…
Browse files Browse the repository at this point in the history
…g the righe exception for move conversion or Stockfish error. fixes #60
  • Loading branch information
jalpp authored Jun 9, 2024
1 parent 11f95af commit 7f34f9f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>ChessLise</artifactId>
<version>14-SNAPSHOT</version>
<version>14.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
Expand Down
87 changes: 42 additions & 45 deletions src/main/java/Discord/HelperModules/PuzzleSolverContextModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import Abstraction.ChessUtil;
import Engine.StockFish;
import Engine.StockFishInternalErrorException;
import com.github.bhlangonijr.chesslib.Board;
import com.github.bhlangonijr.chesslib.move.MoveConversionException;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;

Expand All @@ -25,31 +27,39 @@ public PuzzleSolverContextModule(String userID, String FEN){


public void getPuzzleSolverCard(SlashCommandInteractionEvent slashEvent){
ChessUtil util = new ChessUtil();
EmbedBuilder puzzleBuilder = new EmbedBuilder();
puzzleBuilder.setTitle(slashEvent.getUser().getName() + "'s Puzzle Challenge!");
puzzleBuilder.setDescription("Its **" + util.getWhichSideToMove(getUserPuzzleFEN.get(slashEvent.getUser().getId())) +
"** Find the best move! ** use /solve [move ex: e4]** to solve the puzzle! \uD83E\uDDE9 ");
puzzleBuilder.setImage(util.getImageFromFEN(getUserPuzzleFEN.get(slashEvent.getUser().getId()), false, "green", "alpha"));
slashEvent.deferReply(false).queue();
slashEvent.getHook().sendMessageEmbeds(puzzleBuilder.build()).queue();
try {
ChessUtil util = new ChessUtil();
EmbedBuilder puzzleBuilder = new EmbedBuilder();
puzzleBuilder.setTitle(slashEvent.getUser().getName() + "'s Puzzle Challenge!");
puzzleBuilder.setDescription("Its **" + util.getWhichSideToMove(getUserPuzzleFEN.get(slashEvent.getUser().getId())) +
"** Find the best move! ** use /solve [move ex: e4]** to solve the puzzle! \uD83E\uDDE9 ");
puzzleBuilder.setImage(util.getImageFromFEN(getUserPuzzleFEN.get(slashEvent.getUser().getId()), false, "green", "alpha"));
slashEvent.deferReply(false).queue();
slashEvent.getHook().sendMessageEmbeds(puzzleBuilder.build()).queue();
}catch (Exception e){
slashEvent.reply("Internal server error! Please contact the dev by joining the support server in /help").setEphemeral(true).queue();
}
}

public void determineTheFinishLine(SlashCommandInteractionEvent slashEvent){
Board hookBoard = new Board();
hookBoard.loadFromFen(getUserPuzzleFEN.get(slashEvent.getUser().getId()));

double eval = Double.parseDouble(StockFish.getEvalForFEN(15, getUserPuzzleFEN.get(slashEvent.getUser().getId())));

if(eval >= -0.99 && eval <= 0.99){
getUserPuzzleHitRate.put(slashEvent.getUser().getId(), 4);
getUserFinishLine.put(slashEvent.getUser().getId(), getUserFinishLine.get(slashEvent.getUser().getId()) + 1);
}else if (eval >= -2.99 && eval <= 2.99){
getUserFinishLine.put(slashEvent.getUser().getId(), getUserFinishLine.get(slashEvent.getUser().getId()) + 1);
getUserPuzzleHitRate.put(slashEvent.getUser().getId(), 2);
}else{
getUserFinishLine.put(slashEvent.getUser().getId(), getUserFinishLine.get(slashEvent.getUser().getId()) + 1);
getUserPuzzleHitRate.put(slashEvent.getUser().getId(), 1);
public void determineTheFinishLine(SlashCommandInteractionEvent slashEvent) throws StockFishInternalErrorException {
try {
Board hookBoard = new Board();
hookBoard.loadFromFen(getUserPuzzleFEN.get(slashEvent.getUser().getId()));

double eval = Double.parseDouble(StockFish.getEvalForFEN(15, getUserPuzzleFEN.get(slashEvent.getUser().getId())));

if (eval >= -0.99 && eval <= 0.99) {
getUserPuzzleHitRate.put(slashEvent.getUser().getId(), 4);
getUserFinishLine.put(slashEvent.getUser().getId(), getUserFinishLine.get(slashEvent.getUser().getId()) + 1);
} else if (eval >= -2.99 && eval <= 2.99) {
getUserFinishLine.put(slashEvent.getUser().getId(), getUserFinishLine.get(slashEvent.getUser().getId()) + 1);
getUserPuzzleHitRate.put(slashEvent.getUser().getId(), 2);
} else {
getUserFinishLine.put(slashEvent.getUser().getId(), getUserFinishLine.get(slashEvent.getUser().getId()) + 1);
getUserPuzzleHitRate.put(slashEvent.getUser().getId(), 1);
}
}catch (Exception e){
throw new StockFishInternalErrorException("eval not working");
}


Expand Down Expand Up @@ -95,13 +105,16 @@ public void getSolverCard(SlashCommandInteractionEvent slashEvent){
getUserPuzzleFEN.put(slashEvent.getUser().getId(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
getUserFinishLine.put(slashEvent.getUser().getId(), 0);
getUserPuzzleHitRate.put(slashEvent.getUser().getId(), 1);
slashEvent.reply("You have solved the puzzle!").queue();
slashEvent.reply("You have solved the puzzle!").queue ();
}


}catch (Exception e){
slashEvent.reply("You entered an illegal move! Please try again!").queue();
System.out.println(e.getMessage());
}catch (MoveConversionException | StockFishInternalErrorException e ){
if(e instanceof MoveConversionException){
slashEvent.reply("You entered an illegal move! Please try again!").queue();
}else {
slashEvent.reply("Internal Stockfish Error! Seems like engine is not working, why don't you run **/puzzle <post to community>** while devs solve this!").queue();
}
}


Expand All @@ -110,23 +123,7 @@ public void getSolverCard(SlashCommandInteractionEvent slashEvent){

}





















}

8 changes: 6 additions & 2 deletions src/main/java/Discord/HelperModules/ToolContextModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ public void sendPuzzleSolverTrigger(SlashCommandInteractionEvent slashEvent){
PuzzleSolverContextModule solver = solverMapper.get(slashEvent.getUser().getId());
solver.getSolverCard(slashEvent);
}catch (Exception e){
slashEvent.reply("You have not created a puzzle challenge! please use /puzzle!").queue();
if(e instanceof NullPointerException){
slashEvent.reply("You have not created a puzzle challenge! please use /puzzle!").setEphemeral(true).queue();
}else{
slashEvent.reply("Internal Stockfish Error! Seems like engine is not working, why don't you run **/puzzle <post to community>** while devs solve this!").setEphemeral(true).queue();
}
}

}
Expand All @@ -130,4 +134,4 @@ public void sendPlayChallengeCommand(SlashCommandInteractionEvent slashEvent, Me



}
}
8 changes: 8 additions & 0 deletions src/main/java/Engine/StockFishInternalErrorException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Engine;

public class StockFishInternalErrorException extends Exception{

public StockFishInternalErrorException(String msg){
super(msg);
}
}

0 comments on commit 7f34f9f

Please sign in to comment.