ExtinctionOnline/static/js/host-controller.js

117 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

2022-11-13 21:30:01 +09:00
"use strict";
2022-11-04 22:05:08 +09:00
const firstCardCount = 5;
class HostController {
playerController = null;
deck = new Array();
discarded = new Array();
2022-11-13 21:30:01 +09:00
players = new Map();
turnOrder;
turn = -1;
2023-06-10 00:06:54 +09:00
latestWaitingId = 0;
waiting = new Map();
2022-11-04 22:05:08 +09:00
constructor() {
this.playerController = new PlayerController();
}
2023-06-10 00:06:54 +09:00
async gameStart() {
// ターンを決定
this.turnOrder = EXOUtils.shuffleArray([...this.players.keys()]);
// ゲームを開始
2022-11-13 21:30:01 +09:00
let startMessage = new MessageBuilder().game();
startMessage.addCommand(commands.gameStart, null, ...this.turnOrder).send();
2022-11-13 21:30:01 +09:00
// すべてのカードを生成
2022-11-04 22:05:08 +09:00
Object.keys(cardTypes).forEach(key => {
for (let i = 0; i < cardTypes[key].count; ++i)
this.deck.push(new Card(cardTypes[key], i + 1));
});
2022-11-13 21:30:01 +09:00
2022-11-04 22:05:08 +09:00
// 山札をシャッフル
EXOUtils.shuffleArray(this.deck);
2022-11-13 21:30:01 +09:00
2023-06-10 00:06:54 +09:00
await this.distributionCards();
this.nextTurn();
}
async distributionCards() {
for (let i = 0; i < firstCardCount; ++i) {
for (const player of this.players) {
await player[1].addCard(this.deck[0]);
this.deck.splice(0, 1);
await new Promise(resolve => setTimeout(resolve, 700));
}
}
/*
2022-11-13 21:30:01 +09:00
// プレイヤーに5枚ずつ配布
for (const player of this.players) {
// 本人通達用
let messageBuilder = new MessageBuilder(player[0]).game();
// 他人通達用
let messageBuilderForRoom = new MessageBuilder().game();
2022-11-04 22:05:08 +09:00
for (let j = 0; j < firstCardCount; ++j) {
2022-11-13 21:30:01 +09:00
player[1].cards.push(this.deck[0]);
messageBuilder.addCommand(commands.addCard, player[0], this.deck[0].cardType.id, this.deck[0].idIndex);
messageBuilderForRoom.addCommand(commands.addCard, player[0], cardTypes.unknown.id, -1)
2022-11-04 22:05:08 +09:00
this.deck.splice(0, 1);
2022-11-04 22:05:08 +09:00
}
2022-11-13 21:30:01 +09:00
messageBuilder.send();
messageBuilderForRoom.send();
}*/
2022-11-04 22:05:08 +09:00
}
2023-06-10 00:06:54 +09:00
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));
// 操作待ち
}
2022-11-04 22:05:08 +09:00
onGameMessage(message) {
if (message.body.side != side.host)
this.playerController.onGameMessage(message);
2022-11-04 22:05:08 +09:00
}
joinNewPlayer(obj) {
2022-11-13 21:30:01 +09:00
this.players.set(obj.from, new Player(obj.from));
let messageBuilder = new MessageBuilder().game();
messageBuilder.addCommand(commands.syncRoomData, null, clientId, [...this.players.keys()]);
messageBuilder.send();
2022-11-04 22:05:08 +09:00
}
2023-06-10 00:06:54 +09:00
clientOperations = {
selectedCards: () => { }
}
cardCommands = {
remove: (target, card, index, canRecycle) => {
const player = controller.players.get(target);
2023-06-10 00:06:54 +09:00
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);
2023-06-10 00:06:54 +09:00
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);
},
2023-06-10 00:06:54 +09:00
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();
},
};
2022-11-04 22:05:08 +09:00
}