add card distr and some fix
This commit is contained in:
parent
b9d8c3a5b8
commit
43226f904d
@ -1,6 +1,8 @@
|
|||||||
|
"use strict";
|
||||||
const cardTypes = {
|
const cardTypes = {
|
||||||
unknown: {
|
unknown: {
|
||||||
id: "unknown",
|
id: "unknown",
|
||||||
|
prefix: "!",
|
||||||
count: -1
|
count: -1
|
||||||
},
|
},
|
||||||
one: {
|
one: {
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
|
"use strict";
|
||||||
let clientId = null;
|
let clientId = null;
|
||||||
let controller = null;
|
let controller = null;
|
||||||
let roomData = null;
|
let roomData = null;
|
||||||
|
|
||||||
const commands = { gameStart: "GameStart", addCard:"AddCard"};
|
const commands = { syncRoomData: "SyncRoomData", gameStart: "GameStart", addCard: "AddCard" };
|
||||||
|
|
||||||
function onSystemMessage(obj) {
|
function onSystemMessage(obj) {
|
||||||
if (clientId === null) {
|
if (clientId === null) {
|
||||||
@ -29,16 +30,62 @@ function joinToRoom(id, name) {
|
|||||||
roomName: name
|
roomName: name
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
console.log(obj);
|
|
||||||
socket.send(JSON.stringify(obj));
|
socket.send(JSON.stringify(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
class Card {
|
class Card {
|
||||||
cardType;
|
cardType;
|
||||||
|
idIndex;
|
||||||
id;
|
id;
|
||||||
|
|
||||||
constructor(type, idIndex) {
|
constructor(type, idIndex) {
|
||||||
this.cardType = type;
|
this.cardType = type;
|
||||||
|
this.idIndex = idIndex;
|
||||||
this.id = `${type.prefix}-${type.count}-${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));
|
||||||
|
}
|
||||||
|
}
|
@ -1,30 +1,25 @@
|
|||||||
|
"use strict";
|
||||||
const firstCardCount = 5;
|
const firstCardCount = 5;
|
||||||
|
|
||||||
class HostController {
|
class HostController {
|
||||||
playerController = null;
|
playerController = null;
|
||||||
deck = new Array();
|
deck = new Array();
|
||||||
players = new Array();
|
players = new Map();
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.playerController = new PlayerController();
|
this.playerController = new PlayerController();
|
||||||
}
|
}
|
||||||
|
|
||||||
gameStart() {
|
gameStart() {
|
||||||
socket.send(JSON.stringify({
|
let startMessage = new MessageBuilder().game();
|
||||||
from: clientId,
|
startMessage.addCommand(commands.gameStart).send();
|
||||||
messageType: "GAME",
|
|
||||||
roomData: roomData,
|
// すべてのカードを生成
|
||||||
deliveryTo: {
|
|
||||||
type: "ROOM"
|
|
||||||
},
|
|
||||||
body: {
|
|
||||||
commands: [{ name: commands.gameStart }]
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
Object.keys(cardTypes).forEach(key => {
|
Object.keys(cardTypes).forEach(key => {
|
||||||
for (let i = 0; i < cardTypes[key].count; ++i)
|
for (let i = 0; i < cardTypes[key].count; ++i)
|
||||||
this.deck.push(new Card(cardTypes[key], i + 1));
|
this.deck.push(new Card(cardTypes[key], i + 1));
|
||||||
});
|
});
|
||||||
|
|
||||||
// 山札をシャッフル
|
// 山札をシャッフル
|
||||||
let currentIndex = this.deck.length;
|
let currentIndex = this.deck.length;
|
||||||
while (currentIndex) {
|
while (currentIndex) {
|
||||||
@ -33,47 +28,32 @@ class HostController {
|
|||||||
this.deck[currentIndex] = this.deck[j];
|
this.deck[currentIndex] = this.deck[j];
|
||||||
this.deck[j] = t;
|
this.deck[j] = t;
|
||||||
}
|
}
|
||||||
for (let i = 0; i < this.players.length; ++i) {
|
|
||||||
let messageObject = {
|
// プレイヤーに5枚ずつ配布
|
||||||
from: clientId,
|
for (const player of this.players) {
|
||||||
messageType: "GAME",
|
// 本人通達用
|
||||||
roomData: roomData,
|
let messageBuilder = new MessageBuilder(player[0]).game();
|
||||||
deliveryTo: {
|
// 他人通達用
|
||||||
type: "CLIENT",
|
let messageBuilderForRoom = new MessageBuilder().game();
|
||||||
clientId: this.players[i].clientId
|
|
||||||
},
|
|
||||||
body: {
|
|
||||||
commands: []
|
|
||||||
}
|
|
||||||
};
|
|
||||||
for (let j = 0; j < firstCardCount; ++j) {
|
for (let j = 0; j < firstCardCount; ++j) {
|
||||||
messageObject.body.commands.push({
|
player[1].cards.push(this.deck[0]);
|
||||||
name: commands.addCard,
|
messageBuilder.addCommand(commands.addCard, player[0], this.deck[0].cardType.id, this.deck[0].idIndex);
|
||||||
target: this.players[i].clientId,
|
messageBuilderForRoom.addCommand(commands.addCard, player[0], cardTypes.unknown.id, -1)
|
||||||
args: [
|
|
||||||
this.deck[0].cardType.id,
|
|
||||||
this.deck[0].id
|
|
||||||
]
|
|
||||||
});
|
|
||||||
this.deck.splice(0, 1);
|
this.deck.splice(0, 1);
|
||||||
}
|
}
|
||||||
socket.send(JSON.stringify(messageObject));
|
messageBuilder.send();
|
||||||
|
messageBuilderForRoom.send();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onGameMessage(message) {
|
onGameMessage(message) {
|
||||||
|
this.playerController.onGameMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
joinNewPlayer(obj) {
|
joinNewPlayer(obj) {
|
||||||
this.players.push(new Player(obj.from));
|
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();
|
||||||
class Player {
|
|
||||||
clientId;
|
|
||||||
cards = new Array();
|
|
||||||
|
|
||||||
constructor(id) {
|
|
||||||
this.clientId = id;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,7 @@
|
|||||||
|
"use strict";
|
||||||
class PlayerController {
|
class PlayerController {
|
||||||
hostClientId;
|
hostClientId;
|
||||||
|
players = new Map();
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
}
|
}
|
||||||
@ -7,13 +9,22 @@ class PlayerController {
|
|||||||
onGameMessage(message) {
|
onGameMessage(message) {
|
||||||
message.body.commands.forEach(command => {
|
message.body.commands.forEach(command => {
|
||||||
switch (command.name) {
|
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:
|
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;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
joinNewPlayer(obj){
|
joinNewPlayer() { }
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
"use strict";
|
||||||
// WebSocket 接続を作成
|
// WebSocket 接続を作成
|
||||||
const socket = new WebSocket('ws://localhost:1234');
|
const socket = new WebSocket('ws://localhost:1234');
|
||||||
|
|
||||||
@ -8,11 +9,31 @@ socket.addEventListener('open', function (event) {
|
|||||||
|
|
||||||
// メッセージの待ち受け
|
// メッセージの待ち受け
|
||||||
socket.addEventListener('message', function (event) {
|
socket.addEventListener('message', function (event) {
|
||||||
console.log('Server>', event.data);
|
|
||||||
let obj = JSON.parse(event.data);
|
let obj = JSON.parse(event.data);
|
||||||
|
console.log('Server>', obj);
|
||||||
if (obj.messageType == "SYSTEM") {
|
if (obj.messageType == "SYSTEM") {
|
||||||
onSystemMessage(obj);
|
onSystemMessage(obj);
|
||||||
} else if (obj.messageType == "GAME") {
|
} else if (obj.messageType == "GAME") {
|
||||||
controller.onGameMessage(obj);
|
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);
|
||||||
|
}*/
|
Loading…
Reference in New Issue
Block a user