Skip to content

Commit

Permalink
fix more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jalpp authored Dec 21, 2023
1 parent 876b2f5 commit e191eea
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/Abstraction/Context/ContextHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package Abstraction.Context;

import Discord.MainHandler.AntiSpam;
import chariot.Client;
import com.github.bhlangonijr.chesslib.Board;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;

public interface ContextHandler {


public void handleLogic(MessageContextInteractionEvent context, SlashCommandInteractionEvent slashEvent, ButtonInteractionEvent buttonEvent, ModalInteractionEvent eventModal, Client client , Board board, Board blackboard
, AntiSpam spam, AntiSpam dailyspam, AntiSpam watchlimit);




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

import chariot.Client;
import chariot.model.Enums;
import chariot.model.Event;
import chariot.model.GameStateEvent;
import chariot.model.VariantType;
import chariot.model.Enums.Speed;

import com.github.bhlangonijr.chesslib.*;
import com.github.bhlangonijr.chesslib.move.Move;

import java.util.Random;

public class LichessBotRunner {

public LichessBotRunner(){

}


public static void main(String[] args) {

var client = Client.auth(System.getenv("LICHESS_TOKEN"));
var bot = client.bot();
var events = bot.connect().stream();
var username = client.account().profile().get().name().toLowerCase();
String[] s = {"maia1", "maia5", "maia9", "charibot", "TurtleBot", "SxRandom", "zeekat", "roundmoundofrebounds", "WorstFish", "pawnrobot", "knucklefish"};
int picker = s.length;
int index = new Random().nextInt(picker);
String botname = s[index];
try {
bot.challengeKeepAlive("Maia1", challengeBuilder -> challengeBuilder.clockBullet1m0s().rated(false).color(Enums.ColorPref.white));
}catch (Exception e){
System.out.println("Failed Challege");
}

events.forEach(event -> {


switch (event.type()) {

case challenge:

var challenge = (Event.ChallengeEvent) event;
boolean std = challenge.challenge().gameType().variant() == VariantType.Variant.standard;
boolean non_rated = challenge.challenge().gameType().rated();
boolean isCoores = challenge.challenge().gameType().timeControl().speed() == Speed.correspondence;
if(std && !non_rated && !isCoores ){
bot.acceptChallenge(event.id());
}else if(non_rated){
bot.declineChallenge(event.id(), provider -> provider.casual());

}else if(isCoores) {
bot.declineChallenge(event.id(), provider -> provider.timeControl());

}
else{
bot.declineChallenge(event.id(), provider -> provider.variant());
}

break;



case challengeDeclined:
bot.abort(event.id());

break;

case challengeCanceled:
bot.abort(event.id());

break;

case gameFinish:
bot.chat(event.id(), "Thanks for playing me! ggs");
bot.chatSpectators(event.id(), "Thanks for watching!");

break;


case gameStart:

var gameEvents = bot.connectToGame(event.id()).stream();
var board = new Board();
var engine = new LiseChessEngine(board);
boolean[] isWhite = {false};



bot.chat(event.id(), "omg you are very strong.. I'm scared but hey good luck!!");



gameEvents.forEach(gameEvent -> {




switch (gameEvent.type()) {


case opponentGone:
bot.abort(event.id());

break;

case gameFull:

try {

isWhite[0] = ((GameStateEvent.Full) gameEvent).white().name().toLowerCase().equals(username);
if (isWhite[0]) {
try {

var move = engine.generateMoveFromIndexLookUp(board);
bot.move(event.id(), move.toString());
board.doMove(move);

} catch (Exception e) {
bot.resign(event.id());
}
}

}catch (Exception e){
bot.resign(event.id());
System.out.println(e.getMessage());
}

break;

case gameState:
try {

var names = ((GameStateEvent.State) gameEvent).moves().split(" ");
var whiteTurn = names.length % 2 == 0;

if (isWhite[0] == whiteTurn) {
try {
var name = names[names.length - 1];
var from = Square.fromValue(name.substring(0, 2).toUpperCase());
var to = Square.fromValue(name.substring(2, 4).toUpperCase());
if (names.length == 5) {
var type = PieceType.fromSanSymbol(name.substring(4).toUpperCase());
var piece = Piece.make(whiteTurn ? Side.WHITE : Side.BLACK, type);
var move = new Move(from, to, piece);
board.doMove(move);
} else {
var move = new Move(from, to);
board.doMove(move);
}
}catch (Exception e){
bot.resign(event.id());
}


var move = engine.generateMoveFromIndexLookUp(board);
bot.move(event.id(), move.toString());
board.doMove(move);

}

}catch (Exception e){
bot.resign(event.id());

}
break;
}
});



break;
}
});


}



}

0 comments on commit e191eea

Please sign in to comment.