ExtinctionOnline/static/js/host-controller.js

59 lines
2.0 KiB
JavaScript
Raw 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();
2022-11-13 21:30:01 +09:00
players = new Map();
2022-11-04 22:05:08 +09:00
constructor() {
this.playerController = new PlayerController();
}
gameStart() {
2022-11-13 21:30:01 +09:00
let startMessage = new MessageBuilder().game();
startMessage.addCommand(commands.gameStart).send();
// すべてのカードを生成
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
// 山札をシャッフル
let currentIndex = this.deck.length;
while (currentIndex) {
let j = Math.floor(Math.random() * currentIndex);
let t = this.deck[--currentIndex];
this.deck[currentIndex] = this.deck[j];
this.deck[j] = t;
}
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-13 21:30:01 +09:00
messageBuilder.send();
messageBuilderForRoom.send();
2022-11-04 22:05:08 +09:00
}
}
onGameMessage(message) {
2022-11-13 21:30:01 +09:00
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
}
}