UNPKG

xchess

Version:

Chess Engine

73 lines (55 loc) 975 B
export {IdleDrawOffer} import {Color} from './color.js' class IdleDrawOffer { #context; constructor(context){ this.#context = context; } get context(){ return this.#context; } get isDrawOffer(){ return false; } next(){ return this; } draw(color){ const drawOffer = new DrawOffer(this.context, color); this.context.drawOffer = drawOffer; this.context.emit('draw-offer'); return true; } } class DrawOffer { #context; #color; #time = new Date(); constructor(context, color){ this.#context = context; this.#color = Color.from(color); } get context(){ return this.#context; } get color(){ return this.#color; } get time(){ return this.#time; } get isDrawOffer(){ return true; } draw(color){ if(this.color.opposite(color)){ this.context.toDraw(this.color); return true; } return false; } next(){ if(this.color === this.context.color) return this; return new IdleDrawOffer(this.context); } }