IOKernel / Sangatsu

A barebone chess.com cheat
3 stars 5 forks source link

Make a lichess port #16

Open SolsticeSpectrum opened 1 year ago

SolsticeSpectrum commented 1 year ago

Would be nice to have this for lichess too

SolsticeSpectrum commented 1 year ago

Btw I tried to code it but their system is so drastically different. Couldn't make selection work and couldn't make detect move work.

I tried something like this but it doesn't work

def highlight_move(driver, best_move):
    first_square = str(best_move)[:2]
    second_square = str(best_move)[2:]
    first_coord = str(ord(first_square[0])-96) + first_square[1]
    second_coord = str(ord(second_square[0])-96) + second_square[1]
    driver.execute_script(f"""
    var squares = document.getElementsByClassName("square");
    var first_coord_x = 0;
    var first_coord_y = 0;
    var second_coord_x = 0;
    var second_coord_y = 0;
    if("{first_coord}" < "41"){{
        first_coord_x = "{first_coord}" * 62;
        first_coord_y = 0;
    }}
    else{{
        first_coord_x = "{first_coord}" % 40 * 62;
        first_coord_y = 62;
    }}
    if("{second_coord}" < "41"){{
        second_coord_x = "{second_coord}" * 62;
        second_coord_y = 0;
    }}
    else{{
        second_coord_x = "{second_coord}" % 40 * 62;
        second_coord_y = 62;
    }}
    for(var i=0; i<squares.length; i++) {{
        if(squares[i].style.transform === "translate("+ first_coord_x +"px, "+ first_coord_y +"px)") {{
            squares[i].classList.add("selected");
        }}
        if(squares[i].style.transform === "translate("+ second_coord_x +"px, "+ second_coord_y +"px)") {{
            squares[i].classList.add("selected");
        }}
    }}
    """)

def detect_move(driver, move_number:int):
    # wait for either the move or the game to end
    move_number += 1
    xpath = f'//l4x/i5z[{move_number}]/following-sibling::kwdb[1]'
    player_color = "Your turn" in driver.page_source
    if player_color:
        xpath = f'//l4x/i5z[{move_number}]/following-sibling::kwdb[2]'
    move = WebDriverWait(driver, 360).until(
        EC.presence_of_element_located((By.XPATH, xpath)))
    print(move.text)
    return move.text
SolsticeSpectrum commented 1 year ago

@IOKernel Some reference can be found here https://github.com/wtfseanscool/lichess-autoplay/blob/main/lichess_autoplay.py

SolsticeSpectrum commented 1 year ago

Also keep in mind board rotation as it flips the values completely. Also keep in mind size of the board as that also flips the numbers completely. Yeah it will be slightly harder than chess.com that positions the highlighting automatically when injected

IOKernel commented 1 year ago

It would be easier to make a computer vision python script instead that detects the board and chess pieces, which will work on all chess websites. But I decided to do the chess.com one with selenium to learn the framework

SolsticeSpectrum commented 1 year ago

It would be easier to make a computer vision python script instead that detects the board and chess pieces, which will work on all chess websites. But I decided to do the chess.com one with selenium to learn the framework

He added arrow drawing. This is probably the best way so you don't have to screw around with the pixel values changing on resize. He already has all the XPATH logic too so if you wanted you could add some kind of parameter to switch between chess.com and lichess.org. I would still prefer having both under one umbrella. Also it would be nice to have separate credentials in that case.

https://github.com/wtfseanscool/lichess-autoplay/commit/cd2ccffd4059c3f089c84f65c050ac3f0c9cb31d