Fix moveList + progress on game navigation system
[vchess.git] / public / javascripts / components / game.js
index bbef6f8..4048a3d 100644 (file)
@@ -1,9 +1,11 @@
 // TODO: envoyer juste "light move", sans FEN ni notation ...etc
+// TODO: also "observers" prop, we should send moves to them too (in a web worker ? webRTC ?)
 
 // Game logic on a variant page: 3 modes, analyze, computer or human
 Vue.component('my-game', {
        // gameId: to find the game in storage (assumption: it exists)
-       props: ["gameId","mode","allowChat","allowMovelist"],
+       // fen: to start from a FEN without identifiers (analyze mode)
+       props: ["conn","gameId","fen","mode","allowChat","allowMovelist"],
        data: function() {
                return {
                        // if oppid == "computer" then mode = "computer" (otherwise human)
@@ -12,7 +14,6 @@ Vue.component('my-game', {
                        oppid: "", //opponent ID in case of HH game
                        score: "*", //'*' means 'unfinished'
                        mycolor: "w",
-                       conn: null, //socket connection (et WebRTC connection ?!)
                        oppConnected: false, //TODO?
                        pgnTxt: "",
                        // sound level: 0 = no sound, 1 = sound only on newgame, 2 = always
@@ -21,13 +22,34 @@ Vue.component('my-game', {
                        compWorker: new Worker('/javascripts/playCompMove.js'),
                        timeStart: undefined, //time when computer starts thinking
                        vr: null, //VariantRules object, describing the game state + rules
+                       endgameMessage: "",
+                       orientation: "w",
+                       fenStart: "",
+                       
+                       moves: [], //TODO: initialize if gameId is defined...
+                       cursor: 0,
+                       // orientation :: button flip
+                       // userColor: given by gameId, or fen (if no game Id)
+                       // gameOver: known if gameId; otherwise assue false
+                       // lastMove: update after every play, initialize with last move from list (if continuation)
+                       //orientation ? userColor ? gameOver ? lastMove ?
+               
                };
        },
+       watch: {
+               fen: function(newFen) {
+                       this.vr = new VariantRules(newFen);
+               },
+               gameId: function() {
+                       this.loadGame();
+               },
+       },
        computed: {
                showChat: function() {
                        return this.allowChat && this.mode=='human' && this.score != '*';
                },
                showMoves: function() {
+                       return true;
                        return this.allowMovelist && window.innerWidth >= 768;
                },
                showFen: function() {
@@ -36,12 +58,16 @@ Vue.component('my-game', {
        },
        // Modal end of game, and then sub-components
        // TODO: provide chat parameters (connection, players ID...)
-       // and alwo moveList parameters (just moves ?)
+       // and also moveList parameters (just moves ?)
        // TODO: connection + turn indicators en haut à droite (superposé au menu)
        // TODO: controls: abort, clear, resign, draw (avec confirm box)
        // et si partie terminée : (mode analyse) just clear, back / play
        // + flip button toujours disponible
        // gotoMove : vr = new VariantRules(fen stocké dans le coup [TODO])
+       
+       // NOTE: move.color must be fulfilled after each move played, because of Marseille (or Avalanche) chess
+       // --> useful in moveList component (universal comma separator ?)
+       
        template: `
                <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2">
                        <input id="modal-eog" type="checkbox" class="modal"/>
@@ -56,27 +82,39 @@ Vue.component('my-game', {
                        </div>
                        <my-chat v-if="showChat">
                        </my-chat>
-                       <my-board v-bind:vr="vr">
+                       <my-board v-bind:vr="vr" :mode="mode" :orientation="orientation" :user-color="mycolor" @play-move="play">
                        </my-board>
-                       <div v-show="showFen" id="fen-div" class="section-content">
+                       <div class="button-group">
+                               <button @click="() => play()">Play</button>
+                               <button @click="() => undo()">Undo</button>
+                               <button @click="flip">Flip</button>
+                               <button @click="gotoBegin">GotoBegin</button>
+                               <button @click="gotoEnd">GotoEnd</button>
+                       </div>
+                       <div v-if="showFen && !!vr" id="fen-div" class="section-content">
                                <p id="fen-string" class="text-center">
                                        {{ vr.getFen() }}
                                </p>
                        </div>
                        <div id="pgn-div" class="section-content">
-                               <a id="download" href"#">
+                               <a id="download" href="#">
                                </a>
                                <button id="downloadBtn" @click="download">
-                                       {{ translations["Download PGN"] }}
+                                       {{ translate("Download PGN") }}
                                </button>
                        </div>
-                       <my-move-list v-if="showMoves">
+                       <my-move-list v-if="showMoves" :moves="moves" :cursor="cursor" @goto-move="gotoMove">
                        </my-move-list>
                </div>
        `,
        created: function() {
-               const url = socketUrl;
-               this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant._id);
+               if (!!this.gameId)
+                       this.loadGame();
+               else if (!!this.fen)
+               {
+                       this.vr = new VariantRules(this.fen);
+                       this.fenStart = this.fen;
+               }
                // TODO: after game, archive in indexedDB
                // TODO: this events listener is central. Refactor ? How ?
                const socketMessageListener = msg => {
@@ -151,7 +189,6 @@ Vue.component('my-game', {
                };
 
                const socketCloseListener = () => {
-                       this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant._id);
                        this.conn.addEventListener('message', socketMessageListener);
                        this.conn.addEventListener('close', socketCloseListener);
                };
@@ -184,10 +221,14 @@ Vue.component('my-game', {
                        }, delay);
                }
        },
-       //TODO: conn pourrait être une prop, donnée depuis variant.js
+       // this.conn est une prop, donnée depuis variant.js
        //dans variant.js (plutôt room.js) conn gère aussi les challenges
        // Puis en webRTC, repenser tout ça.
        methods: {
+               translate: translate,
+               loadGame: function() {
+                       // TODO: load this.gameId ...
+               },
                setEndgameMessage: function(score) {
                        let eogMessage = "Undefined";
                        switch (score)
@@ -285,12 +326,25 @@ Vue.component('my-game', {
                        }, 250);
                },
                play: function(move, programmatic) {
+                       // Forbid playing outside analyze mode when cursor isn't at moves.length-1
+                       if (this.mode != "analyze" && this.cursor < this.moves.length-1)
+                               return;
+                       let navigate = !move;
+                       if (navigate)
+                       {
+                               if (this.cursor == this.moves.length)
+                                       return; //no more moves
+                               move = this.moves[this.cursor];
+                       }
                        if (!!programmatic) //computer or human opponent
                                return this.animateMove(move);
                        // Not programmatic, or animation is over
                        if (!move.notation)
                                move.notation = this.vr.getNotation(move);
+                       if (!move.color)
+                               move.color = this.vr.turn;
                        this.vr.play(move);
+                       this.cursor++;
                        if (!move.fen)
                                move.fen = this.vr.getFen();
                        if (this.sound == 2)
@@ -306,10 +360,13 @@ Vue.component('my-game', {
                                // Send the move to web worker (including his own moves)
                                this.compWorker.postMessage(["newmove",move]);
                        }
-                       if (this.score == "*" || this.mode == "analyze")
+                       if (!navigate && (this.score == "*" || this.mode == "analyze"))
                        {
-                               // Stack move on movesList
-                               this.moves.push(move);
+                               // Stack move on movesList at current cursor
+                               if (this.cursor == this.moves.length)
+                                       this.moves.push(move);
+                               else
+                                       this.moves = this.moves.slice(0,this.cursor-1).concat([move]);
                        }
                        // Is opponent in check?
                        this.incheck = this.vr.getCheckSquares(this.vr.turn);
@@ -324,18 +381,45 @@ Vue.component('my-game', {
                        }
                        else if (this.mode == "computer" && this.vr.turn != this.userColor)
                                this.playComputerMove();
+                       if (navigate)
+                               this.$children[0].$forceUpdate(); //TODO!?
                },
                undo: function(move) {
+                       let navigate = !move;
+                       if (navigate)
+                       {
+                               if (this.cursor == 0)
+                                       return; //no more moves
+                               move = this.moves[this.cursor-1];
+                       }
                        this.vr.undo(move);
+                       this.cursor--;
+                       if (navigate)
+                               this.$children[0].$forceUpdate(); //TODO!?
                        if (this.sound == 2)
                                new Audio("/sounds/undo.mp3").play().catch(err => {});
                        this.incheck = this.vr.getCheckSquares(this.vr.turn);
-                       if (this.mode == "analyze")
+                       if (!navigate && this.mode == "analyze")
                                this.moves.pop();
+                       if (navigate)
+                               this.$forceUpdate(); //TODO!?
+               },
+               gotoMove: function(index) {
+                       this.vr = new VariantRules(this.moves[index].fen);
+                       this.cursor = index+1;
+               },
+               gotoBegin: function() {
+                       this.vr = new VariantRules(this.fenStart);
+                       this.cursor = 0;
+               },
+               gotoEnd: function() {
+                       this.gotoMove(this.moves.length-1);
+               },
+               flip: function() {
+                       this.orientation = V.GetNextCol(this.orientation);
                },
        },
 })
-// cursor + ........
 //TODO: confirm dialog with "opponent offers draw", avec possible bouton "prevent future offers" + bouton "proposer nulle"
 //+ bouton "abort" avec score == "?" + demander confirmation pour toutes ces actions,
 //comme sur lichess