diff --git a/pom.xml b/pom.xml index ae5fcff..5bf3335 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.example ChessLise - 14-SNAPSHOT + 14.1-SNAPSHOT diff --git a/src/main/java/Discord/HelperModules/PuzzleSolverContextModule.java b/src/main/java/Discord/HelperModules/PuzzleSolverContextModule.java index 30f81dc..89f7505 100644 --- a/src/main/java/Discord/HelperModules/PuzzleSolverContextModule.java +++ b/src/main/java/Discord/HelperModules/PuzzleSolverContextModule.java @@ -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; @@ -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"); } @@ -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 ** while devs solve this!").queue(); + } } @@ -110,23 +123,7 @@ public void getSolverCard(SlashCommandInteractionEvent slashEvent){ } - - - - - - - - - - - - - - - - - - + } + diff --git a/src/main/java/Discord/HelperModules/ToolContextModule.java b/src/main/java/Discord/HelperModules/ToolContextModule.java index e294ecf..3853fe3 100644 --- a/src/main/java/Discord/HelperModules/ToolContextModule.java +++ b/src/main/java/Discord/HelperModules/ToolContextModule.java @@ -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 ** while devs solve this!").setEphemeral(true).queue(); + } } } @@ -130,4 +134,4 @@ public void sendPlayChallengeCommand(SlashCommandInteractionEvent slashEvent, Me -} +} \ No newline at end of file diff --git a/src/main/java/Engine/StockFishInternalErrorException.java b/src/main/java/Engine/StockFishInternalErrorException.java new file mode 100644 index 0000000..d6a06e6 --- /dev/null +++ b/src/main/java/Engine/StockFishInternalErrorException.java @@ -0,0 +1,8 @@ +package Engine; + +public class StockFishInternalErrorException extends Exception{ + + public StockFishInternalErrorException(String msg){ + super(msg); + } +}