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 compThink: false, //avoid asking a new move while one is being searched
33 "gameInfo.fen": function() {
36 "gameInfo.score": function(newScore) {
39 this.game.score = newScore; //user action
40 this.game.mode = "analyze";
42 this.$emit("game-stopped"); //otherwise wait for comp
46 // Modal end of game, and then sub-components
48 // Computer moves web worker logic:
49 this.compWorker = new Worker();
50 this.compWorker.onmessage = e => {
51 let compMove = e.data;
53 return; //after game ends, no more moves, nothing to do
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 // NOTE: Dark and 2-moves are incompatible
60 const animate = (this.gameInfo.vname != "Dark");
61 const animDelay = (animate ? 250 : 0);
64 (function executeMove() {
65 self.$refs.basegame.play(compMove[moveIdx++], animate);
66 if (moveIdx >= compMove.length)
68 self.compThink = false;
69 if (self.game.score != "*") //user action
70 self.$emit("game-stopped");
73 setTimeout(executeMove, 500 + animDelay);
77 if (!!this.gameInfo.fen)
80 // dans variant.js (plutôt room.js) conn gère aussi les challenges
81 // et les chats dans chat.js. Puis en webRTC, repenser tout ça.
83 launchGame: async function() {
84 const vModule = await import("@/variants/" + this.gameInfo.vname + ".js");
85 window.V = vModule.VariantRules;
86 this.compWorker.postMessage(["scripts",this.gameInfo.vname]);
87 this.compWorker.postMessage(["init",this.gameInfo.fen]);
88 this.vr = new V(this.gameInfo.fen);
89 const mycolor = (Math.random() < 0.5 ? "w" : "b");
90 let players = [{name:"Myself"},{name:"Computer"}];
92 players = players.reverse();
93 // NOTE: fen and fenStart are redundant in game object
94 this.game = Object.assign({},
97 fenStart: this.gameInfo.fen,
102 this.compWorker.postMessage(["init",this.gameInfo.fen]);
103 if (mycolor != "w" || this.gameInfo.mode == "auto")
104 this.playComputerMove();
106 playComputerMove: function() {
107 this.timeStart = Date.now();
108 this.compThink = true;
109 this.compWorker.postMessage(["askmove"]);
111 processMove: function(move) {
112 // Send the move to web worker (including his own moves)
113 this.compWorker.postMessage(["newmove",move]);
114 // subTurn condition for Marseille (and Avalanche) rules
115 if ((!this.vr.subTurn || this.vr.subTurn <= 1)
116 && (this.gameInfo.mode == "auto" || this.vr.turn != this.game.mycolor))
118 this.playComputerMove();
121 gameOver: function(score) {
122 this.game.score = score;
123 this.game.mode = "analyze";
124 this.$emit("game-over", score); //bubble up to Rules.vue