diff --git a/static/js/card-module.js b/static/js/card-module.js index 51f0bc2..238d4be 100644 --- a/static/js/card-module.js +++ b/static/js/card-module.js @@ -1,4 +1,6 @@ "use strict"; +const AsyncFunction = async function () {}.constructor; + function discard(target, card) { console.log("DIS!CAR!D!"); postMessage({ type: "game", game: { command: "discard", target: target, card: card } }); @@ -16,7 +18,7 @@ onmessage = e => { console.log(e); switch (e.data.type) { case "run": - let fun = new Function("target", "players", e.data.function); + let fun = new AsyncFunction("target", "players", e.data.function); fun(e.data.target, e.data.players); break; } diff --git a/static/js/cards.js b/static/js/cards.js index 97c09fb..9c638d5 100644 --- a/static/js/cards.js +++ b/static/js/cards.js @@ -84,7 +84,8 @@ const cardTypes = { annihilation: { id: "annihilation", prefix: "I", - count: 1 + count: 1, + onGet: "players.forEach(player => {if(player.cards.length <= 5)player.cards.forEach(card => {discard(player,card);})});" }, drop: { id: "drop", diff --git a/static/js/game-client.js b/static/js/game-client.js index fe206b6..86db249 100644 --- a/static/js/game-client.js +++ b/static/js/game-client.js @@ -147,6 +147,9 @@ class EXOUtils { case "discard": controller.cardCommands.remove(e.data.game.target.clientId, e.data.game.card, null, true); break; + case "selectCard": + controller.cardCommands.selectCard(e.data.game.target.clientId, e.data.game.card, null, true); + break; } break; } diff --git a/static/js/host-controller.js b/static/js/host-controller.js index 06fc652..e2f80c4 100644 --- a/static/js/host-controller.js +++ b/static/js/host-controller.js @@ -8,12 +8,14 @@ class HostController { players = new Map(); turnOrder; turn = -1; + latestWaitingId = 0; + waiting = new Map(); constructor() { this.playerController = new PlayerController(); } - gameStart() { + async gameStart() { // ターンを決定 this.turnOrder = EXOUtils.shuffleArray([...this.players.keys()]); @@ -30,7 +32,8 @@ class HostController { // 山札をシャッフル EXOUtils.shuffleArray(this.deck); - this.distributionCards(); + await this.distributionCards(); + this.nextTurn(); } async distributionCards() { @@ -60,6 +63,20 @@ class HostController { }*/ } + async nextTurn() { + // ターンを進める + ++this.turn; + if (this.turnOrder.length >= this.turn) this.turn = 0; + + const turnPlayer = players[this.turnOrder[this.turn]]; + // カードを一枚引く + await turnPlayer.addCard(this.deck[0]); + this.deck.splice(0, 1); + await new Promise(resolve => setTimeout(resolve, 700)); + + // 操作待ち + } + onGameMessage(message) { if (message.body.side != side.host) this.playerController.onGameMessage(message); @@ -72,19 +89,29 @@ class HostController { messageBuilder.send(); } + clientOperations = { + selectedCards: () => { } + } + cardCommands = { remove: (target, card, index, canRecycle) => { const player = controller.players.get(target); - index = player.cards.findIndex(it => it.id == card.id); + index = player.cards.findIndex(it => it.id == card.id && it.cardType.id == card.cardType.id); card = player.cards[index]; if (canRecycle) this.discarded.push(...player.cards.splice(index, 1)); else this.cardCommands.backIntoDeck(card); - new MessageBuilder().game().addCommand(commands.worker, target, "remove", [target, card, index, canRecycle]).send(); + new MessageBuilder().game().addCommand(commands.worker, target, "remove", [target, { cardType: { id: card.cardType.id }, idIndex: card.idIndex }, index, canRecycle]).send(); }, backIntoDeck: (card) => { this.deck.splice(Math.floor(Math.random() * (this.deck.length + 1)), 0, card); }, + selectCard: (target, count, waitingId) => { + const player = controller.players.get(target); + waitingId = ++this.latestWaitingId; + this.waiting.set(waitingId, this.clientOperations.selectedCards); + new MessageBuilder(target).game().addCommand(commands.worker, target, "selectCard", [target, count, waitingId]).send(); + }, }; } \ No newline at end of file