3 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
4 BaseGame(:game="game" :vr="vr" ref="basegame"
5 @newmove="processMove" @gameover="gameOver")
9 import BaseGame from "@/components/BaseGame.vue";
10 import { store } from "@/store";
11 import Worker from 'worker-loader!@/playCompMove';
14 name: 'my-computer-game',
18 // gameInfo: fen + mode + vname
19 // mode: "auto" (game comp vs comp), "versus" (normal) or "analyze"
26 // Web worker to play computer moves without freezing interface:
27 timeStart: undefined, //time when computer starts thinking
28 lockCompThink: false, //to avoid some ghost moves
33 "gameInfo.fen": function() {
34 // (Security) No effect if a computer move is in progress:
35 if (this.lockCompThink)
36 return this.$emit("computer-think");
39 "gameInfo.userStop": function() {
40 if (this.gameInfo.userStop)
42 // User stopped the game: unknown result
43 this.game.mode = "analyze";
47 // Modal end of game, and then sub-components
49 // Computer moves web worker logic: (TODO: also for observers in HH games ?)
50 this.compWorker = new Worker(); //'/javascripts/playCompMove.js'),
51 this.compWorker.onmessage = e => {
52 this.lockCompThink = true; //to avoid some ghost moves
53 let compMove = e.data;
54 if (!Array.isArray(compMove))
55 compMove = [compMove]; //to deal with MarseilleRules
56 // Small delay for the bot to appear "more human"
57 const delay = Math.max(500-(Date.now()-this.timeStart), 0);
59 const animate = (this.gameInfo.vname != "Dark");
60 this.$refs.basegame.play(compMove[0], animate);
61 if (compMove.length == 2)
62 setTimeout( () => { this.$refs.basegame.play(compMove[1], animate); }, 750);
63 else //250 == length of animation (TODO: should be a constant somewhere)
64 setTimeout( () => this.lockCompThink = false, 250);
67 if (!!this.gameInfo.fen)
70 // dans variant.js (plutôt room.js) conn gère aussi les challenges
71 // et les chats dans chat.js. Puis en webRTC, repenser tout ça.
73 launchGame: async function() {
74 const vModule = await import("@/variants/" + this.gameInfo.vname + ".js");
75 window.V = vModule.VariantRules;
76 this.compWorker.postMessage(["scripts",this.gameInfo.vname]);
77 this.compWorker.postMessage(["init",this.gameInfo.fen]);
78 this.vr = new V(this.gameInfo.fen);
79 const mycolor = (Math.random() < 0.5 ? "w" : "b");
80 let players = ["Myself","Computer"];
82 players = players.reverse();
83 // NOTE: (TODO?) fen and fenStart are redundant in game object
84 this.game = Object.assign({},
87 fenStart: this.gameInfo.fen,
92 this.compWorker.postMessage(["init",this.gameInfo.fen]);
93 if (mycolor != "w" || this.gameInfo.mode == "auto")
94 this.playComputerMove();
96 playComputerMove: function() {
97 this.timeStart = Date.now();
98 this.compWorker.postMessage(["askmove"]);
100 // TODO: do not process if game is over (check score ?)
101 processMove: function(move) {
102 // Send the move to web worker (including his own moves)
103 this.compWorker.postMessage(["newmove",move]);
104 // subTurn condition for Marseille (and Avalanche) rules
105 if ((!this.vr.subTurn || this.vr.subTurn <= 1)
106 && (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor))
108 this.playComputerMove();
111 gameOver: function(score) {
112 // Just switch to analyze mode: no user action can set score
113 this.game.mode = "analyze";