From 43226f904d05a747c2b4043775fcbd43e916221d Mon Sep 17 00:00:00 2001 From: noyciy7037 Date: Sun, 13 Nov 2022 21:30:01 +0900 Subject: [PATCH] add card distr and some fix --- static/js/cards.js | 2 + static/js/game-client.js | 51 ++++++++++++++++++++++++- static/js/host-controller.js | 68 ++++++++++++---------------------- static/js/player-controller.js | 23 +++++++++--- static/js/websocket.js | 27 ++++++++++++-- 5 files changed, 116 insertions(+), 55 deletions(-) diff --git a/static/js/cards.js b/static/js/cards.js index 173a650..47bc0b4 100644 --- a/static/js/cards.js +++ b/static/js/cards.js @@ -1,6 +1,8 @@ +"use strict"; const cardTypes = { unknown: { id: "unknown", + prefix: "!", count: -1 }, one: { diff --git a/static/js/game-client.js b/static/js/game-client.js index ba0405b..5866f77 100644 --- a/static/js/game-client.js +++ b/static/js/game-client.js @@ -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)); + } } \ No newline at end of file diff --git a/static/js/host-controller.js b/static/js/host-controller.js index b6d036f..72d8229 100644 --- a/static/js/host-controller.js +++ b/static/js/host-controller.js @@ -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(); } } \ No newline at end of file diff --git a/static/js/player-controller.js b/static/js/player-controller.js index e16e870..848da82 100644 --- a/static/js/player-controller.js +++ b/static/js/player-controller.js @@ -1,19 +1,30 @@ +"use strict"; class PlayerController { hostClientId; + players = new Map(); constructor() { } - onGameMessage(message){ - message.body.commands.forEach(command=>{ - switch(command.name){ + 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() { } } \ No newline at end of file diff --git a/static/js/websocket.js b/static/js/websocket.js index f115c4d..9f9c435 100644 --- a/static/js/websocket.js +++ b/static/js/websocket.js @@ -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"){ + } else if (obj.messageType == "GAME") { controller.onGameMessage(obj); } -}); \ No newline at end of file +}); + +/* +// ユーザのスクリプトを実行する +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); +}*/ \ No newline at end of file