2022-11-13 21:30:01 +09:00
|
|
|
"use strict";
|
2022-11-04 22:05:08 +09:00
|
|
|
let clientId = null;
|
|
|
|
let controller = null;
|
|
|
|
let roomData = null;
|
|
|
|
|
2023-04-23 12:40:54 +09:00
|
|
|
const commands = {
|
|
|
|
syncRoomData: "SyncRoomData", // args: ホストのPlayerID, [PlayerID...]
|
|
|
|
gameStart: "GameStart", // args: PlayerID(ターン順)...
|
|
|
|
addCard: "AddCard", // target: 追加するPlayerID; args: 追加するカードのID, カードのIndex
|
|
|
|
worker: "Worker", //
|
2025-08-31 18:28:13 +09:00
|
|
|
waiting: "Waiting",//
|
2023-04-23 12:40:54 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
const side = {
|
|
|
|
host: "HOST",
|
|
|
|
player: "PLAYER",
|
|
|
|
both: "BOTH"
|
|
|
|
}
|
2022-11-04 22:05:08 +09:00
|
|
|
|
|
|
|
function onSystemMessage(obj) {
|
|
|
|
if (clientId === null) {
|
|
|
|
clientId = obj.deliveryTo.clientId;
|
|
|
|
} else if (obj.from === clientId) {
|
|
|
|
roomData = obj.roomData;
|
|
|
|
controller.joinNewPlayer(obj);
|
|
|
|
} else {
|
|
|
|
controller.joinNewPlayer(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function joinToRoom(id, name) {
|
|
|
|
if (id == null)
|
|
|
|
controller = new HostController();
|
|
|
|
else
|
|
|
|
controller = new PlayerController();
|
|
|
|
|
|
|
|
let obj = {
|
|
|
|
messageType: "SYSTEM",
|
|
|
|
from: clientId,
|
|
|
|
roomData: {
|
|
|
|
roomId: id,
|
|
|
|
roomName: name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
socket.send(JSON.stringify(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
class Card {
|
|
|
|
cardType;
|
2022-11-13 21:30:01 +09:00
|
|
|
idIndex;
|
2022-11-04 22:05:08 +09:00
|
|
|
id;
|
|
|
|
|
|
|
|
constructor(type, idIndex) {
|
|
|
|
this.cardType = type;
|
2022-11-13 21:30:01 +09:00
|
|
|
this.idIndex = idIndex;
|
2022-11-04 22:05:08 +09:00
|
|
|
this.id = `${type.prefix}-${type.count}-${idIndex}`;
|
|
|
|
}
|
2022-11-13 21:30:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
class Player {
|
|
|
|
clientId;
|
|
|
|
cards = new Array();
|
|
|
|
|
|
|
|
constructor(id) {
|
|
|
|
this.clientId = id;
|
|
|
|
}
|
2023-04-23 12:40:54 +09:00
|
|
|
|
2025-08-31 18:28:13 +09:00
|
|
|
// Side: Host
|
2023-04-23 12:40:54 +09:00
|
|
|
async addCard(card) {
|
|
|
|
this.cards.push(card);
|
|
|
|
new MessageBuilder(this.clientId).game().addCommand(commands.addCard, this.clientId, card.cardType.id, card.idIndex).send();
|
|
|
|
new MessageBuilder().game().addCommand(commands.addCard, this.clientId, cardTypes.unknown.id, -1).send();
|
|
|
|
if (card.cardType.onGet) {
|
|
|
|
EXOUtils.capsuleExecute(card.cardType.onGet, this);
|
|
|
|
}
|
|
|
|
}
|
2022-11-13 21:30:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
class MessageBuilder {
|
|
|
|
object;
|
|
|
|
|
2023-04-23 12:40:54 +09:00
|
|
|
constructor(to, trSide = side.player) {
|
2022-11-13 21:30:01 +09:00
|
|
|
this.object = {
|
|
|
|
from: clientId,
|
|
|
|
roomData: roomData,
|
|
|
|
deliveryTo: {
|
|
|
|
type: to == null ? "ROOM" : "CLIENT",
|
|
|
|
clientId: to
|
|
|
|
},
|
|
|
|
body: {
|
2023-04-23 12:40:54 +09:00
|
|
|
side: trSide,
|
2022-11-13 21:30:01 +09:00
|
|
|
commands: []
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
game() {
|
|
|
|
this.object.messageType = "GAME";
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
addCommand(name, target, ...args) {
|
|
|
|
this.object.body.commands.push({
|
|
|
|
name: name,
|
|
|
|
target: target,
|
|
|
|
args: args
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
send() {
|
|
|
|
socket.send(JSON.stringify(this.object));
|
2025-08-31 18:28:13 +09:00
|
|
|
this.object = null;
|
2023-04-23 12:40:54 +09:00
|
|
|
}
|
2022-11-04 22:05:08 +09:00
|
|
|
}
|