add card distr and some fix

This commit is contained in:
noyciy7037 2022-11-13 21:30:01 +09:00
parent b9d8c3a5b8
commit 43226f904d
Signed by: noyciy7037
GPG Key ID: 46A54FA18BBA58FF
5 changed files with 116 additions and 55 deletions

View File

@ -1,6 +1,8 @@
"use strict";
const cardTypes = {
unknown: {
id: "unknown",
prefix: "!",
count: -1
},
one: {

View File

@ -1,8 +1,9 @@
"use strict";
let clientId = null;
let controller = null;
let roomData = null;
const commands = { gameStart: "GameStart", addCard:"AddCard"};
const commands = { syncRoomData: "SyncRoomData", gameStart: "GameStart", addCard: "AddCard" };
function onSystemMessage(obj) {
if (clientId === null) {
@ -29,16 +30,62 @@ function joinToRoom(id, name) {
roomName: name
}
};
console.log(obj);
socket.send(JSON.stringify(obj));
}
class Card {
cardType;
idIndex;
id;
constructor(type, idIndex) {
this.cardType = type;
this.idIndex = idIndex;
this.id = `${type.prefix}-${type.count}-${idIndex}`;
}
}
class Player {
clientId;
cards = new Array();
constructor(id) {
this.clientId = id;
}
}
class MessageBuilder {
object;
constructor(to) {
this.object = {
from: clientId,
roomData: roomData,
deliveryTo: {
type: to == null ? "ROOM" : "CLIENT",
clientId: to
},
body: {
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));
}
}

View File

@ -1,30 +1,25 @@
"use strict";
const firstCardCount = 5;
class HostController {
playerController = null;
deck = new Array();
players = new Array();
players = new Map();
constructor() {
this.playerController = new PlayerController();
}
gameStart() {
socket.send(JSON.stringify({
from: clientId,
messageType: "GAME",
roomData: roomData,
deliveryTo: {
type: "ROOM"
},
body: {
commands: [{ name: commands.gameStart }]
}
}));
let startMessage = new MessageBuilder().game();
startMessage.addCommand(commands.gameStart).send();
// すべてのカードを生成
Object.keys(cardTypes).forEach(key => {
for (let i = 0; i < cardTypes[key].count; ++i)
this.deck.push(new Card(cardTypes[key], i + 1));
});
// 山札をシャッフル
let currentIndex = this.deck.length;
while (currentIndex) {
@ -33,47 +28,32 @@ class HostController {
this.deck[currentIndex] = this.deck[j];
this.deck[j] = t;
}
for (let i = 0; i < this.players.length; ++i) {
let messageObject = {
from: clientId,
messageType: "GAME",
roomData: roomData,
deliveryTo: {
type: "CLIENT",
clientId: this.players[i].clientId
},
body: {
commands: []
}
};
// プレイヤーに5枚ずつ配布
for (const player of this.players) {
// 本人通達用
let messageBuilder = new MessageBuilder(player[0]).game();
// 他人通達用
let messageBuilderForRoom = new MessageBuilder().game();
for (let j = 0; j < firstCardCount; ++j) {
messageObject.body.commands.push({
name: commands.addCard,
target: this.players[i].clientId,
args: [
this.deck[0].cardType.id,
this.deck[0].id
]
});
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)
this.deck.splice(0, 1);
}
socket.send(JSON.stringify(messageObject));
messageBuilder.send();
messageBuilderForRoom.send();
}
}
onGameMessage(message) {
this.playerController.onGameMessage(message);
}
joinNewPlayer(obj) {
this.players.push(new Player(obj.from));
}
}
class Player {
clientId;
cards = new Array();
constructor(id) {
this.clientId = id;
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();
}
}

View File

@ -1,5 +1,7 @@
"use strict";
class PlayerController {
hostClientId;
players = new Map();
constructor() {
}
@ -7,13 +9,22 @@ class PlayerController {
onGameMessage(message) {
message.body.commands.forEach(command => {
switch (command.name) {
case commands.syncRoomData:
if (message.from !== command.args[0]) { socket.close(); return; }
this.hostClientId = command.args[0];
this.players = new Map(command.args[1].map(playerId => [playerId, new Player(playerId)]));
break;
case commands.gameStart:
this.hostClientId = message.from;
if (message.from !== this.hostClientId) { socket.close(); return; }
break;
case commands.addCard:
if (message.from !== this.hostClientId) { socket.close(); return; }
if (command.target == clientId && command.args[0] == cardTypes.unknown.id) return;
this.players.get(command.target).cards.push(new Card(cardTypes[command.args[0]], command.args[1]));
break;
}
});
}
joinNewPlayer(obj){
}
joinNewPlayer() { }
}

View File

@ -1,3 +1,4 @@
"use strict";
// WebSocket 接続を作成
const socket = new WebSocket('ws://localhost:1234');
@ -8,11 +9,31 @@ socket.addEventListener('open', function (event) {
// メッセージの待ち受け
socket.addEventListener('message', function (event) {
console.log('Server>', event.data);
let obj = JSON.parse(event.data);
console.log('Server>', obj);
if (obj.messageType == "SYSTEM") {
onSystemMessage(obj);
} else if (obj.messageType == "GAME") {
controller.onGameMessage(obj);
}
});
/*
// ユーザのスクリプトを実行する
var funText = "\"use strict\";return \"TEST MESSAGE\";";
var workerFile = "\
function testFunction(){" + funText +
"}\
postMessage(testFunction());\
onmessage = function(e){console.log(e);\
}"
var blob = new Blob([workerFile], {
type: "text/javascript"
});
var worker = new Worker(window.URL.createObjectURL(blob));
worker.onmessage = function (e) {
console.log('Function result:', e.data);
}*/