Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a lichess port #16

Open
SolsticeSpectrum opened this issue Jan 26, 2023 · 5 comments
Open

Make a lichess port #16

SolsticeSpectrum opened this issue Jan 26, 2023 · 5 comments

Comments

@SolsticeSpectrum
Copy link

Would be nice to have this for lichess too

@SolsticeSpectrum
Copy link
Author

SolsticeSpectrum commented Jan 26, 2023

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
Copy link
Author

@SolsticeSpectrum
Copy link
Author

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
Copy link
Owner

IOKernel commented Feb 3, 2023

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
Copy link
Author

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.

wtfseanscool/lichess-autoplay@cd2ccff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants