3 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
4 BaseGame(:variant="variant.name" :analyze="analyze" : players="players")
8 import BaseGame from "@/components/BaseGame.vue";
9 import { store } from "@/store";
10 import Worker from 'worker-loader!@/playCompMove';
13 name: 'my-computer-game',
17 // mode: "auto" (game comp vs comp), "versus" (normal) or "analyze"
18 props: ["fen","mode","variant"],
22 // Web worker to play computer moves without freezing interface:
23 timeStart: undefined, //time when computer starts thinking
24 lockCompThink: false, //to avoid some ghost moves
28 //TODO: players ? Computed?
34 return this.mode == "analyze";
39 // (Security) No effect if a computer move is in progress:
40 if (this.lockCompThink)
41 return this.$emit("computer-think");
45 // 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.variant.name != "Dark";
60 this.play(compMove[0], animate);
61 if (compMove.length == 2)
62 setTimeout( () => { this.play(compMove[1], animate); }, 750);
63 else //250 == length of animation (TODO: should be a constant somewhere)
64 setTimeout( () => this.lockCompThink = false, 250);
68 // dans variant.js (plutôt room.js) conn gère aussi les challenges
69 // et les chats dans chat.js. Puis en webRTC, repenser tout ça.
71 launchGame: async function() {
72 const vModule = await import("@/variants/" + this.variant.name + ".js");
73 window.V = vModule.VariantRules;
74 this.compWorker.postMessage(["scripts",this.variant.name]);
75 this.newGameFromFen(this.fen);
77 newGameFromFen: function(fen) {
83 if (this.mode == "analyze")
85 this.mycolor = V.ParseFen(fen).turn;
86 this.orientation = this.mycolor;
88 else if (this.mode == "computer") //only other alternative (HH with gameId)
90 this.mycolor = (Math.random() < 0.5 ? "w" : "b");
91 this.orientation = this.mycolor;
92 this.compWorker.postMessage(["init",fen]);
93 if (this.mycolor != "w" || this.subMode == "auto")
94 this.playComputerMove();
97 playComputerMove: function() {
98 this.timeStart = Date.now();
99 this.compWorker.postMessage(["askmove"]);