mliebelt / pgn-viewer

Simple PGN viewer with the necessary features to display chess games
GNU General Public License v3.0
164 stars 44 forks source link

a question on pgnvjs.js #101

Closed cgencer closed 5 years ago

cgencer commented 6 years ago

Hi again, I have a question on adding moves for branches. I am mimicking a few parts of your drag/snap codes within my code (for adding custom behaviours). I use this code:

        var thePGN = theBoard.getPgn();
        if(currentMoveNotation !== ''){
            console.log(currentMoveNotation+'...'+themno);
            var curMove = thePGN.addMove(currentMoveNotation, themno);
            var pgnMove = thePGN.getMove(curMove);
            themno = pgnMove.moveNumber;
            console.dir(pgnMove);
            chess.load(pgnMove.fen);
            cbUI.position(pgnMove.fen);
//          console.dir(pgnMove);

            $('#gamecode').text(thePGN.write_pgn());

This works fine for the first move in a new branch, but whenever I add a second move to this branch right afterwards, it gives error. TypeError: pgn_move is null (Line19612) which points to addMove and then existing_move afterwards.

themno is the move no obtained from the 'to-be'branching' and existing move (e.g. .moveNumber). But as the second move added has no moveNumber attached to it, I cant add it to the branch. With removing themno= line it adds many branches (with only one move) to the main line serially.( e.g. xxx (n. yyy) (n. zzz) ) and so on. So I try to find a way to add moves sequentially to a specific main which I have the moveNumber for. If you have some minutes, I'd be glad to hear from you what is the required steps I need to take for adding moves into a branch. Vielen Dank für Ihre Zeit! :)

mliebelt commented 6 years ago

Sorry, I have not too much time at the moment. Hopefully, I will be able next Sunday to look into your code, and see what the problem is. The main focus of PgnViewerJS is a common UI to display boards, game notation, play through existing games and even edit new ones. So it is (yet) not meant as universal toolkit for everything you would possible do in playing chess.

Some of the enhancements may simple, some of the things found may be bugs. Bug some are just not anticipated, and will not.

mliebelt commented 6 years ago

I checked the code (as good as possible from what I have seen), and I don't see a problem. The notation currentMove is misleading. In my case (playing on a visual board), I have all the time only one position which is on the board shown, and set in the chess object inside. The currentMove is the number to the array of moves held by the pgn object.

So I have no recommendation for what you want to do, just a tip:

It would help if I would understand what you want to do. So I am able to use the pgnEdit function to have a board to play new games (including variations). So the library is able to add variations without a problem.

I have some Jasmine tests that correspond to what you want to do. Have a look at PgnViewerJS/test/spec/pgnSpec.js, there the lines from 613. Try to create first a test case, and understand how addMove works.

Give me some hints what you want to do, by telling me the whole scenario, and perhaps I am able to help then. And I will try to come up with a short description how to install the development version of PgnViewerJS, and to use that in your own code then.

Have a look at the latest version of dev.md in the repository on GitHub (https://github.com/mliebelt/PgnViewerJS/blob/master/dev.md).

cgencer commented 6 years ago

thanks a lot for taking the time, really i appreciate your help. kudos! after examining deeply, i dropped my faulty approach and started from scratch with restyling the design for my purposes a bit. it went quite ok after your suggestions. i have now a few thoughts, maybe you can use the one or other...

best!

mliebelt commented 6 years ago

Just fixed en passant (see #108 for details). So at least this should be no more a problem. And if you have an idea (and some code) how to catch the draw events (circles, arrows, ...) in Chessground, I would be really glad. I am not at all an expert in Typescript and Javascript, and I have no idea to find a way to register those events.

mliebelt commented 5 years ago

Fixed #91 (drawing arrows and circles) as well, will close that question, because there will no further work on it.