import Channel from "../channel.js"; import { cancelGame, gameReady, putMove } from "../../../../services/shogi/index.js"; export default class extends Channel { chName = "shogiGame"; static shouldShare = false; static requireCredential = false; gameId = null; constructor(id, connection){ super(id, connection); this.onEvent = this.onEvent.bind(this); } async init(params) { if (typeof params.gameId !== "string") return; this.gameId = params.gameId; this.subscriber.on(`shogiGameStream:${this.gameId}`, this.onEvent); } onEvent(data) { this.send(data); } onMessage(type, body) { if (!this.gameId) return; switch(type){ case "ready": if (this.user && typeof body === "boolean") gameReady(this.gameId, this.user, body); break; case "cancel": if (this.user) cancelGame(this.gameId, this.user); break; case "move": if (this.user && body && typeof body.usi === "string") { putMove(this.gameId, this.user, body.usi, typeof body.id === "string" ? body.id : null); } break; } } dispose() { if (this.gameId) { this.subscriber.off(`shogiGameStream:${this.gameId}`, this.onEvent); } } }